zgq105 / blog

2 stars 0 forks source link

Android中Drawable总结 #40

Open zgq105 opened 5 years ago

zgq105 commented 5 years ago

image

1. 什么是Drawable?作用?

Drawable是抽象类,表示Android可以绘制的对象。通过各种绘制对象将Drawable资源绘制到屏幕上。

2. Drawable分类

2.1 BitmapDrawable

BitmapDrawable可以来源于文件路径、输入流、XML或者Bitmap对象,以xml为例,实现代码如下: //定义bitmap资源

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:alpha="0.5"
    android:gravity="fill"
    android:src="@drawable/close" />

//使用bitmap资源 imageView.setImageDrawable(getResources().getDrawable(R.drawable.bitmap01));

2.2 ColorDrawable

ColorDrawable主要来源XML中使用定义的信息 ,示例代码如下: //定义资源

<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
</resources>

//使用资源

TextView textView = new TextView(this);
textView.setTextColor(getResources().getColor(R.color.colorAccent));

2.3 NinePatchDrawable

NinePatchDrawable是一种可伸缩的图片资源,具体参考:链接

2.4 VectorDrawable

VectorDrawable是Android 5.0引入的API,指矢量绘制资源,即矢量图资源。

可缩放矢量图形(SVG):是一种基于可扩展标记语言(XML),用于描述二维矢量图形的图形格式。

//定义VectorDrawable资源:

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="32dp"
    android:height="32dp"
    android:viewportWidth="1024"
    android:viewportHeight="1024">

    <path
        android:fillColor="#1296db"
        android:pathData="M714.112 464H424.512l121.728-121.792a48.64 48.64 0 0 0 0.96-68.736 48.64 48.64 0 0 0-68.8 0.832L275.84 476.928c-0.448 0.384-1.024 0.512-1.472 0.96a47.36 47.36 0 0 0-13.696 34.624c0 12.224 4.48 24.448 13.76 33.664 0.384 0.384 0.896 0.512 1.28 0.832l202.688 202.688c19.264 19.2 50.048 19.712 68.8 0.96a48.64 48.64 0 0 0-0.96-68.736L424.448 560h289.664c27.2 0 49.28-21.504 49.28-48s-22.08-48-49.28-48z"></path>
    <path
        android:fillColor="#1296db"
        android:pathData="M512 32A479.936 479.936 0 0 0 32 512c0 265.152 214.848 480 480 480s480-214.848 480-480S777.152 32 512 32zM512 896A384 384 0 1 1 512 128a384 384 0 0 1 0 768z"></path>

</vector>

//使用VectorDrawable资源:

<ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/vector1"
        android:id="@+id/iv"
        />

//创建SVG图片资源的方式:

2.5 ShapeDrawable

ShapeDrawable表示形状可绘制对象,比如绘制矩形(rectangle)、椭圆(oval)、水平线(line)、环形(ring)。以绘制矩形为例: //定义资源:

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

    <gradient
        android:angle="45"
        android:endColor="#80FF00FF"
        android:startColor="#FFFF0000" />
    <padding
        android:bottom="7dp"
        android:left="7dp"
        android:right="7dp"
        android:top="7dp" />
    <corners android:radius="8dp" />
</shape>

//使用资源

<ImageView
        android:id="@+id/iv"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/shape1" />

2.6 LayerDrawable

LayerDrawable是管理其他可绘制对象阵列的可绘制对象。列表中的每个可绘制对象按照列表的顺序绘制,列表中的最后一个可绘制对象绘于顶部。(逐层绘制,类似前端z-index) //定义资源:

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

    <item>
        <bitmap android:src="@drawable/d6"
            android:gravity="center" />
    </item>
    <item android:top="50dp" android:left="50dp">
        <bitmap android:src="@drawable/d6"
            android:gravity="center" />
    </item>
    <item android:top="100dp" android:left="100dp">
        <bitmap android:src="@drawable/d6"
            android:gravity="center" />
    </item>
</layer-list>

//使用资源

<ImageView
        android:id="@+id/iv"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/layerdraw1" />

//效果 image

2.7 GradientDrawable

GradientDrawable是一种颜色渐变资源,可以用于设置按钮、背景等渐变效果。可以在xml中定义渐变效果。 //定义资源

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="45"
        android:centerX="50%"
        android:centerY="30%"
        android:endColor="#0C73DA"
        android:startColor="#aabbcc" />
</shape>

//使用资源

<ImageView
        android:id="@+id/iv"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/gradient1" />

//效果 image

2.8 AdaptiveIconDrawable

AdaptiveIconDrawable是Android API 26新增的类,表示可伸缩的绘制资源。可以创建在xml的中创建。 //定义资源

<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <foreground android:drawable="@drawable/ic_launcher_foreground">
    </foreground>
    <background android:drawable="@drawable/ic_launcher_foreground">
    </background>
</adaptive-icon>

//使用资源

<ImageView
        android:id="@+id/iv"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/adaptive1" />

2.9 AnimatedImageDrawable

AnimatedImageDrawable表示动图绘制资源,比如gif图资源的加载。示例代码如下:

 ImageDecoder.Source source = ImageDecoder.createSource(getResources(), R.drawable.gif1);
                try {
                    AnimatedImageDrawable drawable = (AnimatedImageDrawable) ImageDecoder.decodeDrawable(source);
                    iv.setImageDrawable(drawable);
                    drawable.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }

2.10 AnimatedVectorDrawable

AnimatedVectorDrawable是矢量绘制动画资源,其动画属性可以使用ObjectAnimator或者AnimatorSet定义。 //示例1:

<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>

//示例2:

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
     android:drawable="@drawable/vectordrawable" >
     <target
         android:name="rotationGroup"
         android:animation="@animator/rotation" />
     <target
         android:name="v"
         android:animation="@animator/path_morph" />
 </animated-vector>

//示例3:

<set xmlns:android="http://schemas.android.com/apk/res/android">
     <objectAnimator
         android:duration="3000"
         android:propertyName="pathData"
         android:valueFrom="M300,70 l 0,-70 70,70 0,0 -70,70z"
         android:valueTo="M300,70 l 0,-70 70,0  0,140 -70,0 z"
         android:valueType="pathType"/>
 </set>

2.11 ColorStateListDrawable

ColorStateListDrawable表示颜色状态列表绘制对象,主要用于设置View不同状态的显示颜色。 //定义xml资源

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorPrimary" android:state_pressed="true" />
    <item android:drawable="@color/colorAccent" android:state_pressed="false" />
</selector>

注意:android:drawable="@color/colorPrimary"不能写成android:drawable=“#aabbcc”,必须@color方式指定颜色。

//使用资源

<ImageView
        android:id="@+id/iv"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/s1" />

2.12 DrawableContainer

DrawableContainer是一个绘制对象Drawable的帮助类,可以选择哪个Drawable对象用于绘制。

2.13 DrawableWrapper

DrawableWrapper是包装类,是一个抽象类。其子类包括ClipDrawable, InsetDrawable, RotateDrawable, ScaleDrawable。

2.14 PictureDrawable

PictureDrawable表示图片绘制对象。

3. 小结

Drawable是安卓中的可绘制资源的抽象,在实际开发中根据需求选择具体的绘制对象,应用在View上。一般在开发过程中,将drawable资源定义在res/drawable/的xml中。