自定义控件Android 技术文档收集

高级UI<第二十六篇>:Google自带Drawable详解

2019-12-15  本文已影响0人  NoBugException

Drawable是高级UI必须了解的一个环节,在自定义View中,Drawable可以起到很重要的辅助作用。本文主要讲解的API有:BitmapDrawableShapeDrawableGradientDrawableLayerDrawableStateListDrawableLevelListDrawableTransitionDrawableColorDrawableScaleDrawableInsetDrawableClipDrawablePictureDrawableRotateDrawableRoundedBitmapDrawableRippleDrawablePaintDrawableNinePatchDrawableDrawerArrowDrawableCircularProgressDrawableAdaptiveIconDrawableVectorDrawableAnimationDrawableAnimatedStateListDrawableAnimatedVectorDrawableAnimatedImageDrawable

什么是Drawable?
Drawable的优点
Drawable内部宽高的获取
各种Drawable子类实现

【BitmapDrawable】

BitmapDrawable对应的xml标签是bitmap,表示一张图片。

xml代码如下:

bitmap.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ic_launcher"
    android:tileMode="mirror"
    android:antialias="true"
    android:filter="true"
    android:dither="true">
</bitmap>

其中,属性的意义是:

BitmapDrawable加载到ImageView中

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/bitmap"/>

【ShapeDrawable】【GradientDrawable】

它们对应的标签是shape,通过颜色构建的图形,其中GradientDrawable是梯度渐变的对象。

xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--画图角度-->
    <corners
        android:radius="20dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="20dp"
        android:bottomLeftRadius="20dp"
        android:bottomRightRadius="40dp" />
    <!--描边-->
    <stroke
        android:width="2dp"
        android:color="#000000"
        android:dashWidth="5dp"
        android:dashGap="2dp" />
    <!--渐变效果-->
    <gradient
        android:angle="45"
        android:centerX="0"
        android:centerY="0"
        android:centerColor="#96D669"
        android:endColor="#CE4343"
        android:gradientRadius="0"
        android:startColor="#ffff"
        android:type="linear"
        android:useLevel="true"/>
    <!--表示距离框架的空白-->
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp"/>
    <!--大小-->
    <size android:width="100dp"
        android:height="100dp"/>
</shape>

其中,属性的意义是:

根元素,其android:shape属性定义了这个xml文件定义的形状,可以是retangle(矩形),oval(椭圆),line 和 ring(圆环)。

<corners>表示的是矩形的四个角,只能用在android:shape = "rectangle" 的时候,一般情况下就是用来实现圆角矩形的效果

android:dashWidth:组成虚线的线段的宽度

android:dashGap="2dp":组成虚线的线段之间的间隔

android:angle:渐变的角度,默认为0,其值必须是45的倍数

android:centerX:渐变中心点的横坐标

android:startColor:渐变的起始颜色,还有中间色和结束色

android:gradientRadius:渐变半径

android:type:渐变类别;line(线性渐变)、radial(径向渐变)sweep(扫描渐变)

表示纯色填充

表示包含它的view的空白

Drawable大小

【LayerDrawable】

LayerDrawable对应的标签是layer-list,表示层次化的Drawable集合。常常用于多张图像组合成一个图像。

xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/shapebitmap"
        android:gravity="center"
        android:id="@+id/layer_1"/>

    <item
        android:drawable="@drawable/shapebitmap"
        android:gravity="center"
        android:top="50dp"
        android:left="50dp"
        android:id="@+id/layer_2"/>

    <item
        android:drawable="@drawable/shapebitmap"
        android:gravity="center"
        android:top="100dp"
        android:left="100dp"
        android:id="@+id/layer_3"/>
</layer-list>

效果展示:

图片.png

【StateListDrawable】

StateListDrawable对应的标签是selector,Drawable集合,其中每个Drawable对应一个状态(如:点击时状态、获取焦点时状态、选中时状态)。

xml代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 点击时状态 -->
    <item android:state_pressed="true"  android:drawable= "@drawable/shapebitmap_4" />
    <!-- 选中时状态 -->
    <item android:state_selected="true" android:drawable="@drawable/shapebitmap_3" />
    <!-- 获取焦点时 -->
    <item android:state_focused="true" android:drawable="@drawable/shapebitmap_2" />
    <!-- 默认状态 -->
    <item android:drawable="@drawable/shapebitmap_1" />
