zoukankan      html  css  js  c++  java
  • Android XML Drawable

    一、简介

      Android把任何可绘制在屏幕上的图形图像都称为drawable。drawable是一种抽象的图形,一个继承了Drawable类的子类,或者是一张位图图像。

    二、示例

       1. 下面通过一些例子感受一下,利用ShapeDrawable创建彩色图形,既然XML Drewable与特定的像素密度无关,因此无需考虑特定像素密度的目录。Drawable的XML文件默认放于res/drawable目录下,利用Drawable和Shape创建一个圆角按钮。创建button_shape_normal.xml文件。

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <shape
     3     xmlns:android="http://schemas.android.com/apk/res/android"
     4     android:shape="rectangle">
     5 
     6     <corners android:radius="10dp" />
     7     <gradient
     8         android:angle="90"
     9         android:endColor="#cccccc"
    10         android:startColor="#acacac" />
    11 
    12 </shape>

      在style.xml中,修改如下:

    1 <style name="Remote_control_btn_style">
    2         <item name="android:layout_width">0dp</item>
    3         <item name="android:layout_height">match_parent</item>
    4         <item name="android:background">@drawable/button_shape_normal</item>
    5     </style>

      这样一个圆角按钮就完成了。

      2. 下面完成一个按钮在默认状态下的样式,及在点击按钮后的变化样式,再创建一个按钮点击后的Drewable文件button_shape_pressed.xml:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <shape
     3     xmlns:android="http://schemas.android.com/apk/res/android"
     4     android:shape="rectangle">
     5 
     6     <corners android:radius="10dp" />
     7 
     8     <gradient
     9         android:angle="270"
    10         android:endColor="#cccccc"
    11         android:startColor="#acacac" />
    12 
    13 </shape>

      创建交互式的按钮shape(button_shape.xml):

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <selector
     3     xmlns:android="http://schemas.android.com/apk/res/android">
     4 
     5     <item android:drawable="@drawable/button_shape_pressed"
     6           android:state_pressed="true" />
     7     <item android:drawable="@drawable/button_shape_normal"
     8           android:state_pressed="false" />
     9 
    10 </selector>

      再次修改样式文件style.xml,如下所示:

    1 <style name="Remote_control_btn_style">
    2         <item name="android:layout_width">0dp</item>
    3         <item name="android:layout_height">match_parent</item>
    4         <item name="android:background">@drawable/button_shape</item>
    5 </style>

      这样,一个通过xml绘制的按钮就完成了。

    三、总结

      使用两张图片做了个selector (tab_item_home.xml): 

    1 <?xml version="1.0" encoding="utf-8"?>
    2 <selector
    3     xmlns:android="http://schemas.android.com/apk/res/android">
    4     <item android:drawable="@drawable/tab_ic_norm_home" />
    5     <item android:state_pressed="true" android:drawable="@drawable/tab_ic_sel_home" />
    6 </selector>

      为ImageView的src或者background设置样式(itemView.xml),

    1 <ImageView
    2         android:id="@+id/tab_item_icon"
    3         android:layout_width="wrap_content"
    4         android:layout_height="wrap_content"
    5         android:layout_margin="3dp"
    6         android:src="@drawable/tab_item_home"/>

      在运行后,点击总是没有效果,那么,问题出在哪里了呢?

      在做出如下修改,点击效果就恢复正常了,

    1 <?xml version="1.0" encoding="utf-8"?>
    2 <selector
    3     xmlns:android="http://schemas.android.com/apk/res/android">
    4     <item android:state_pressed="true" android:drawable="@drawable/tab_ic_sel_home" />
    5     <item android:state_selected="true" android:drawable="@drawable/tab_ic_sel_home"/>
    6     <item android:drawable="@drawable/tab_ic_norm_home" />
    7 </selector>

      PS: 设置按钮selected状态,在代码中可以动态设置被选中后的持续效果。

      总结:default(默认)/不点击的图片应该放在最下边,才会有点击效果,不然,只会显示default图片。

    四、核心图像资源子类

    • BitmapDrawable对象:用于创建、平铺、拉伸以及对齐位图。
    • ColorDrawable对象:用于用颜色填充指定的其它各类对象。
    • GradientDrawable对象:用于创建并绘制自定义渐变。
    • AnimationDrawable对象:用于创建基于帧的动画。
    • TransitionDrawable对象:用于创建交叉淡入淡出过渡。
    • NinePatchDrawable对象:用于通过定义可拉伸区域来创建可缩放位图。
  • 相关阅读:
    10. Regular Expression Matching
    9. Palindrome Number (考虑负数的情况)
    8. String to Integer (整数的溢出)
    7. Reverse Integer (整数的溢出)
    LeetCode Minimum Size Subarray Sum
    LeetCode Course Schedule II
    Linux 文件缓存 (一)
    LeetCode Tries Prefix Tree
    Linux : lsof 命令
    LeetCode Binary Tree Right Side View
  • 原文地址:https://www.cnblogs.com/naray/p/5318829.html
Copyright © 2011-2022 走看看