[Android][VectorDrawable的使用]

1.什么是Vector矢量图

Android API 21(5.0)引入了一个Drawable的子类VectorDrawable目的就是用来渲染矢量图,AnimatedVectorDrawable用来播放矢量动画。之前老的小于21的API设备可以分别使用VectorDrawableCompat和AnimatedVectorDrawableCompat这两个兼容包来同样达到渲染矢量图的目的。

2.语法规则

以下是pathData中用到的语法:

M:move to 移动绘制点,作用相当于把画笔落在哪一点。
L:line to 直线,就是一条直线,注意,只是直线,直线是没有宽度的,所以你什么也看不到。
Z:close 闭合,嗯,就是把图封闭起来。
C:cubic bezier 三阶贝塞尔曲线
Q:quatratic bezier 二阶贝塞尔曲线
A:ellipse 圆弧

语法详解:

M (x y) 
    把画笔移动到x,y,要准备在这个地方画图了。
L (x y) 
    直线连到x,y,还有简化命令H(x) 水平连接、V(y)垂直连接。
Z
    没有参数,连接起点和终点
C(x1 y1 x2 y2 x y)
    控制点(x1,y1)( x2,y2),终点x,y 。
Q(x1 y1 x y)
    控制点(x1,y1),终点x,y
A(rx ry x-axis-rotation large-arc-flag sweep-flag x y) 
    rx ry 椭圆半径 
    x-axis-rotation x轴旋转角度 
    large-arc-flag 为0时表示取小弧度,1时取大弧度 (舍取的时候,是要长的还是短的)
    sweep-flag 0取逆时针方向,1取顺时针方向 

3.标签参数

多个pathData标签叠加需要使用, 另外还有一些属性,

Path标签:
android:name 定义该 path 的名字,这样在其他地方可以通过名字来引用这个路径
android:pathData 和 SVG 中 d 元素一样的路径信息。
android:fillColor 定义填充路径的颜色,如果没有定义则不填充路径
android:strokeColor 定义如何绘制路径边框,如果没有定义则不显示边框
android:strokeWidth 定义路径边框的粗细尺寸
android:strokeAlpha 定义路径边框的透明度
android:fillAlpha 定义填充路径颜色的透明度
android:trimPathStart 从路径起始位置截断路径的比率,取值范围从 0 到1
android:trimPathEnd 从路径结束位置截断路径的比率,取值范围从 0 到1
android:trimPathOffset 设置路径截取的范围,取值范围从 0 到1
android:strokeLineCap 设置路径线帽的形状,取值为 butt, round, square.
android:strokeLineJoin 设置路径交界处的连接方式,取值为 miter,round,bevel.
android:strokeMiterLimit 设置斜角的上限

group 标签:主要是用来设置路径做动画的关键属性的
android:name 定义 group 的名字
android:rotation 定义该 group 的路径旋转多少度
android:pivotX 定义缩放和旋转该 group 时候的 X 参考点。该值相对于 vector 的 viewport 值来指定的。
android:pivotY 定义缩放和旋转该 group 时候的 Y 参考点。该值相对于 vector 的 viewport 值来指定的。
android:scaleX 定义 X 轴的缩放倍数
android:scaleY 定义 Y 轴的缩放倍数
android:translateX 定义移动 X 轴的位移。相对于 vector 的 viewport 值来指定的。
android:translateY 定义移动 Y 轴的位移。相对于 vector 的 viewport 值来指定的。

clip-path标签:定义当前绘制的剪切路径。注意,clip-path 只对当前的 group 和子 group 有效
android:name 定义 clip path 的名字
android:pathData 和 android:pathData 的取值一样。

vector标签:定义这个矢量图
android:name 定义该drawable的名字
android:width 定义该 drawable 的内部(intrinsic)宽度,支持所有 Android 系统支持的尺寸,通常使用 dp
android:height 定义该 drawable 的内部(intrinsic)高度,支持所有 Android 系统支持的尺寸,通常使用 dp
android:viewportWidth 定义矢量图视图的宽度,视图就是矢量图 path 路径数据所绘制的虚拟画布
android:viewportHeight 定义矢量图视图的高度,视图就是矢量图 path 路径数据所绘制的虚拟画布
android:tint 定义该 drawable 的 tint 颜色。默认是没有 tint 颜色的
android:tintMode 定义 tint 颜色的 Porter-Duff blending 模式,默认值为 src_in
android:autoMirrored 设置当系统为 RTL (right-to-left) 布局的时候,是否自动镜像该图片。比如 阿拉伯语。
android:alpha 该图片的透明度属性

如下图是一个VectorDrawable的xml文件内容:


image.png

4.如何用Android Studio创建VectorDrawable

点击Vector Asset


image.png

会打开如下界面,点击Next,再点击finish即可


image.png

5.对比自定义View画图

其实VectorDrawable很像自定义控件里的画图步骤。如下代码

