android之 clipdraw 在 .xml中的使用 图片进度条 cpu扫描
lipDraw 代表从其他位图上截取的一个“图片片段”。
在xml文件中定义ClipDraw对象使用<clip... />, 其结构如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <clip xmlns:android="http://schemas.android.com/apk/res/android"
- android:drawable="@drawable/shuangta"
- android:clipOrientation=["vertical"]|["vertical"]
- android:gravity="top" | "bottom" | "left" | "right" | "center_vertical"|"fill_vertical" | "center_horizontal" | "fill_horizontal" | "center" |"fill" | "clip_vertical" | "clip_horizontal">
- </clip>
--> android:drawable :指定截取的源Drawable对象。
--> android:clipOrientation : 指定截取方向,可设置水平截取或者垂直截取。
--> android:gravity : 指定截取时对齐的方式。
使用ClipDraw对象时可调用setLevel(int level) 方法来设置截取的区域大小,当level为0时,截取的图片片段为空;当level为10000时,
截取整张图片。
下面实例:每200ms设置一次Level 的大小, 以实现上图图卷展开的效果:
.xml 文件 应放在Drawable 目录下,在layout 文件中调用:
- <?xml version="1.0" encoding="UTF-8"?>
- <clip xmlns:android="http://schemas.android.com/apk/res/android"
- android:drawable="@drawable/we"
- android:clipOrientation="horizontal"
- android:gravity="left">
- </clip>
Java 程序代码:
- public class ClipDrawableTest extends Activity
- {
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ImageView imageview = (ImageView) findViewById(R.id.image);
- // 实例化ClipDrawable对象
- final ClipDrawable drawable = (ClipDrawable)
- imageview.getDrawable();
- final Handler handler = new Handler()
- {
- @Override
- public void handleMessage(Message msg)
- {
- if (msg.what == 0x1233)
- {
- //更改截取的区域大小ֵ
- drawable.setLevel(drawable.getLevel() + 100);
- if (drawable.getLevel() >= 10000)
- {
- //显示完整图片后重新开始
- drawable.setLevel(0);
- }
- }
- }
- };
- final Timer timer = new Timer();
- timer.schedule(new TimerTask()
- {
- @Override
- public void run()
- {
- Message msg = new Message();
- msg.what = 0x1233;
- handler.sendMessage(msg);
- }
- }, 0, 200);
- }
- }