初識android vector drawable

VectorDrawable

在處理Android開發佈局適配的時候,我們會經常遇到圖片圖標因爲縮放引起的模糊等問題。雖然能夠通過多套圖片來解決這個問題,但是這種方法帶來的一個弊端就是應用的體積會變得很大。提到縮放而不失真我們可能會很快想到了矢量圖這個詞,從Android5.0開始(API21)Google提供了對於矢量圖片的支持——VectorDrawable。使用VectorDrawable能夠在Android中使用矢量圖,推薦針對於簡單的圖形來進行使用,特別是圖標一類的簡單圖像。
VectorDrawable能夠帶來哪些方便:
- 不會因爲不同分辨率出現模糊等失真的情況。
- 替換png圖片能夠在一定程度上減小應用程序的體積。
- 配合使用屬性動畫能夠做出一些比較絢麗的動畫效果。
使用中一些不方便的地方:因爲矢量圖形主要數據部分是一些“軌跡”命令,VectorDrawable只能支持部分。編寫起來要費盡一些。不過一些簡單的內容就能夠幫我們解決一些問題,所以對於開發的幫助還是非常有幫助的。

一個簡單的矢量圖例子

這裏寫圖片描述

<?xml version="1.0" encoding="utf-8"?>
<!--android:width/android:height:矢量圖形的大小,-->
<!--android:viewportHeight/android:viewportWidth:矢量圖形xy的座標長度-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="100dp"
    android:height="100dp"
    android:viewportHeight="200"
    android:viewportWidth="200"
    >

  <path
      android:name="left_ellipse"
      android:strokeColor="@android:color/background_dark"
      android:strokeWidth="1"
      android:fillColor="@android:color/background_light"
      android:pathData="M100,100 A50,50 0 1,1 100,200 A100,100 0 1,1 100,0 A50,50, 0, 1,0 100,100"
      />

  <path
      android:name="left_ellipse"
      android:strokeColor="@android:color/background_dark"
      android:strokeWidth="1"
      android:fillColor="@android:color/background_dark"
      android:pathData="M100,100 A50,50 0 1,1 100,0 A100,100 0 1,1 100,200 A50,50, 0, 1,0 100,100"
      />

  <path
      android:name="left_inner_circle"
      android:strokeColor="@android:color/background_dark"
      android:strokeWidth="1"
      android:fillColor="@android:color/background_dark"
      android:pathData="M100,130 A20,20 0 1,1 99,130"
      />

  <path
      android:name="left_inner_circle"
      android:strokeColor="@android:color/background_dark"
      android:strokeWidth="1"
      android:fillColor="@android:color/background_light"
      android:pathData="M100,30 A20,20 0 1,1 99,30"

      />


</vector>

各節點屬性解釋

解釋爲個人理解並非直譯,沒有翻譯的部分沒有用到所以貼出來原句。個人理解可能有偏差,若有錯望指正。

vector爲根節點,定義一個vector drawable

常用屬性以及解釋:
1.android:width/android:height:矢量圖形的大小,這個應該理解很簡單
2.android:viewportHeight/android:viewportWidth:矢量圖形xy的座標長度,
    可以理解爲畫布的尺寸,這裏設置爲長X寬=200X200,圖形的位置,線條的尺寸等等都與此有關。
3.android:name:節點的名稱,可以通過此名稱來引用該節點。例如設置圖像的一部分有動畫效果。
    group和path的本屬性同理。
4.android:alpha:透明度

一下爲不常用屬性:
5.android:tintMode:着色方式,可參考http://segmentfault.com/a/1190000003038675
6.android:tint:可設置着色顏色
7.android:autoMirrored:Indicates if the drawable needs to be mirrored
    when its layout direction is RTL (right-to-left).

group 爲一組path的集合,可以用於一組path的位移,旋轉,縮放等狀態和動畫。

屬性以及解釋:
1.android:name:節點的名稱,可以通過此名稱來引用該節點。例如設置圖像的一部分有動畫效果。
2.android:rotation:該部分的旋轉角度
3.android:pivotX/Y:旋轉中心的位置
4.android:scaleX/Y:縮放中心的位置
5.android:translateX/Y:移動的距離

path 定義了要繪製的path的屬性

屬性以及解釋:
1.android:name:節點的名稱,可以通過此名稱來引用該節點。例如設置圖像的一部分有動畫效果。
2.android:pathData:矢量繪製指令
3.android:fillColor:填充顏色
4.android:strokeColor:邊線顏色
5.android:strokeWidth:線條的寬度
6.android:strokeAlpha:線條的透明度
7.android:fillAlpha:填充的透明度
8.android:trimPathStart:可設置爲0-1,會呈現繪製逆過程的動畫
9.android:trimPathEnd:可設置0-1,呈現繪製過程的動畫
10.android:trimPathOffset:Shift trim region
  (allows showed region to include the start and end), in the range from 0 to 1
11.android:strokeLineCap:線頭屬性: butt, round, square.
12.android:strokeLineJoin:Sets the lineJoin for a stroked path: miter,round,bevel.
13.android:strokeMiterLimit:Sets the Miter limit for a stroked path.

不常用的clip-path:Defines path to be the current clip.Note that the clip path only apply to the current group and its children.

1.android:name:Defines the name of the clip path
2.android:pathData:Defines clip path using the same format as "d" attribute in the SVG's path data.

SVG PATH繪製指令簡介

常用的繪製指令

  • M:moveto 移動,用法Mx,y移動到x,y

  • L:line to,繪製直線,用法Lx,y直線連到x,y,起點爲上一步的結尾點或者是(0,0),還有簡化命令H(x) 水平連接、V(y)垂直連接 參數爲線的結束點的x/y座標

  • Z:沒有參數,連接起點和終點

  • C:三次貝塞爾曲線,用法C(x1 y1 x2 y2 x y)控制點x1,y1 x2,y2,終點x,y

  • Q:二次貝塞爾曲線,用法Q(x1 y1 x y),控制點x1,y1,終點x,y

  • A:橢圓圓弧,用法A(rx ry x-axis-rotation large-arc-flag sweep-flag x y) ,X軸半徑rx,Y軸半徑ry, x-axis-rotation爲X軸偏角, large-arc-flag爲0取小弧度,1取大弧度,sweep-flag爲0逆時針,1順時針,終點x,y

關於貝塞爾曲線的深入理解可以參考:http://www.zhangxinxu.com/wordpress/2014/06/deep-understand-svg-path-bezier-curves-command/

關於圓弧的參數的更深入理解:http://blog.csdn.net/xu_fu/article/details/44004841

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章