</selector>

效果如下:

42.gif

添加到ImageView中

<ImageView
    android:id="@+id/imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/selector_bitmap"
    android:layout_centerInParent="true"/>

【LevelListDrawable】

LevelListDrawable对应的标签是level-list,Drawable集合,其中每个Drawable对应一个等级。

xml代码如下:

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

    <item android:drawable="@drawable/qing" android:maxLevel="1"/>

    <item android:drawable="@drawable/duoyun" android:maxLevel="2" />

    <item android:drawable="@drawable/xiayu" android:maxLevel="3" />

</level-list>

其中,maxLevel属于最大等级,minLevel属于最小等级。

LevelListDrawable设置为ImageView的背景。

<ImageView
    android:id="@+id/imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/level_bitmap"
    android:layout_centerInParent="true"/>

由于默认等级是0,所以显示的默认图像是maxLevel为1的图像。

在代码中将Drawable等级设置为2,如下:

   imageview.getDrawable().setLevel(2);

那么,就会展示maxLevel = 2的图像。

【TransitionDrawable】

图片.png

TransitionDrawableLayerDrawable的子类,它对应的标签是transition,用于实现两个Drawable的切换动画。

xml代码如下:

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

TransitionDrawable设置到ImageView

<ImageView
    android:id="@+id/imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/transition_bitmap"
    android:layout_centerInParent="true"/>

最后在代码中启动动画

 TransitionDrawable drawable=(TransitionDrawable)imageview.getBackground();
 drawable.startTransition(1000);

如果将imageview.getBackground改成imageview.getDrawable,那么android:background="@drawable/transition_bitmap"也要改成android:src="@drawable/transition_bitmap"

效果展示:

43.gif

如图所示,默认显示第一张,当执行startTransition(int durationMillis)方法时,第二层开始显示渐变显示,durationMillis为渐变显示的时长。

另外,图中,显示第二层的图像之后第一层的图像仍然能看到是因为这两张图像都为透明背景。

【ColorDrawable】

ColorDrawable对应的标签是color,仅仅表示颜色。

xml代码如下:

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

ColorDrawable设置到ImageView:

<ImageView
    android:id="@+id/imageview"
    android:layout_width="400dp"
    android:layout_height="400dp"
    android:background="@drawable/color_drawable"
    android:layout_centerInParent="true"/>

【ScaleDrawable】

ScaleDrawable对应的标签是scale,可将指定的Drawable缩放到一定的比例。

xml代码如下:

其中属性的意义是:

图像缩放后最终摆放的方位。

表示Drawable的高的缩放比例,值越大,内部Drawable的高度显示得越小,例如android:scaleHeight=”70%”,那么显示时Drawable的高度只有原来的30%。

表示Drawable的宽的缩放比例,值越大,内部Drawable的宽显示得越小,例如android:scaleWidth=”70%”,那么显示时Drawable的宽度只有原来的30%。

代码实现:

当设置为imageview的src属性

ScaleDrawable drawable = (ScaleDrawable) ivShow.getDrawable();
drawable.setLevel(1);

当设置为imageview的background属性

ScaleDrawable scale= (ScaleDrawable) scaleImage.getBackground();
scale.setLevel(1);

scaleHeightscaleWidth都为30%时,图片展示的大小为原本大小的70%,如图:

图片.png

scaleHeightscaleWidth都为70%时,图片展示的大小为原本大小的30%,如图:

图片.png

除此之外,通过drawable.setLevel(1)设置等级,默认等级为0并且不显示图像,当设置为[0,10000]之间的任意数时显示图像。一般将等级设置为1。

我们来看一下设置等级的源码:

public final boolean setLevel(@IntRange(from=0,to=10000) int level) {
    if (mLevel != level) {
        mLevel = level;
        return onLevelChange(level);
    }
    return false;
}

源码中很明显的告诉我们,这个等级的取值范围为[0,10000],那么这个等级有什么用呢?

假设将一个图像缩小30%,也就是说,从100%缩小到70%,那么可以将这个缩放的过程看成一个由10000张图片组成的动画,当等级设置为1时,就显示缩放到70%后的最后一张,如果设置为5000,那么就显示倒数第5000张。

既然这样,我们可以添加一个进度条来验证个结论,如下:

44.gif

【InsetDrawable】

InsetDrawable对应的标签是inset,可以将其他Drawable内嵌到自己当中。

