ClipDrawable资源的使用

因别人空间代码字体太难看,复制过来

ClipDrawable代表从其它位图上截取一个"图片片段",XML中的根元素为<clip.../>,截取的方向由clipOrientation控制

下面以一个慢慢展开的图片为例

先定义一个ClipDrawable资源文件my_clip.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <clip xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:drawable="@drawable/fengjing"  
  4.     android:clipOrientation="horizontal"  
  5.     android:gravity="center">  
  6. </clip>  

接下来在主界面main.xml中的imageview中引用它

  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.     <!-- 这里imageView的src定义为clipdrawable资源 -->  
  7.     <ImageView  
  8.         android:id="@+id/image"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:src="@drawable/my_clip" />  
  12. </LinearLayout>  
主界面代码

  1. package WangLi.Resouce.ClipDrawableTest;  
  2.   
  3. import java.util.Timer;  
  4. import java.util.TimerTask;  
  5.   
  6. import android.app.Activity;  
  7. import android.graphics.drawable.ClipDrawable;  
  8. import android.os.Bundle;  
  9. import android.os.Handler;  
  10. import android.os.Message;  
  11. import android.widget.ImageView;  
  12.   
  13. public class ClipDrawableTest extends Activity {  
  14.     /** Called when the activity is first created. */  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.         ImageView imageview = (ImageView)findViewById(R.id.image);  
  20.         //获取图片所显示的ClipDrawble对象  
  21.         final ClipDrawable drawable = (ClipDrawable)imageview.getDrawable();  
  22.         final Handler handler = new Handler()  
  23.         {  
  24.             public void handleMessage(Message msg)  
  25.             {  
  26.                 if(msg.what == 0x1233)  
  27.                 {  
  28.                     //修改ClipDrawable的level值  
  29.                     drawable.setLevel(drawable.getLevel() +200);  
  30.                 }  
  31.             }  
  32.         };  
  33.         final Timer timer = new Timer();  
  34.         timer.schedule(new TimerTask()  
  35.         {  
  36.             public void run()  
  37.             {  
  38.                 Message msg = new Message();  
  39.                 msg.what = 0x1233;  
  40.                 //发送消息,通知应用修改ClipDrawable对象的level值  
  41.                 handler.sendMessage(msg);  
  42.                 //取消定时器  
  43.                 if(drawable.getLevel() >= 10000)  
  44.                 {  
  45.                     timer.cancel();  
  46.                 }  
  47.             }  
  48.         },0,300);  
  49.     }  
  50. }  
运行效果如下

刚开始图片从中间往两边展开


最后展开完整的图片


利用ClipDrawable,还可以做图片进度条

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值