package com.lgy.android.draw;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * 1.确定画布的大小,将画布分成40等份
 * 2.根据坐标点绘制图形
 */
public class MyView extends View {

    private int width;
    private int height;
    private Paint mPaint = null;
    private int strokeColor = Color.parseColor("#f24e4e");
    private int strokeWidth;
    private Context mContext = null;

    public MyView(Context context) {
        this(context, null, 0);
    }

    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(strokeColor);
        strokeWidth = Util.dp2px(mContext,1);
        mPaint.setStrokeWidth(strokeWidth);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (width <= 0 || height <= 0) {
            return;
        }
//        1.确定画布的大小,将画布分成40等份
        float tempw = width / 40.0f;
        float temph = height/40.0f;
//        2.根据坐标点绘制图形
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            canvas.drawRoundRect(4 * tempw, 4 * temph, 36 * tempw, 36 * temph, 4 * tempw, 4 * temph, mPaint);
        } else {
            //方法一,一般不会用这种
//            RectF rectF = new RectF(4 * tempw, 4 * temph, 12 * tempw, 12 * temph);
//            canvas.drawArc(rectF, 180, 90, true, mPaint);
//            canvas.drawLine(8 * tempw, 4 * temph,32 * tempw,4 * temph, mPaint);
//            RectF rectF2 = new RectF(28 * tempw, 4 * temph, 36 * tempw, 12 * temph);
//            canvas.drawArc(rectF2, 270, 90, true, mPaint);
//            canvas.drawLine(36 * tempw, 8 * temph,36 * tempw,32 * temph, mPaint);
//            RectF rectF3 = new RectF(28 * tempw, 28 * temph, 36 * tempw, 36 * temph);
//            canvas.drawArc(rectF3, 0, 90, true, mPaint);
//            canvas.drawLine(32 * tempw, 36 * temph,8 * tempw,36 * temph, mPaint);
//            RectF rectF4 = new RectF(4 * tempw, 28 * temph, 12 * tempw, 36 * temph);
//            canvas.drawArc(rectF4, 90, 90, true, mPaint);
//            canvas.drawLine(4 * tempw, 32 * temph,4 * tempw,8 * temph, mPaint);
//            canvas.drawRect(8* tempw,4* temph,32* tempw,36* temph,mPaint);
//            canvas.drawRect(4* tempw,8* temph,36* tempw,32* temph,mPaint);
            //方法二
            //如果使用的是addArc会导致中间有些部分不会填充颜色
//            Path path = new Path();
//            path.moveTo(4 * tempw, 4 * temph);
//            RectF rectF = new RectF(4 * tempw, 4 * temph, 12 * tempw, 12 * temph);
//            path.addArc(rectF, 180, 90);
//            path.lineTo(32 * tempw,4 * temph);
//            RectF rectF2 = new RectF(28 * tempw, 4 * temph, 36 * tempw, 12 * temph);
//            path.addArc(rectF2, 270, 90);
//            path.lineTo(36 * tempw,32 * temph);
//            RectF rectF3 = new RectF(28 * tempw, 28 * temph, 36 * tempw, 36 * temph);
//            path.addArc(rectF3, 0, 90);
//            path.lineTo(8 * tempw,36 * temph);
//            RectF rectF4 = new RectF(4 * tempw, 28 * temph, 12 * tempw, 36 * temph);
//            path.addArc(rectF4, 90, 90);
//            path.lineTo(4 * tempw,8 * temph);
//            path.close();
////            绘制填充区域
//            mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
//            canvas.drawPath(path, mPaint);

            Path path = new Path();
            path.moveTo(4 * tempw, 4 * temph);
            RectF rectF = new RectF(4 * tempw, 4 * temph, 12 * tempw, 12 * temph);
            path.arcTo(rectF, 180, 90);
            path.lineTo(32 * tempw,4 * temph);
            RectF rectF2 = new RectF(28 * tempw, 4 * temph, 36 * tempw, 12 * temph);
            path.arcTo(rectF2, 270, 90);
            path.lineTo(36 * tempw,32 * temph);
            RectF rectF3 = new RectF(28 * tempw, 28 * temph, 36 * tempw, 36 * temph);
            path.arcTo(rectF3, 0, 90);
            path.lineTo(8 * tempw,36 * temph);
            RectF rectF4 = new RectF(4 * tempw, 28 * temph, 12 * tempw, 36 * temph);
            path.arcTo(rectF4, 90, 90);
            path.lineTo(4 * tempw,8 * temph);
            path.close();
            canvas.drawPath(path, mPaint);
        }
    }
}

具体比较可以在例子代码里查看。

6.参考资料

https://blog.csdn.net/u014644594/article/details/80465980
https://www.cnblogs.com/yuhanghzsd/p/5466846.html
https://developer.android.com/reference/android/graphics/drawable/VectorDrawable

7.源码地址

https://download.csdn.net/download/lgywsdy/11215102

你可能感兴趣的:([Android][VectorDrawable的使用])