xml代码如下:

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

它的四个属性比较简单,一张图就可以看懂了,如下:

图片.png

【ClipDrawable】

ClipDrawable对应的标签是clip,根据当前等级来裁剪另一个Drawable。

xml代码很简单,如下:

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

代码实现如下:

<ImageView
    android:id="@+id/imageview"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:background="@drawable/clip_drawable"
    android:layout_centerInParent="true"/>
    final ClipDrawable drawable = (ClipDrawable) imageview.getBackground();
    SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar);
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            int max = seekBar.getMax();
            double scale = (double)progress/(double)max;
            drawable.setLevel((int) (10000*scale));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

图片展示如下:

45.gif

【PictureDrawable】

PictureDrawable无xml表示方法,它的使用方式如下:

    // 包装成为PictureDrawable
    PictureDrawable drawable = new PictureDrawable(mPicture);
    // 设置绘制区域
    drawable.setBounds(0,0,250,mPicture.getHeight());
    // 绘制
    drawable.draw(canvas);

【RotateDrawable】

RotateDrawable对应的标签是rotate。

xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/qing"
    android:fromDegrees="-230"
    android:toDegrees="50"
    android:pivotX="50%"
    android:pivotY="50%" />

其中属性的意义是:

fromDegrees:从多少的角度开始旋转
toDegrees:到多少的角度结束旋转
pivotX:旋转的中心在图片X轴的百分比
pivotY:旋转的中心在图片Y轴的百分比

代码实现如下:

<ImageView
    android:id="@+id/imageview"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:background="@drawable/rotate_drawable"
    android:layout_centerInParent="true"/>
    final RotateDrawable drawable = (RotateDrawable) imageview.getBackground();
    SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar);
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            int max = seekBar.getMax();
            double scale = (double)progress/(double)max;
            drawable.setLevel((int) (10000*scale));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

效果展示如下:

46.gif

【RoundedBitmapDrawable】

RoundedBitmapDrawable没有对应的标签,不能在xml中体现,它的使用方法如下:

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.che);
    RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
    roundedBitmapDrawable.setCornerRadius(100);
    imageview.setImageDrawable(roundedBitmapDrawable);

效果如下:

图片.png

如果需要显示圆形图像,那么代码如下:

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.che);
    RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
    roundedBitmapDrawable.setCircular(true);
    imageview.setImageDrawable(roundedBitmapDrawable);

效果如下:

图片.png

这里图像变形了,那是必然的,因为图片做了拉伸,如果不想变形,那么只能先把图片处理成一个正方形图片再使用RoundedBitmapDrawable生成圆形图片。

【RippleDrawable】

RippleDrawable对应的标签是,它是体现水波纹动画的Drawable。(又叫触摸反馈动画)RippleDrawable只在Android 5.0以上的设备有效。

定义一个按钮:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="按钮"
    android:textSize="30sp"
    android:padding="100dp"
    android:layout_centerInParent="true"/>

现在的Button都默认支持MD风格,其效如下:

336.gif

当将RippleDrawable设置为背景会是什么效果呢?

RippleDrawable的xml标签是ripple,xml代码如下:

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

以上代码是无边框的水波纹动画,将这个作为背景设置到Button控件上,代码如下:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="按钮"
    android:textSize="30sp"
    android:padding="100dp"
    android:background="@drawable/ripple_drawable"
    android:layout_centerInParent="true"/>

效果如下:

337.gif

添加边界后的xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#E4F34A">
    <item android:drawable="@android:color/holo_blue_bright"/>
</ripple>

效果如下:

338.gif

【PaintDrawable】

PaintDrawable不支持XML实现,只能通过代码去实现,特有的方法是:

void setCornerRadii (float[] radii)

为四个角的每一个指定半径。 对于每个角落,数组包含2个值[X_radius,Y_radius]。 角落的顺序是左上角,右上角,右下角,左下角。

实现圆角矩形,示例代码如下:

    PaintDrawable drawable = new PaintDrawable(Color.GREEN);
    drawable.setCornerRadii(new float[]{10,10,20,20,30,30,50,50});
    imageview.setBackground(drawable);

效果展示:

图片.png

还有一个特定方法,和前者的参数不一样:

void setCornerRadius (float radius)

指定矩形拐角的半径。

实现圆角矩形,示例代码如下:

    PaintDrawable drawable3 = new PaintDrawable(Color.GREEN);
    drawable3.setCornerRadius(30);
    mTextView.setBackground(drawable3);

