Android Drawable 系列——ClipDrawable

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

main.xml文件的内容:

  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/image"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:src="@drawable/my_clip"  
  12.         />  
  13.           
  14. </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",调用了这个配置文件。这个配置文件的内容是:

  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>  
<?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的代码:

  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 imageView = (ImageView) findViewById(R.id.image);  
  19.           
  20.         final android.graphics.drawable.ClipDrawable  drawable = (android.graphics.drawable.ClipDrawable) imageView.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. }  
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进行转换的。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值