Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

自訂彩色斜紋分隔線 View:像外送 App 那樣的緞帶效果

編輯:關於 Android 程式設計

需求背景

近期產品涉及訂單頁,設計稿上出現一個類似「餓了麼」、「支付寶口碑外賣」訂單頁的彩帶元素——通俗來說,就是由兩種顏色交錯拼接而成的分隔線。

共同特色是:以兩色相間拼接,長度則完全由螢幕寬度決定。正因為寬度不固定,用一張圖片當 ImageView 背景並不理想。與其如此,不如自訂 View 把它畫出來。

一、先學會畫平行四邊形

 public class ColourLineView extends View{
 public ColourLineView(Context context) {
 super(context, null);
 }
 public ColourLineView(Context context, AttributeSet attrs) {
 super(context, attrs, 0);
 }
 public ColourLineView(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 }

 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 int width = getWidth();
 int height = getHeight();

 Path path = new Path();
 canvas.save();
 path.reset();
 path.moveTo(width / 2, 0); // 左上
 path.lineTo(0, height); // 左下
 path.lineTo(width - width / 2, height); // 右下
 path.lineTo(width, 0); // 右上
 canvas.clipPath(path); // 把路徑範圍裁出來
 canvas.drawColor(Color.RED);
 path.reset();
 canvas.restore();
 }
 }

關鍵在 onDraw:先取得 View 的寬高,建立 Path,把起點移到(寬的二分之一, 0),接著依序連到(0, 高)、(寬的二分之一, 高)、(寬, 0)。四個點連完後呼叫 clipPath 把範圍裁出來,再填色,一個平行四邊形就完成了。

在版面中試用:

 <com.example.yang.statubardemo.ColourLineView
 android:layout_width="80dp"
 android:layout_height="80dp"
 android:background="#000"/>

形狀出來了。學會畫一塊磚,砌成整面牆自然不成問題。

二、定義可調整的屬性

一個夠用的彩色分隔線應該允許調整:線條高度、單一色塊寬度、色塊間距、傾斜程度、兩種顏色以及底色。先在 attrs.xml 宣告:

 <declare-styleable name="ColourLineView">
 <!-- 線條高度 -->
 <attr name="line_height" format="dimension"/>
 <!-- 第一種顏色塊的寬度 -->
 <attr name="item_width" format="dimension"/>
 <!-- 第二種顏色塊的寬度 -->
 <attr name="separation_width" format="dimension"/>
 <!-- 平行四邊形傾斜的程度 -->
 <attr name="lean_degree" format="dimension"/>
 <!-- 第一種顏色 -->
 <attr name="first_color" format="color"/>
 <!-- 第二種顏色 -->
 <attr name="second_color" format="color"/>
 <!-- 線條底色 -->
 <attr name="canvas_color" format="color"/>
 </declare-styleable>

接著在自訂 View 中讀取這些屬性並重複繪製色塊:

 public class ColourLineView extends View{
 private float line_height; // 線條高度
 private float item_width; // 每個顏色塊的寬度
 private float separation_width; // 兩個色塊之間的間距
 private float lean_degree; // 傾斜程度
 private int first_color; // 第一種顏色
 private int second_color; // 第二種顏色
 private int canvas_color; // 底色

 public ColourLineView(Context context) {
 super(context, null);
 }
 public ColourLineView(Context context, AttributeSet attrs) {
 super(context, attrs);
 initAttr(context, attrs);
 }
 public ColourLineView(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 initAttr(context, attrs);
 }

 public void initAttr(Context context, AttributeSet attrs){
 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ColourLineView);
 line_height = typedArray.getDimension(R.styleable.ColourLineView_line_height, 20);
 item_width = typedArray.getDimension(R.styleable.ColourLineView_item_width, 20);
 separation_width = typedArray.getDimension(R.styleable.ColourLineView_separation_width, 20);
 lean_degree = typedArray.getDimension(R.styleable.ColourLineView_lean_degree, 5);
 first_color = typedArray.getColor(R.styleable.ColourLineView_first_color, Color.RED);
 second_color = typedArray.getColor(R.styleable.ColourLineView_second_color, Color.GREEN);
 canvas_color = typedArray.getColor(R.styleable.ColourLineView_canvas_color, Color.WHITE);
 typedArray.recycle();
 }

 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 Path path = new Path();
 int lineWidth = getWidth();
 int lineHeight = getHeight();
 int count = (item_width + separation_width == 0) ? 0
 : lineWidth / (int) (item_width + separation_width) + 1;

 for (int i = 0; i < count; i++) {
 canvas.save();
 path.reset();
 path.moveTo(lean_degree + (item_width + separation_width) * i, 0);
 path.lineTo((item_width + separation_width) * i, lineHeight);
 path.lineTo(item_width * (i + 1) + separation_width * i, lineHeight);
 path.lineTo(lean_degree + item_width * (i + 1) + separation_width * i, 0);
 canvas.clipPath(path);
 if (i % 2 == 0) {
 canvas.drawColor(first_color);
 } else {
 canvas.drawColor(second_color);
 }
 canvas.restore();
 }
 }
 }

initAttr 只是單純讀取屬性值,重點在 onDraw。要間隔繪製多個平行四邊形,得先算出總共需要幾塊:把「一個色塊 + 一段間距」視為一個單位,用整體寬度除以它再進位,就是數量。

真正的難點是四個端點的定位,這裡用歸納法說明:

     當 i = 0,左上角是(lean_degree, 0),左下角是(0, line_height),右上與右下各自加上 item_width。

     當 i = 1,左上角要在前一個的基礎上再加一組「色塊 + 間距」,也就是(lean_degree + (item_width + separation_width) × 1, 0),其餘三點同理。

     推廣到 i = i,四點分別為:

(lean_degree + (item_width + separation_width) × i, 0)

((item_width + separation_width) × i, lineHeight)

(item_width × (i + 1) + separation_width × i, lineHeight)

(lean_degree + item_width × (i + 1) + separation_width × i, 0)

最後依序數的奇偶決定要填哪一色,兩色交錯的效果就完成了。

三、使用方式

 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 xmlns:tools="https://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 xmlns:app="https://schemas.android.com/apk/res-auto"
 android:orientation="vertical"
 android:gravity="center"
 tools:context="com.example.zjyang.statubardemo.MainActivity">

 <com.example.zjyang.statubardemo.ColourLineView
 android:layout_width="match_parent"
 android:layout_height="5dp"
 android:background="#fff"
 app:first_color="@color/colorAccent"
 app:second_color="@color/colorPrimary"
 app:item_width="15dp" />
 </LinearLayout>

高度 5dp、單一色塊寬 15dp、白底、兩色相間,成果如下:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved