Drawable的用法

Android中,Drawable接口及其子类的使用方法  

2009-01-21 21:48:28|  分类: 默认分类 |字号 订阅


A Drawable is a general abstraction for "something that can be drawn." Most often
you will deal with Drawable as the type of resource retrieved for drawing things to
the screen; the Drawable class provides a generic API for dealing with an underlying
visual resource that may take a variety of forms. Unlike a View, a Drawable does not have any facility to receive events or otherwise interact with the user.
Drawable是一个可画对象,可以用它在屏膜上画内容,也可以直接取得已的图等。
Drawable d = this.getResources().getDrawable(R.drawable.a1);
// this指代Activity, R.drawable.a1是在\res\drawable文件夹中的名称为a1的图。

常见的几种Drawable对象类型:
Bitmap: the simplest Drawable, a PNG or JPEG image.
// 一般用于处理jpg和png图

Nine Patch: an extension to the PNG format allows it to specify information about
how to stretch it and place things inside of it.

Shape: contains simple drawing commands instead of a raw bitmap, allowing it to
resize better in some cases.

Layers: a compound drawable, which draws multiple underlying drawables on top of each other.
LayerDrawable(Drawable[] array);
用于图层方式存取多个Drawable,可以用getDrawable(int index)取得其中一个Drawable,对应setLayer(int);

States: a compound drawable that selects one of a set of drawables based on its state.
addState(int[] stateSet, Drawable drawable);
An array of resource Ids to associate with the image. Switch to this image by calling setState().
为不同的状态存取不同的Drawable,通过指定状态的id值,可以取得如获得焦点,失去焦点等时的不同图像
如:addState( new int[]{R.attr.state_focused, R.attr.state_pressed}, ... ); 对应setState(int[]);

Levels: a compound drawable that selects one of a set of drawables based on its level.
addLevel(int low, int high, Drawable drawable)
可以指定在不同的级别中显示不同的图
如:addLevel(1, 3, ...); // 在第1到3级的时候显示相应的图,对应setLevel(int)


Scale: a compound drawable with a single child drawable, whose overall size is
modified based on the current level.
ScaleDrawable(Drawable drawable, int gravity, float scaleWidth, float scaleHeight)
// 这是一个可以缩放的drawable,可以将图缩放到指定的大小


例:
Drawable[] array = new Drawable[] {
this.getResources().getDrawable(R.drawable.a1),
this.getResources().getDrawable(R.drawable.a2),
this.getResources().getDrawable(R.drawable.a3),
this.getResources().getDrawable(R.drawable.a4)
};
LayerDrawable ld = new LayerDrawable( array );

ImageButton imgBtn = new ImageButton( this );
imgBtn.setImageDrawable( ld.getDrawable(2) );

// 显示a3这个图



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


判断资源存在与否如判断一个drawable的图片存在及通过id加载图片

其实这个问题在我的以前的琐碎里已经记录过了

现在拿出来 免得到时候需要的人在找

int i=  getResources().getIdentifier("icon", "drawable", getPackageName()) ;
      if(i>0)
      {Log.i("aa","aa");}
      else
      {Log.i("vbv","aa");}

 

或者

 int resID = getResources().getIdentifier("org.loveandroid.androidtest:drawable/gallery_photo_1",null,null);

 

int resID = getResources ( ). getIdentifier ( "org.anddev.android.testproject:drawable/bug", null, null );
// or
int resID = getResources ( ). getIdentifier ( "bug", "drawable", "org.anddev.android.testproject" );
第一个参数其实
full_package:type/filename_without_ending是这种格式 然后其他的可以为null
Java代码   收藏代码
  1. int idFlag = getResources().getIdentifier(     
  2.       getPackageName() + ":drawable/flag",      
  3.       nullnull);     
  4. // 或是     
  5. int idFlag = getResources().getIdentifier(     
  6.       "flag""drawable", getPackageName());    
 
Java代码   收藏代码
  1. var Drawable[] dw = new Drawable[10];     
  2.     
  3. for (int i = 1; i <= 10; i++) {     
  4.   int id = getResources().getIdentifier(     
  5.                     "flag" + i,      
  6.                     "drawable", getPackageName());     
  7.   dw[i-1] = getResources().getDrawable(id);     
  8. }  
 用反射发 可以得到 所有的资源
