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

Android 繪圖之 Path 與七種 PathEffect 特效比較

編輯:關於 Android 程式設計

Path 是 Android 繪圖裡相當靈活的一塊,而搭配不同的 PathEffect,同一條線條能呈現完全不同的質感。這個範例用同一組座標畫七次,只替換 PathEffect 與顏色,方便直接比較差異。

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposePathEffect;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.DiscretePathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.SumPathEffect;
import android.graphics.PathEffect;
import android.os.Bundle;
import android.view.View;

public class PathTest extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));
    }

    class MyView extends View {
        float phase;
        PathEffect[] effects = new PathEffect[7];
        int[] colors;
        private Paint paint;
        Path path;

        public MyView(Context context) {
            super(context);
            paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(4);

            // 建立並初始化 Path
            path = new Path();
            path.moveTo(0, 0);
            for (int i = 1; i <= 15; i++) {
                // 產生 15 個點,Y 座標隨機,連成一條折線
                path.lineTo(i * 20, (float) Math.random() * 60);
            }

            // 初始化 7 種顏色
            colors = new int[] { Color.BLACK, Color.BLUE, Color.CYAN,
                    Color.GREEN, Color.MAGENTA, Color.RED, Color.YELLOW };

            // ---------- 初始化 7 種路徑效果 ----------
            effects[0] = null;                              // 無效果
            effects[1] = new CornerPathEffect(10);          // 轉角平滑
            effects[2] = new DiscretePathEffect(3.0f, 5.0f);// 隨機偏移
        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(Color.WHITE);

            // DashPathEffect 帶有動畫感
            effects[3] = new DashPathEffect(new float[] { 20, 10, 5, 10 }, phase);

            // PathDashPathEffect:用一個小方塊沿路徑排列
            Path p = new Path();
            p.addRect(0, 0, 8, 8, Path.Direction.CCW);
            effects[4] = new PathDashPathEffect(p, 12, phase,
                    PathDashPathEffect.Style.ROTATE);

            // ComposePathEffect:先套用 effects[4],再套用 effects[2]
            effects[5] = new ComposePathEffect(effects[2], effects[4]);
            // SumPathEffect:兩個效果同時作用
            effects[6] = new SumPathEffect(effects[4], effects[3]);

            // 座標變換:整體位移到 (8, 8) 起繪
            canvas.translate(8, 8);

            for (int i = 0; i < effects.length; i++) {
                paint.setPathEffect(effects[i]);
                paint.setColor(colors[i]);
                canvas.drawPath(path, paint);
                canvas.translate(0, 60);   // 每行往下 60
            }

            // 改變 phase 值形成動畫
            phase += 1;
            invalidate();
        }
    }
}

七種效果怎麼記

  • null:不做任何加工,原始線條。
  • CornerPathEffect(radius):把尖銳轉角磨成圓角,半徑越大越圓潤。
  • DiscretePathEffect(seg, dev):沿線段隨機偏移,段長與偏移量決定毛糙程度。
  • DashPathEffect(intervals, phase):虛線,數字依序是「實線長、間隔長」循環,phase 可動。
  • PathDashPathEffect(shape, interval, phase, style):用自訂圖形(這裡是小方塊)沿路徑排列,style 可選 ROTATE、TRANSLATE、MORPH。
  • ComposePathEffect:把兩個效果串起來依序套用。
  • SumPathEffect:把兩個效果同時疊上去。

最後在 onDraw 裡 phase += 1 再 invalidate(),虛線與方塊就會持續位移,形成流動的視覺效果——這也是自繪載入動畫最省力的做法之一。

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