[Android][VectorDrawable的使用]

1.什么是Vector矢量圖

Android API 21(5.0)引入了一個Drawable的子類VectorDrawable目的就是用來渲染矢量圖,AnimatedVectorDrawable用來播放矢量動畫。之前老的小于21的API設(shè)備可以分別使用VectorDrawableCompat和AnimatedVectorDrawableCompat這兩個兼容包來同樣達到渲染矢量圖的目的。

2.語法規(guī)則

以下是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
    沒有參數(shù),連接起點和終點
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軸旋轉(zhuǎn)角度 
    large-arc-flag 為0時表示取小弧度,1時取大弧度 (舍取的時候,是要長的還是短的)
    sweep-flag 0取逆時針方向,1取順時針方向 

3.標簽參數(shù)

多個pathData標簽疊加需要使用<group></group>, 另外還有一些屬性,

Path標簽:
android:name 定義該 path 的名字,這樣在其他地方可以通過名字來引用這個路徑
android:pathData 和 SVG 中 d 元素一樣的路徑信息。
android:fillColor 定義填充路徑的顏色,如果沒有定義則不填充路徑
android:strokeColor 定義如何繪制路徑邊框,如果沒有定義則不顯示邊框
android:strokeWidth 定義路徑邊框的粗細尺寸
android:strokeAlpha 定義路徑邊框的透明度
android:fillAlpha 定義填充路徑顏色的透明度
android:trimPathStart 從路徑起始位置截斷路徑的比率,取值范圍從 0 到1
android:trimPathEnd 從路徑結(jié)束位置截斷路徑的比率,取值范圍從 0 到1
android:trimPathOffset 設(shè)置路徑截取的范圍,取值范圍從 0 到1
android:strokeLineCap 設(shè)置路徑線帽的形狀,取值為 butt, round, square.
android:strokeLineJoin 設(shè)置路徑交界處的連接方式,取值為 miter,round,bevel.
android:strokeMiterLimit 設(shè)置斜角的上限

group 標簽:主要是用來設(shè)置路徑做動畫的關(guān)鍵屬性的
android:name 定義 group 的名字
android:rotation 定義該 group 的路徑旋轉(zhuǎn)多少度
android:pivotX 定義縮放和旋轉(zhuǎn)該 group 時候的 X 參考點。該值相對于 vector 的 viewport 值來指定的。
android:pivotY 定義縮放和旋轉(zhuǎn)該 group 時候的 Y 參考點。該值相對于 vector 的 viewport 值來指定的。
android:scaleX 定義 X 軸的縮放倍數(shù)
android:scaleY 定義 Y 軸的縮放倍數(shù)
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 的內(nèi)部(intrinsic)寬度,支持所有 Android 系統(tǒng)支持的尺寸,通常使用 dp
android:height 定義該 drawable 的內(nèi)部(intrinsic)高度,支持所有 Android 系統(tǒng)支持的尺寸,通常使用 dp
android:viewportWidth 定義矢量圖視圖的寬度,視圖就是矢量圖 path 路徑數(shù)據(jù)所繪制的虛擬畫布
android:viewportHeight 定義矢量圖視圖的高度,視圖就是矢量圖 path 路徑數(shù)據(jù)所繪制的虛擬畫布
android:tint 定義該 drawable 的 tint 顏色。默認是沒有 tint 顏色的
android:tintMode 定義 tint 顏色的 Porter-Duff blending 模式,默認值為 src_in
android:autoMirrored 設(shè)置當系統(tǒng)為 RTL (right-to-left) 布局的時候,是否自動鏡像該圖片。比如 阿拉伯語。
android:alpha 該圖片的透明度屬性

如下圖是一個VectorDrawable的xml文件內(nèi)容:


image.png

4.如何用Android Studio創(chuàng)建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.根據(jù)坐標點繪制圖形
 */
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.根據(jù)坐標點繪制圖形
        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會導(dǎo)致中間有些部分不會填充顏色
//            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();
////            繪制填充區(qū)域
//            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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,030評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,310評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 175,951評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,796評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,566評論 6 407
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,055評論 1 322
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,142評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,303評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,799評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,683評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,899評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,409評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,135評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,520評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,757評論 1 282
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,528評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,844評論 2 372

推薦閱讀更多精彩內(nèi)容