Path 类 简介及使用示例

Android 除了可以将 N 个点连成一条路径,还为路径绘制提供了 PathEffect 来定义绘制效果,PathEffect 包含了如下子类(每个子类代表一种绘制效果):

  1. ComposePathEffect 可以用来组合两种路径效果,就是把两种效果二合一。它是先将路径变成innerpe的效果,再去复合outerpe的路径效果,即:outerpe(innerpe(Path))。
  2. CornerPathEffect 将路径的转角变得圆滑,CornerPathEffect的构造方法只接受一个参数radius,意思就是转角处的圆滑程度。
  3. DashPathEffect 它包含了两个参数:第一个参数是一个浮点型的数组,偶数参数定义了每一条实线的长度,而奇数参数则表示每一条虚线的长度;第二个参数(phase)我称之为偏移值,动态改变其值会让路径产生动画的效果。
  4. DiscretePathEffect (离散路径效果)相对来说则稍微复杂点,它会在路径上绘制很多“杂点”的突出来模拟一种类似生锈铁丝的效果。其构造方法有两个参数:第一个呢指定这些突出的“杂点”的密度,值越小杂点越密集;第二个参数呢则是“杂点”突出的大小,值越大突出的距离越大反之反之。
  5. PathDashPathEffect 它和DashPathEffect是类似的,不同的是PathDashPathEffect可以让我们自己定义路径虚线的样式
  6. SumPathEffect 可以用来组合两种路径效果,就是把两种效果二合一。它是把两种路径效果加起来再作用于路径。

下面使用一个简单示例来演示上面 6 种效果。

下面是主程序的代码:

package com.toby.personal.testlistview;

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.PathEffect;
import android.graphics.SumPathEffect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    final private static String TAG = "Toby_Test";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new TestView(this));
    }

    class TestView extends View {

        private float phase;
        private PathEffect[] effects;
        private int[] colors;
        private Paint paint;
        private Path path;
        private Path path1;

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

            path = new Path();
            path.moveTo(0, 0);
            for (int i = 1; i <= 15; i++) {
                path.lineTo(i * 20, (float) Math.random() * 60);
            }

            path1 = new Path();
            path1.addRect(0, 0, 8, 8, Path.Direction.CCW);

            colors = new int[]{Color.BLACK, Color.BLUE, Color.CYAN, Color.GREEN,
                    Color.MAGENTA, Color.RED, Color.YELLOW};

            effects = new PathEffect[7];

        }

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

            effects[0] = null;
            effects[1] = new CornerPathEffect(10);
            effects[2] = new DiscretePathEffect(3.0f, 5.0f);
            effects[3] = new DashPathEffect(new float[]{20, 10, 5, 10}, phase);
            effects[4] = new PathDashPathEffect(path1, 12, phase, PathDashPathEffect.Style.ROTATE);
            effects[5] = new ComposePathEffect(effects[2], effects[4]);
            effects[6] = new SumPathEffect(effects[4], effects[3]);

            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);
            }

            phase += 1; // 改变 phase 的值形成动画效果
            invalidate();
        }
    }

}

上面示例的运行效果图:

显示效果

除此之外,Android 的 Canvas 还提供了一个 drawTextOnPath 方法,该方法可以沿着 Path 绘制文本。下面是该方法的简单示例:

package com.toby.personal.testlistview;

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.PathEffect;
import android.graphics.RectF;
import android.graphics.SumPathEffect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    final private static String TAG = "Toby_Test";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new TestTextView(this));
    }

    class TestTextView extends View {

        final private static String DRAW_TEXT = "This is a test text.";
        Path[] paths = new Path[3];
        Paint paint;

        public TestTextView(Context context) {
            super(context);

            paths[0] = new Path();
            paths[0].moveTo(0, 0);
            for (int i = 0; i <= 7; ++i) {
                paths[0].lineTo(i * 30, (float) Math.random() * 30);
            }

            paths[1] = new Path();
            RectF rectF = new RectF(0, 0, 200, 120);
            paths[1].addOval(rectF, Path.Direction.CCW);

            paths[2] = new Path();
            paths[2].addArc(rectF, 60, 180);

            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(Color.CYAN);
            paint.setStrokeWidth(1);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            canvas.drawColor(Color.GRAY);

            canvas.translate(40, 40);
            paint.setTextAlign(Paint.Align.RIGHT);
            paint.setTextSize(30);
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawPath(paths[0], paint);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawTextOnPath(DRAW_TEXT, paths[0], -8, 20, paint);

            canvas.translate(0, 60);
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawPath(paths[1], paint);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawTextOnPath(DRAW_TEXT, paths[1], -20, 20, paint);

            canvas.translate(0, 120);
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawPath(paths[2], paint);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawTextOnPath(DRAW_TEXT, paths[2], -10, 20, paint);
        }
    }

}

该示例的运行效果:


显示效果

参考文献:《疯狂Android讲义(第2版)》
参考链接:详解Paint的setPathEffect(PathEffect effect)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,569评论 25 707
  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,422评论 22 663
  • 我喜欢的歌词。 狼爱上羊,爱的疯狂,谁让他们真爱了一场。老狼 我在遥望,月亮之上,有多少梦想在自由的飞翔。凤凰传奇...
    Nait99阅读 603评论 1 1
  • 下面这篇文章来为大家介绍《精进》这本书的关于“选择”的部分。 我们的一生都是在不断选择中度过的,某种程度上说,所谓...
    栉风沐雨1阅读 275评论 0 1
  • 1、如果皮囊难以修复,我愿意用思想填满它 2.①我是胡歌 全盘规划好自好的人生是为了在将来成为一名真正的一家之主 ...
    我爱成均馆阅读 232评论 0 0