ClipDrawable 是一个挺好看的一个图片Drawable,操作起来也算比较简单。下面先把代码贴上来,通过例子进行说明。
main.xml文件的内容:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/image"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:src="@drawable/my_clip"
- />
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/my_clip" /> </LinearLayout>
这里面重点是android:sec="@drawable/my_clip",调用了这个配置文件。这个配置文件的内容是:
- <?xml version="1.0" encoding="UTF-8"?>
- <clip xmlns:android="http://schemas.android.com/apk/res/android"
- android:drawable="@drawable/test"
- android:clipOrientation="horizontal"
- android:gravity="center">
- </clip>
<?xml version="1.0" encoding="UTF-8"?> <clip xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/test" android:clipOrientation="horizontal" android:gravity="center"> </clip>
上面定义了三个属性。属性一是drawable的图片内容。第二个属性是展开的方向,第三个属性大家都经常用,不阐述。
这样,一个clipDrawable的配置文件都已经搞定了。调用一个clip配置文件,展示效果。比较简单。也很容易给项目添加一点感官上面的好处。
下面是Activity的代码:
- package cn.jason.drawable;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.widget.ImageView;
- public class ClipDrawable extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ImageView imageView = (ImageView) findViewById(R.id.image);
- final android.graphics.drawable.ClipDrawable drawable = (android.graphics.drawable.ClipDrawable) imageView.getDrawable();
- final Handler handler = new Handler(){
- @Override
- public void handleMessage(Message msg) {
- if (msg.what == 0x1233) {
- drawable.setLevel(drawable.getLevel() + 200);
- }
- }
- };
- final Timer timer = new Timer();
- timer.schedule(new TimerTask() {
- @Override
- public void run() {
- Message msg = new Message();
- msg.what= 0x1233;
- handler.sendMessage(msg);
- if (drawable.getLevel() >=10000) {
- timer.cancel();
- }
- }
- }, 0,300);
- }
- }
package cn.jason.drawable; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.ImageView; public class ClipDrawable extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView imageView = (ImageView) findViewById(R.id.image); final android.graphics.drawable.ClipDrawable drawable = (android.graphics.drawable.ClipDrawable) imageView.getDrawable(); final Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { if (msg.what == 0x1233) { drawable.setLevel(drawable.getLevel() + 200); } } }; final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { Message msg = new Message(); msg.what= 0x1233; handler.sendMessage(msg); if (drawable.getLevel() >=10000) { timer.cancel(); } } }, 0,300); } }
上面的代码定义了一个Handler,如果接收的是本程序的消息,则进行操作。特别注意。定义ClipDrawable的用法是把Drawable进行转换的。