ClipDrawable 是一个挺好看的一个图片Drawable,操作起来也算比较简单。下面先把代码贴上来,通过例子进行说明。


main.xml文件的内容:


[html] view plain copy print?

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     android:layout_width="fill_parent"  

  4.     android:layout_height="fill_parent"  

  5.     android:orientation="vertical" >  

  6.   

  7.     <ImageView   

  8.         android:id="@+id/p_w_picpath"  

  9.         android:layout_width="fill_parent"  

  10.         android:layout_height="wrap_content"  

  11.         android:src="@drawable/my_clip"  

  12.         />  

  13.           

  14. </LinearLayout>  



这里面重点是android:sec="@drawable/my_clip",调用了这个配置文件。这个配置文件的内容是:


[html] view plain copy print?

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <clip xmlns:android="http://schemas.android.com/apk/res/android"   

  3.     android:drawable="@drawable/test"   

  4.     android:clipOrientation="horizontal"   

  5.     android:gravity="center">   

  6. </clip>  


上面定义了三个属性。属性一是drawable的图片内容。第二个属性是展开的方向,第三个属性大家都经常用,不阐述。


这样,一个clipDrawable的配置文件都已经搞定了。调用一个clip配置文件,展示效果。比较简单。也很容易给项目添加一点感官上面的好处。

下面是Activity的代码:


[java] view plain copy print?

  1. package cn.jason.drawable;  

  2.   

  3. import java.util.Timer;  

  4. import java.util.TimerTask;  

  5.   

  6. import android.app.Activity;  

  7. import android.os.Bundle;  

  8. import android.os.Handler;  

  9. import android.os.Message;  

  10. import android.widget.ImageView;  

  11.   

  12. public class ClipDrawable extends Activity {  

  13.     /** Called when the activity is first created. */  

  14.     @Override  

  15.     public void onCreate(Bundle savedInstanceState) {  

  16.         super.onCreate(savedInstanceState);  

  17.         setContentView(R.layout.main);  

  18.         ImageView p_w_picpathView = (ImageView) findViewById(R.id.p_w_picpath);  

  19.           

  20.         final android.graphics.drawable.ClipDrawable  drawable = (android.graphics.drawable.ClipDrawable) p_w_picpathView.getDrawable();  

  21.         final Handler handler = new Handler(){  

  22.             @Override  

  23.             public void handleMessage(Message msg) {  

  24.                 if (msg.what == 0x1233) {  

  25.                     drawable.setLevel(drawable.getLevel() + 200);  

  26.                       

  27.                 }  

  28.             }  

  29.         };  

  30.           

  31.         final Timer timer = new Timer();  

  32.         timer.schedule(new TimerTask() {  

  33.             @Override  

  34.             public void run() {  

  35.                 Message msg = new Message();  

  36.                 msg.what= 0x1233;  

  37.                 handler.sendMessage(msg);  

  38.                 if (drawable.getLevel() >=10000) {  

  39.                     timer.cancel();  

  40.                 }  

  41.             }  

  42.         }, 0,300);  

  43.     }  

  44.       

  45.       

  46.       

  47.       

  48.       

  49.       

  50.       

  51. }  

上面的代码定义了一个Handler,如果接收的是本程序的消息,则进行操作。特别注意。定义ClipDrawable的用法是把Drawable进行转换的。