Drawable学习

2019-06-11  本文已影响0人  _Rice_

Drawable宽高可通过getIntrinsicWidth()和getIntrinsicHeight()获取其内部宽/高。实际区域大小可以通过getBounds获取,通常和View的尺寸相同

但并不是所有Drawable都有内部宽/高。

Drawable类是抽象类,是所有Drawable的基类。继承关系如下:

image.png

Drawable常用子类

ShapeDrawble

可表示纯色、有渐变效果的基础几何图形(矩形,圆形,线条等)。

用法如下:

<?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:topLeftRaidus="integer"
        android:topRightRaidus="integer"
        android:bottomLeftRaidus="integer"
        android:bottomRightRaidus="integer" />
    <gradient
        android:angle="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:centerColor="color"
        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:图形的形状。

radius:表示shape的四个圆角的角度,只适用于矩形。

gradient:渐变

padding:与四周空白的距离。

solid:纯色填充

stroke:描边

值为0时,表示为实线。值大于0则为虚线。

size:图形的固有大小,非最终大小。

参考:Android XML shape 标签使用详解

StateListDrawable

标签selector,表示Drawable集合,每个item都是Drawable,对应View的一种状态

应用场景:如button

<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="@drawable/image"
        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_accelerated="[true | false]"
        android:state_window_focused="[true | false]"/>

</selector>

系统会从上往下的顺序查找,直到找到一条匹配的,如无法匹配会选择默认item,也就是只有drawable属性的item

BitmapDrawable

表示一张图片,设置更多效果

<?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:mipmap=["true" | "false"]
    android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                      "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                      "center" | "fill" | "clip_vertical" | "clip_horizontal"]
    android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] >

</bitmap>
可选性 说明
top 保持原有大小,图片至于容器的顶部
bottom 保持原有大小,图片至于容器的底部
left 保持原有大小,图片至于容器的左部
right 保持原有大小,图片至于容器的右部
center_vertical 保持原有大小,图片垂直居中
fill_vertical 图片垂直方向填充容器
center_horizontal 保持原有大小,图片水平居中
fill_horizontal 图片水平方向填充容器
center 保持原有大小,图片同时水平和垂直居中
fill 默认值,同时水平和垂直拉伸
clip_vertical 附加项,表示水平方向的裁剪
clip_horizontal 附加项,表示水平方向的裁剪

disabled 关闭平铺模式,默认
clamp 大小不变,像素在四周扩散
repeat 常见的水平和垂直方向的平铺
mirror 水平和垂直方向的镜面投影

image.png image.png image.png image.png

LayerDrawable

表示一种层次化的Drawable集合,通过将不同的Drawable放置在不同的层上面从而达到一种叠加后的效果。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/xxx_id"
        android:drawable="@drawable/drawable_id"
        android:top="dimension"
        android:bottom="dimension"
        android:left="dimension"
        android:right="dimension"/>
</layer-list>

一个layer-list可以包含多个item

还可以结合ShapeDrawable使用,例如

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle">
            <solid android:color="#0ac39e"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#0ac39e"/>
        </shape>
    </item>
    <item android:bottom="6dp">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff"/>
        </shape>
    </item>
    <item
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff"/>
        </shape>
    </item>
</layer-list>

效果图:


image.png

TransitionDrawable

实现两个Drawable之间的淡入淡出效果

常用属性与LayerDrawable相同

实例

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/colorPrimaryDark"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/colorAccent"/>
        </shape>
    </item>
</transition>

//通过startTransition和reverseTransition实现淡入淡出效果
TransitionDrawable background = (TransitionDrawable) mDrawableTransitionTv.getBackground();
        background.reverseTransition(1000);

LevelListDrawable

  1. 概念:表示一个Drawable集合,集合中的每个Drawable都有一个等级的概念。
  2. 功能:可根据不同等级切换具体的drawable
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/xxx_id"
        android:drawable="@drawable/drawable_id"
        android:maxLevel="integer"
        android:minLevel="integer"/>
</level-list>

使用方法:

InsetDrawable

概念:表示一个drawable根据指定的距离嵌入到另外一个drawable内部
应用场景:当控件需要的背景比实际边框小的时候

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/id"
    android:insetLeft="dimension"
    android:insetTop="dimension"
    android:insetRight="dimension"
    android:insetBottom="dimension"
    android:visible="[true | false]"/>

实例

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="15dp"
    android:insetTop="20dp"
    android:insetRight="15dp"
    android:insetBottom="5dp"
    android:visible="true">
    <shape android:shape="rectangle">
        <solid android:color="@color/colorAccent"/>
    </shape>
</inset>

效果:

image.png

ScaleDrawable

功能:放缩一定比例

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

使用方法:设置控件背景,调用setLevel()方法控制Drawable等级。

ClipDrawable

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

使用方法:设置控件背景,调用setLevel()方法控制裁剪区域大小。

参考:要点提炼|开发艺术之Drawable,Android开发艺术探索(任玉刚)

上一篇 下一篇

猜你喜欢

热点阅读