首页 > 编程知识 正文

drawable什么意思,to draw和draw的区别

时间:2023-05-03 06:57:13 阅读:109472 作者:2643

在Android中,Drawable接口及其子类的用法2009-01-21 21:48:28|分类:默认分类|订阅

adrawableisageneralabstractionfor ' somethingthatcanbedrawn.' most often

youwilldealwithdrawableasthetypeofresourceretrievedfordrawingthingsto

the screen; thedrawableclassprovidesagenericapifordealingwithanunderlying

visualresourcethatmaytakeavarietyofforms.unlike a view,adrawabledoesnothaveanyfacilitytoreceiveeventsorotherwiseinteractwith

Drawable是一个可绘制的对象,可以使用它在屏幕上绘制内容,或者直接获取现有的图等。

Drawable d=this.getResources ().getdrawable ) ) r.Drawable.a1 );

//this是指Activity,R.drawable.a1是resdrawable文件夹内的名称为a1的图。

几种常见的Drawable对象类型:

Bitmap: the simplest Drawable,a PNG or JPEG image。

//一般用于jpg和png地图的处理

nine patch : anextensiontothepngformatallowsittospecifyinformationabout

howtostretchitandplacethingsinsideofit。

shape : containssimpledrawingcommandsinsteadofarawbitmap,allowing it to

some cases在资源中。

Layers: a compound drawable,whichdrawsmultipleunderlyingdrawablesontopofeachother。

layerdrawable(drawable[]array;

用于以分层方式访问多个Drawable,可以通过getDrawable(intindex )获取任意一个Drawable,并支持set layer (int ) )。

States : acompounddrawablethatselectsoneofasetofdrawablesbasedonitsstate。

addstate(int[]Stateset,Drawable drawable;

anarrayofresourceidstoassociatewiththeimage.switchtothisimagebycallingsetstate (

通过根据状态访问不同的Drawable,指定状态的id值,例如可以获得焦点或失去焦点时的不同的图像

例如,addstate (new int [ ] { r.attr.state _ focused,R.attr.state_pressed}, }; Setstate(int ) );

levels : acompounddrawablethatselectsoneofasetofdrawablesbasedonitslevel。

addlevel(intlow,int high,Drawable drawable )。

可以指定在不同级别显示不同的图

例如,add level (1,3, //从等级1到等级3时表示相应的图,与setlevel(int )相对应

scale : acompounddrawablewithasinglechilddrawable,whose overall size is

已修改的基本级别。

scaledrawable(drawabledrawable,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加载图片 博客分类: android Android

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

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

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代码   int idFlag = getResources().getIdentifier(           getPackageName() + ":drawable/flag",            null, null);     // 或是     int idFlag = getResources().getIdentifier(           "flag", "drawable", getPackageName());       Java代码   var Drawable[] dw = new Drawable[10];         for (int i = 1; i <= 10; i++) {       int id = getResources().getIdentifier(                         "flag" + i,                          "drawable", getPackageName());       dw[i-1] = getResources().getDrawable(id);     }    用反射发 可以得到 所有的资源 Java代码   private void        _DumpAllResourceIDs(Class<?> classType)          throws IllegalArgumentException {       Field[] fIDs = classType.getFields();                    try {         for (int i = 0; i < fIDs.length; i++) {           Field fld = fIDs[i];           int nID = fld.getInt(null);           Log.d("dbg",             classType.getSimpleName() + " " +              i + ": " +              fld.getName() + "=" +             nID);         }       } catch (Exception e) {         throw new IllegalArgumentException();       }     }       Java代码   import java.lang.reflect.Field;     ...       _DumpAllResourceIDs(R.layout.class);       _DumpAllResourceIDs(R.drawable.class);      结果 Java代码   R$layout 0: main=2130903040    R$layout 1: small_spinner_dropdown_item=2130903041    R$drawable 0: icon=2130837504   
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


反射加载图片 分类: Android 学习笔记 2011-06-24 17:45 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",  null,  null ); 
// or 
int resID = getResources ( ). getIdentifier ( "bug",  "drawable",  "org.anddev.android.testproject" ); 第一个参数其实 full_package:type/filename_without_ending是这种格式 然后其他的可以为null Java代码   view plain int idFlag = getResources().getIdentifier(             getPackageName() + ":drawable/flag",              null, null);       // 或是       int idFlag = getResources().getIdentifier(             "flag", "drawable", getPackageName());           Java代码   view plain var Drawable[] dw = new Drawable[10];             for (int i = 1; i <= 10; i++) {         int id = getResources().getIdentifier(                           "flag" + i,                            "drawable", getPackageName());         dw[i-1] = getResources().getDrawable(id);       }        用反射发 可以得到 所有的资源 Java代码   view plain private void          _DumpAllResourceIDs(Class<?> classType)            throws IllegalArgumentException {         Field[] fIDs = classType.getFields();                        try {           for (int i = 0; i < fIDs.length; i++) {             Field fld = fIDs[i];             int nID = fld.getInt(null);             Log.d("dbg",               classType.getSimpleName() + " " +                i + ": " +                fld.getName() + "=" +               nID);           }         } catch (Exception e) {           throw new IllegalArgumentException();         }       }           Java代码   view plain import java.lang.reflect.Field;       ...         _DumpAllResourceIDs(R.layout.class);         _DumpAllResourceIDs(R.drawable.class);           结果 Java代码   view plain R$layout 0: main=2130903040      R$layout 1: small_spinner_dropdown_item=2130903041      R$drawable 0: icon=2130837504        

 



版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。