android学习笔记之使用ClipDrawable

来源:互联网 发布:免费装修设计软件 编辑:程序博客网 时间:2024/06/14 12:03

ClipDrawable代表从其它位图上截取一个“图片片段”。在XML文件中使用<clip.../>元素定义ClipDrawable对象,可指定如下三个属性:

  • android:drawable:指定截取的源Drawable对象
  • android:clipOrientation:指定截取的方向,可设置为水平截取或垂直截取
  • android:gravity:指定截取时的对齐方式

       使用ClipDrawable对象时可以调用setLevel(int level)方法来设置截取的区域大小,当level为0时,截取的图片片段为空;当level为10000时,截取整张图片。

       通过以上说明,我们发现,可以使用ClipDrawable的这种性质控制截取图片的区域大小,让程序不断调用setLevel方法并改变level的值,达到让图片慢慢展开的效果。

 

首先,我们定义一个ClipDrawable对象:

<?xml version="1.0" encoding="UTF-8"?><clip    xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@drawable/muller"    android:clipOrientation="horizontal"    android:gravity="center" >    </clip>

 

接下来,简单设置下布局文件,使得图片能够显示在屏幕上(注意ImageView的src属性的选择):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" >    <ImageView        android:id="@+id/view"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:src="@drawable/clip" /></LinearLayout>

下面在主程序中设置一个定时器来改变level值:

public class MainActivity extends Activity{    private ImageView view=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_main);this.view=(ImageView)super.findViewById(R.id.view);//获取图片所显示的ClipDrawable对象final ClipDrawable drawable=(ClipDrawable) view.getDrawable();final Handler handler=new Handler(){@Overridepublic void handleMessage(Message msg) {//如果消息是本程序发送的if(msg.what==0x123){//修改ClipDrawable的level值drawable.setLevel(drawable.getLevel()+200);}}};final Timer timer=new Timer();timer.schedule(new TimerTask(){@Overridepublic void run() {Message msg=new Message();msg.what=0x123;//发送消息,通知应用修改ClipDrawable的level的值handler.sendMessage(msg);//取消定时器if(drawable.getLevel()>=10000){timer.cancel();}}}, 0,300);}}


0 0
原创粉丝点击