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

main.xml文件的内容:

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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
  7. android:id="@+id/image"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:src="@drawable/my_clip"
  11. />
  12. </LinearLayout>

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

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <clipxmlns: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. packagecn.jason.drawable;
  2. importjava.util.Timer;
  3. importjava.util.TimerTask;
  4. importandroid.app.Activity;
  5. importandroid.os.Bundle;
  6. importandroid.os.Handler;
  7. importandroid.os.Message;
  8. importandroid.widget.ImageView;
  9. publicclassClipDrawableextendsActivity{
  10. /**Calledwhentheactivityisfirstcreated.*/
  11. @Override
  12. publicvoidonCreate(BundlesavedInstanceState){
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. ImageViewimageView=(ImageView)findViewById(R.id.image);
  16. finalandroid.graphics.drawable.ClipDrawabledrawable=(android.graphics.drawable.ClipDrawable)imageView.getDrawable();
  17. finalHandlerhandler=newHandler(){
  18. @Override
  19. publicvoidhandleMessage(Messagemsg){
  20. if(msg.what==0x1233){
  21. drawable.setLevel(drawable.getLevel()+200);
  22. }
  23. }
  24. };
  25. finalTimertimer=newTimer();
  26. timer.schedule(newTimerTask(){
  27. @Override
  28. publicvoidrun(){
  29. Messagemsg=newMessage();
  30. msg.what=0x1233;
  31. handler.sendMessage(msg);
  32. if(drawable.getLevel()>=10000){
  33. timer.cancel();
  34. }
  35. }
  36. },0,300);
  37. }
  38. }
上面的代码定义了一个Handler,如果接收的是本程序的消息,则进行操作。特别注意。定义ClipDrawable的用法是把Drawable进行转换的。

更多相关文章

  1. java第四次实验
  2. 小记初学android过程中遇到的小问题(android 4.4)
  3. android:layout_weight的真实含义
  4. android 127.0.0.1/localhost connection refused 问题的
  5. android评分条RatingBar自定义设置
  6. 从头开始学Android—Android(安卓)Studio(二)
  7. android实现图片平铺效果&WebView多点触控实现缩放
  8. android 自定义适配器Adapter基类BaseAdapter
  9. 【Android】自定义dialog的布局样式

随机推荐

  1. Android中的单任务模式
  2. Kotlin 写 Android 单元测试(四),Robolectri
  3. android 桌面小工具(Widget)开发教程
  4. Android夸进程通信机制九:AIDL深入了解
  5. Android 智能手机开发概述
  6. android Intent常见应用
  7. android 条码识别软件开发全解析
  8. Android(安卓)PopupWindow 的显示位置
  9. android shape 用法整理
  10. Android View 绘制流程之三:draw绘制