效果展示:

图片.png

【NinePatchDrawable】

有听说过9path图片吗,就是所谓的.9图,在Android中非常适合背景,可以指定图片中某区域进行伸缩。

NinePatchDrawable为可调整大小的位图,带有可定义的可伸缩区域。

xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="true"
    android:src="@drawable/che" />

使用方法也比较简单,如下:

<ImageView
    android:id="@+id/imageview"
    android:layout_width="400dp"
    android:layout_height="300dp"
    android:background="@drawable/nine_drawable"
    android:layout_centerInParent="true"/>

当然也可以直接将.9图片资源设置为背景:

<ImageView
    android:id="@+id/imageview"
    android:layout_width="400dp"
    android:layout_height="300dp"
    android:background="@drawable/che"
    android:layout_centerInParent="true"/>

【DrawerArrowDrawable】

这个是配合抽屉布局返回键使用的,用法如下:

    DrawerArrowDrawable drawerArrowDrawable = new DrawerArrowDrawable(this);
    drawerArrowDrawable.setAlpha(1);
    drawerArrowDrawable.setSpinEnabled(false);
    drawerArrowDrawable.setDirection(DrawerArrowDrawable.ARROW_DIRECTION_LEFT);
    drawerArrowDrawable.setColor(Color.BLACK);
    mDrawerToggle.setDrawerArrowDrawable(drawerArrowDrawable);

【CircularProgressDrawable】

代码实现如下:

    CircularProgressDrawable circularProgressDrawable = new CircularProgressDrawable(this);
    // 设置绘制进度弧长
    circularProgressDrawable.setStartEndTrim(0,300);
    // 设置样式
    circularProgressDrawable.setStyle(CircularProgressDrawable.LARGE);
    // 设置边界
    //circularProgressDrawable.setBounds(0,0,5,5);
    // 设置环形的宽度
    circularProgressDrawable.setStrokeWidth(10f);
    // 设置环形的节点显示(Paint.Cap.ROUND即圆角)
    circularProgressDrawable.setStrokeCap(Paint.Cap.ROUND);
    // 设置环形的半径(控制环形的尺寸)
    circularProgressDrawable.setCenterRadius(50f);
    // 启用箭头
    circularProgressDrawable.setArrowEnabled(true);
    // 设置箭头的尺寸
    circularProgressDrawable.setArrowDimensions(20, 20);
    // 在箭头的尺寸上缩放倍数, 如果没有设置尺寸则无效
    circularProgressDrawable.setArrowScale(2f);
    // 判断是否在运行中
    circularProgressDrawable.isRunning();

使用如下:

// 运行
circularProgressDrawable.start();
// 停止
circularProgressDrawable.stop();

效果展示:

46.gif

【AdaptiveIconDrawable】

在Android O(Android 8.0)google推出了Launcher图标的自适应功能。

在3.x的Android Studio中,可以手动设置自适应Launcher图标,步骤如下:

File --> New --> Image Asset
图片.png

除了手动设置自适应图标之外,还可以使用AdaptiveIconDrawable来实现。

AdaptiveIconDrawable的标签是adaptive-icon,xml代码如下:

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

自适应图标由前景和背景两部分组成。

【VectorDrawable】

VectorDrawable是矢量图,由于矢量图不会失真,图片较小,所以比较适合做自适应图标,它的标签是vector,xml表示如下:

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
    <path
        android:fillColor="#FF226622"
        android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14zM12,10h-2v2L9,12v-2L7,10L7,9h2L9,7h1v2h2v1z"/>
</vector>

矢量图由线条组成,实现它的标签是<path>,根据路径可以绘制出任意图形,由于写起来非常复杂,所以建议使用三方工具制作一个矢量图再将矢量图导出,最后由Android Studio生成矢量图的xml代码。

Android Studio生成适量图的方法如下:

File --> New --> Vector Asset

【AnimationDrawable】

帧动画,它的xml标签是animation-list,xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/bird1" android:duration="200" />
    <item android:drawable="@drawable/bird2" android:duration="200" />
    <item android:drawable="@drawable/bird3" android:duration="200" />
    <item android:drawable="@drawable/bird4" android:duration="200" />
    <item android:drawable="@drawable/bird5" android:duration="200" />
    <item android:drawable="@drawable/bird6" android:duration="200" />
    <item android:drawable="@drawable/bird7" android:duration="200" />
    <item android:drawable="@drawable/bird8" android:duration="200" />