Java代码   收藏代码
  1. private void      
  2.   _DumpAllResourceIDs(Class<?> classType)      
  3.     throws IllegalArgumentException {     
  4.   Field[] fIDs = classType.getFields();     
  5.              
  6.   try {     
  7.     for (int i = 0; i < fIDs.length; i++) {     
  8.       Field fld = fIDs[i];     
  9.       int nID = fld.getInt(null);     
  10.       Log.d("dbg",     
  11.         classType.getSimpleName() + " " +      
  12.         i + ": " +      
  13.         fld.getName() + "=" +     
  14.         nID);     
  15.     }     
  16.   } catch (Exception e) {     
  17.     throw new IllegalArgumentException();     
  18.   }     
  19. }    
 
Java代码   收藏代码
  1. import java.lang.reflect.Field;     
  2. ...     
  3.   _DumpAllResourceIDs(R.layout.class);     
  4.   _DumpAllResourceIDs(R.drawable.class);    
 结果
Java代码   收藏代码
  1. R$layout 0: main=2130903040    
  2. R$layout 1: small_spinner_dropdown_item=2130903041    
  3. R$drawable 0: icon=2130837504   
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


反射加载图片

分类: Android 学习笔记 54人阅读 评论(0) 收藏 举报

转:http://wang-peng1.iteye.com/blog/566362

其实这个问题在我的以前的琐碎里已经记录过了

现在拿出来 免得到时候需要的人在找

int i=  getResources().getIdentifier("icon", "drawable", getPackageName()) ;
      if(i>0)
      {Log.i("aa","aa");}
      else
      {Log.i("vbv","aa");}

 

或者

 int resID = getResources().getIdentifier("org.loveandroid.androidtest:drawable/gallery_photo_1",null,null);

 

int resID = getResources ( ). getIdentifier ( "org.anddev.android.testproject:drawable/bug"nullnull )
// or 
int resID = getResources ( ). getIdentifier ( "bug""drawable""org.anddev.android.testproject" );
第一个参数其实
full_package:type/filename_without_ending是这种格式 然后其他的可以为null
Java代码   收藏代码
  1. int idFlag = getResources().getIdentifier(       
  2.       getPackageName() + ":drawable/flag",        
  3.       nullnull);       
  4. // 或是       
  5. int idFlag = getResources().getIdentifier(       
  6.       "flag""drawable", getPackageName());      
 
 
Java代码   收藏代码
  1. var Drawable[] dw = new Drawable[10];       
  2.       
  3. for (int i = 1; i <= 10; i++) {       
  4.   int id = getResources().getIdentifier(       
  5.                     "flag" + i,        
  6.                     "drawable", getPackageName());       
  7.   dw[i-1] = getResources().getDrawable(id);       
  8. }    
 
 用反射发 可以得到 所有的资源
Java代码   收藏代码
  1. private void        
  2.   _DumpAllResourceIDs(Class<?> classType)        
  3.     throws IllegalArgumentException {       
  4.   Field[] fIDs = classType.getFields();       
  5.                
  6.   try {       
  7.     for (int i = 0; i < fIDs.length; i++) {       
  8.       Field fld = fIDs[i];       
  9.       int nID = fld.getInt(null);       
  10.       Log.d("dbg",       
  11.         classType.getSimpleName() + " " +        
  12.         i + ": " +        
  13.         fld.getName() + "=" +       
  14.         nID);       
  15.     }       
  16.   } catch (Exception e) {       
  17.     throw new IllegalArgumentException();       
  18.   }       
  19. }      
 
 
Java代码   收藏代码
  1. import java.lang.reflect.Field;       
  2. ...       
  3.   _DumpAllResourceIDs(R.layout.class);       
  4.   _DumpAllResourceIDs(R.drawable.class);      
  
 结果
Java代码   收藏代码
  1. R$layout 0: main=2130903040      
  2. R$layout 1: small_spinner_dropdown_item=2130903041      
  3. R$drawable 0: icon=2130837504      
 

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值