美文网首页
Android Drawable

Android Drawable

作者: yangweigbh | 来源:发表于2016-12-11 23:46 被阅读52次

    1.BitmapDrawable

    png(最佳),jpg(可以接受)放到drawable目录下,通过R.drawable引用,也可以通过xml来定义bitmapdrawable,可以给drawable增加一些属性

    <?xml version="1.0" encoding="utf-8"?>
    <bitmap
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@[package:]drawable/drawable_resource"
        android:antialias=["true" | "false"]
        android:dither=["true" | "false"]
        android:filter=["true" | "false"]
        android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                          "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                          "center" | "fill" | "clip_vertical" | "clip_horizontal"]
        android:mipMap=["true" | "false"]
        android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />
    

    2.NinePatchDrawable

    后缀为9.png的文件。也可以通过xml定义

    <nine-patch
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@[package:]drawable/drawable_resource"
        android:dither=["true" | "false"] />
    

    3.LayerDrawable

    将不同drawable以分层的方式叠加绘制出来

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list
        xmlns:android="http://schemas.android.com/apk/res/android" >
        <item
            android:drawable="@[package:]drawable/drawable_resource"
            android:id="@[+][package:]id/resource_name"
            android:top="dimension"
            android:right="dimension"
            android:bottom="dimension"
            android:left="dimension" />
    </layer-list>
    

    4.StateListDrawable

    不同状态时展示不同drawable

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android"
        android:constantSize=["true" | "false"]
        android:dither=["true" | "false"]
        android:variablePadding=["true" | "false"] >
        <item
            android:drawable="@[package:]drawable/drawable_resource"
            android:state_pressed=["true" | "false"]
            android:state_focused=["true" | "false"]
            android:state_hovered=["true" | "false"]
            android:state_selected=["true" | "false"]
            android:state_checkable=["true" | "false"]
            android:state_checked=["true" | "false"]
            android:state_enabled=["true" | "false"]
            android:state_activated=["true" | "false"]
            android:state_window_focused=["true" | "false"] />
    </selector>
    

    5.LevelListDrawable

    根据drawable的不同level展示不同图片

    <?xml version="1.0" encoding="utf-8"?>
    <level-list
        xmlns:android="http://schemas.android.com/apk/res/android" >
        <item
            android:drawable="@drawable/drawable_resource"
            android:maxLevel="integer"
            android:minLevel="integer" />
    </level-list>
    

    6.TransitionDrawable

    在两个drawable之间做淡入淡出, 只能有两个item

    <?xml version="1.0" encoding="utf-8"?>
    <transition
    xmlns:android="http://schemas.android.com/apk/res/android" >
        <item
            android:drawable="@[package:]drawable/drawable_resource"
            android:id="@[+][package:]id/resource_name"
            android:top="dimension"
            android:right="dimension"
            android:bottom="dimension"
            android:left="dimension" />
    </transition>
    

    7.InsetDrawable

    在包裹的drawable周围加上衬边

    <?xml version="1.0" encoding="utf-8"?>
    <inset
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawable="@drawable/drawable_resource"
        android:insetTop="dimension"
        android:insetRight="dimension"
        android:insetBottom="dimension"
        android:insetLeft="dimension" />
    

    8.ClipDrawable

    对包裹的drawable进行裁剪

    <?xml version="1.0" encoding="utf-8"?>
    <clip
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawable="@drawable/drawable_resource"
        android:clipOrientation=["horizontal" | "vertical"]
        android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                         "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                         "center" | "fill" | "clip_vertical" | "clip_horizontal"] />
    

    9.ScaleDrawable

    对包裹的drawable进行缩放

    <?xml version="1.0" encoding="utf-8"?>
    <scale
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawable="@drawable/drawable_resource"
        android:scaleGravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                              "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                              "center" | "fill" | "clip_vertical" | "clip_horizontal"]
        android:scaleHeight="percentage"
        android:scaleWidth="percentage" />
    

    10.ShapeDrawable

    通过xml定义shape

    <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape=["rectangle" | "oval" | "line" | "ring"] >
        <corners
            android:radius="integer"
            android:topLeftRadius="integer"
            android:topRightRadius="integer"
            android:bottomLeftRadius="integer"
            android:bottomRightRadius="integer" />
        <gradient
            android:angle="integer"
            android:centerX="float"
            android:centerY="float"
            android:centerColor="integer"
            android:endColor="color"
            android:gradientRadius="integer"
            android:startColor="color"
            android:type=["linear" | "radial" | "sweep"]
            android:useLevel=["true" | "false"] />
        <padding
            android:left="integer"
            android:top="integer"
            android:right="integer"
            android:bottom="integer" />
        <size
            android:width="integer"
            android:height="integer" />
        <solid
            android:color="color" />
        <stroke
            android:width="integer"
            android:color="color"
            android:dashWidth="integer"
            android:dashGap="integer" />
    </shape>
    

    11.AnimationDrawable

    通过定义每帧的drawable来定义的可动画drawable,通过Animatable接口的start方法来进行动画的播放

    <animation-list android:id="@+id/selected" android:oneshot="false">
        <item android:drawable="@drawable/wheel0" android:duration="50" />
        <item android:drawable="@drawable/wheel1" android:duration="50" />
        <item android:drawable="@drawable/wheel2" android:duration="50" />
        <item android:drawable="@drawable/wheel3" android:duration="50" />
        <item android:drawable="@drawable/wheel4" android:duration="50" />
        <item android:drawable="@drawable/wheel5" android:duration="50" />
     </animation-list>
    

    12.ColorDrawable

    通过单一颜色填充的drawable

    <resources>
     <drawable name="red_color">#ff0000</drawable>
     <drawable name="blue_color">#0000ff</drawable>
     <drawable name="green_color">#00ff00</drawable>
    </resources>
    

    13.RippleDrawable (Added in API 21)

    在子item上叠加ripple效果

    <ripple android:color="#ffff0000">//ripple的颜色
       <item android:id="@android:id/mask"
             android:drawable="@android:color/white" /> //ripple的遮罩,可以限制ripple的范围
       <item ..> //添加其他item
     </ripple>
    

    在view的background设置android:background="?android:attr/selectableItemBackground" 或者"?android:attr/selectableItemBackgroundBorderless"也可以起到设置波纹的效果

    14.VectorDrawable (Added in API 21)

    通过xml来定义矢量图形,有<vector>来定义

     <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:height="64dp"
         android:width="64dp"
         android:viewportHeight="600"
         android:viewportWidth="600" >
         <group
             android:name="rotationGroup"
             android:pivotX="300.0"
             android:pivotY="300.0"
             android:rotation="45.0" >
             <path
                 android:name="v"
                 android:fillColor="#000000"
                 android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
         </group>
     </vector>
    

    15.AnimatedVectorDrawable (Added in API 21)

    通过Object Animator来对VectorDrawable的属性做动画,由<animated-vector>定义

    <animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:drawable="@drawable/vectordrawable" > <- vector drawble
         <target
             android:name="rotationGroup" <- 属性
             android:animation="@anim/rotation" /> <- object animator的定义文件
         <target
             android:name="v"
             android:animation="@anim/path_morph" />
     </animated-vector>
    

    16.AnimatedStateListDrawable (Added in API 21)

    StateListDrawble的派生类,state之间切换时可以定义动画

    <animated-selector
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!-- provide a different drawable for each state-->
        <item android:id="@+id/pressed" android:drawable="@drawable/drawableP"
            android:state_pressed="true"/>
        <item android:id="@+id/focused" android:drawable="@drawable/drawableF"
            android:state_focused="true"/>
        <item android:id="@id/default"
            android:drawable="@drawable/drawableD"/>
    
        <!-- specify a transition -->
        <transition android:fromId="@+id/default" android:toId="@+id/pressed">
            <animation-list>
                <item android:duration="15" android:drawable="@drawable/dt1"/>
                <item android:duration="15" android:drawable="@drawable/dt2"/>
                ...
            </animation-list>
        </transition>
        ...
    </animated-selector>
    

    引用

    1.https://developer.android.com/guide/topics/resources/drawable-resource.html#LevelList

    相关文章

      网友评论

          本文标题:Android Drawable

          本文链接:https://www.haomeiwen.com/subject/wagpmttx.html