分享

android中的drawable资源

 米筱厘 2016-03-20

  安卓中的drawable资源位我们在开发过程中提供了很大的便利之处,所以在此要做一下小小的总结。

  一.StateListDrawable

  1,用于组织多个Drawable对象 

  指定selector,每一个状态的Drawable被定义到一个item中 

  2,主要的属性

  android:state_activated 已激活状态

  android:state_active 是否处于激活

  android:state_checkable 是否处于勾选

  android:state_checked 已勾选

  android:state_enabled 是否可用

  android:state_first 是否处于开始

  android:state_focused 是否已得焦点

  android:state_last 是否处于结束

  android:state_middle 是否处于中间

  android:state_pressed 是否处于已按下

  android:state_selected 是否处于已选中状态

  android:state_window_focused 是否窗口已获得焦点

  在drawable中的xml文件

SosoImgView Code  

  二.LayerDrawable

  使用LayoutDrawable要在drawable下的layer-list标签中使用

  drawable中配置的文件,seek.xml 

  1  
  2  
  3
  4      
  5     <>
  6         android:id='@android:id/background' 
  7         android:drawable='@drawable/lbs_popup_female'/> 
  8
  9      
10 
11     <>
12         android:id='@android:id/progress' 
13         android:drawable='@drawable/lbs_popup_female_selected'/> 
14 
15  

  seek_image.xml

  1  
  2  
  3
  4  
  5      
  6         <>
  7             android:gravity='center' 
  8             android:src='@drawable/ic_launcher' /> 
  9     
 
10 
11      
12     <>
13         android:left='20dp' 
14         android:top='20dp'> 
15         <>
16             android:gravity='center' 
17             android:src='@drawable/ic_launcher' /> 
18      
19 
20          
21     <>
22         android:left='40dp' 
23         android:top='40dp'> 
24         <>
25             android:gravity='center' 
26             android:src='@drawable/ic_launcher' /> 
27      
28 
29  

  seek_image.xml中没有设置android:gravity='center'的效果与配置了的效果对比

soso_tc_slider_img

soso_tc_slider_img

  layout中xml文件

SosoImgView Code  

  三.ShapeDrawable

  1,根标签使用sharp,并且在根标签中药定义sharp的类型,

rectangle---矩阵,这也是默认的sharp
oval--------椭圆
line --------一条水平的直线,这个sharp必须使用元素来定义这个线的宽度
ring--------圆环

  实例:<>http://schemas./apk/res/android
  android:shape='rectangle' > 

  2,sharp中的属性

<>
  android:radius='integer' 
  android:topLeftRadius='integer' 
  android:topRightRadius='integer' 
  android:bottomLeftRadius='integer' 
  android:bottomRightRadius='integer' /> 
  <>
  android:angle='integer' 
  android:centerX='integer' 
  android:centerY='integer' 
  android:centerColor='integer' 
  android:endColor='color' 
  android:gradientRadius='integer' 
  android:startColor='color' 
  android:type=['linear' | 'radial' | 'sweep'] 
  android:useLevel=['true' | 'false'] /> 
  <>
  android:left='integer' 
  android:top='integer' 
  android:right='integer' 
  android:bottom='integer' /> 
  <>
  android:width='integer' 
  android:height='integer' /> 
  <>
  android:color='color' /> 
  <>
  android:width='integer' 
  android:color='color' 
  android:dashWidth='integer' 
  android:dashGap='integer' /> 
corners是为Shape创建一个圆角,只有shape是rectangle时候才使用。
gradient是设置渐变效果的
padding是内容与视图边界的距离
size是sharp的大小
solid是用来填充sharp的纯色
stroke是这个shape使用的笔画,当android:shape='line'的时候,必须设置改元素。

  drawabl文件中,sharp1.xml 

  1  
  2 
  3     android:shape='rectangle' > 
  4
  5       
  6     <>
  7         android:angle='45' 
  8         android:endColor='#F69' 
  9         android:startColor='#E83' /> 
10 
11      
12      
13     <>
14         android:left='30dp' 
15         android:right='30dp' 
16         android:top='30dp'/> 
17 
18  

  sharp2.xml

  1  
  2 
  3     android:shape='oval' > 
  4
  5     <>
  6         android:centerColor='#0F0' 
  7         android:endColor='#00F' 
  8         android:startColor='#F00' /> 
  9
10  

  layout中应用

  1 <>
  2             android:layout_width='match_parent' 
  3             android:layout_height='90dp' 
  4             android:background='@drawable/sharp1' 
  5             android:gravity='center' 
  6             android:text='渐变风格1'/> 
  7         <>
  8             android:layout_width='match_parent' 
  9             android:layout_height='90dp' 
10             android:background='@drawable/sharp2' 
11             android:gravity='center' 
12             android:text='渐变风格2'/> 

  效果图:

soso_tc_slider_img

  四,clipDrawable

  drawable中的配置文件 , clip.xml

 

3     android:clipOrientation='horizontal' 
4     android:drawable='@drawable/a' 
5     android:gravity='left' > 

 
8  

  layout文件中引用配置文件

SosoImgView Code  

  java代码:

  1import java.util.Timer; 
  2import java.util.TimerTask; 
  3
  4import android.annotation.SuppressLint; 
  5import android.app.Activity; 
  6import android.graphics.drawable.Drawable; 
  7import android.os.Bundle; 
  8import android.os.Handler; 
  9import android.os.Message; 
10import android.view.Window; 
11import android.widget.ImageView; 
12 
13publicclass MainActivity extends Activity { 
14privatestaticfinalint TAG=1; 
15     @SuppressLint('HandlerLeak') 
16    @Override 
17protectedvoid onCreate(Bundle savedInstanceState) { 
18super.onCreate(savedInstanceState); 
19        requestWindowFeature(Window.FEATURE_NO_TITLE); 
20        setContentView(R.layout.activity_main); 
21         ImageView image=(ImageView) findViewById(R.id.imageView1); 
22////获取图片所显示的ClipDrawble对象 
23final Drawable drawable = image.getDrawable(); 
24 
25//用Handler跟新消息 
26final Handler handler=new Handler(){ 
27            @Override 
28publicvoid handleMessage(Message msg) { 
29//设置drawable的level值 
30                 drawable.setLevel(drawable.getLevel()+200); 
31super.handleMessage(msg); 
32            } 
33        }; 
34 
35final Timer timer=new Timer(); 
36         timer.scheduleAtFixedRate(new TimerTask() { 
37 
38            @Override 
39publicvoid run() { 
40                 Message msg=new Message(); 
41                 msg.what=TAG; 
42                handler.sendMessage(msg); 
43//level的取值为0-10000,大于其值就取消定时器 
44if (drawable.getLevel()>10000) { 
45                    timer.cancel(); 
46                } 
47            } 
48         }, 0, 300); 
49 
50    } 
51 
52 } 

  效果图展示:

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多