</animation-list>

其中oneshot表示是否只执行一次动画。

使用代码如下:

AnimationDrawable animationDrawable = (AnimationDrawable) imageview.getDrawable();
animationDrawable.start();

效果展示:

48.gif

【AnimatedStateListDrawable】

AnimatedStateListDrawable在Android 5.0之后开始支持。其xml标签是animated-selector,xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<animated-selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/off" android:drawable="@drawable/bird1" android:state_checked="false"/>
    <item android:id="@+id/on"  android:drawable="@drawable/bird8" android:state_checked="true"/>
    <transition
        android:fromId="@id/off"
        android:toId="@id/on">
        <animation-list >
            <item android:drawable="@drawable/bird1" android:duration="100"/>
            <item android:drawable="@drawable/bird2" android:duration="100"/>
            <item android:drawable="@drawable/bird3" android:duration="100"/>
            <item android:drawable="@drawable/bird4" android:duration="100"/>
            <item android:drawable="@drawable/bird5" android:duration="100"/>
            <item android:drawable="@drawable/bird6" android:duration="100"/>
            <item android:drawable="@drawable/bird7" android:duration="100"/>
            <item android:drawable="@drawable/bird8" android:duration="100"/>
        </animation-list>
    </transition>
</animated-selector>

根据视图的状态执行动画。

【AnimatedVectorDrawable】

AnimatedVectorDrawable即矢量图动画,矢量图本身不具备动画特性,需要结合属性动画完成矢量图动画。其xml标签是animated-vector,xml代码如下:

<animated-vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt">
    <aapt:attr name="android:drawable">
        <vector
            android:name="playtopause"
            android:width="24dp"
            android:height="24dp"
            android:viewportWidth="24"
            android:viewportHeight="24">
            <group
                android:name="group"
                android:pivotX="12"
                android:pivotY="12">
                <path
                    android:name="path"
                    android:pathData="M 8 5 L 8 19 L 19 12 Z"
                    android:fillColor="#000000"
                    android:strokeWidth="1"/>
            </group>
        </vector>
    </aapt:attr>
    <target android:name="path">
        <aapt:attr name="android:animation">
            <objectAnimator
                android:propertyName="pathData"
                android:duration="300"
                android:valueFrom="M 8 12 L 8 19 L 19 12 L 19 12 L 8 12 M 8 5 L 8 12 L 19 12 L 19 12 L 8 5"
                android:valueTo="M 5 6 L 5 10 L 19 10 L 19 6 L 5 6 M 5 14 L 5 18 L 19 18 L 19 14 L 5 14"
                android:valueType="pathType"
                android:interpolator="@android:interpolator/fast_out_slow_in"/>
        </aapt:attr>
    </target>
    <target android:name="group">
        <aapt:attr name="android:animation">
            <objectAnimator
                android:propertyName="rotation"
                android:duration="300"
                android:valueFrom="0"
                android:valueTo="90"
                android:valueType="floatType"
                android:interpolator="@android:interpolator/fast_out_slow_in"/>
        </aapt:attr>
    </target>
</animated-vector>

可以查看这边文章了解详细Android动画<第十一篇>:矢量图动画

【AnimatedImageDrawable】

Android 9.0 引入了AnimatedImageDrawable类,用于绘制和显示 GIF 和 WebP 动画图像。 AnimatedImageDrawable 的工作方式与 AnimatedVectorDrawable 的相似之处在于,都是渲染线程驱动 AnimatedImageDrawable 的动画。 渲染线程还使用工作线程进行解码,因此,解码不会干扰渲染线程的其他操作。 这种实现机制允许您的应用在显示动画图像时,无需管理其更新,也不会干扰应用界面线程上的其他事件。

可使用 ImageDecoder 的实例对 AnimatedImageDrawable 进行解码。 代码如下:

    Drawable decodedAnimation = ImageDecoder.decodeDrawable(ImageDecoder.createSource(getResources(), R.drawable.demo));
    
    if (decodedAnimation instanceof AnimatedImageDrawable) {
        // Prior to start(), the first frame is displayed.
        ((AnimatedImageDrawable) decodedAnimation).start();
    }

[本章完...]

上一篇 下一篇

猜你喜欢

热点阅读