Android Api Demos登顶之路(七十九)Graphics-->PathEffects

本文介绍了一个Android应用程序示例,展示了如何通过不同的路径效果来改变绘制路径的外观,包括使用圆角连接、虚线效果以及自定义路径形状。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * 这个demon演示了路径的特效,在默认的情况下我们所绘制的路径是黑色细实线,连接处为锐角形连接。
 * 在需要的情况上我们可以通过 设置路径的特效,改变路径的连接方式(如圆弧连接,这们路径就会看起来比较平滑)。
 * 可以设置虚线,并且可以自定义虚线段的形状。也可以综合使用两种路径的特效。
 */
public class MainActivity extends Activity {

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

    private class SampleView extends View {
        private Paint mPaint;
        private Path mPath;
        private PathEffect[] mEffects;
        private int[] mclolors;
        private float mPhase;

        // 设置路径效果
        private void makeEffects(PathEffect[] e, float phase) {
            e[0] = null;
            // 使用圆角连接的效果,参数为连接处圆角的半径
            e[1] = new CornerPathEffect(10);
            // 设置虚线的效果,第一参数为一个folat型数组,用于定制虚线。数组的奇数项表示
            // 线段的宽度,数组的偶数项表示间隔的宽度。第二个参数表示绘制时的偏移量(即从哪个位置起始)。
            float[] inverse=new float[] { 10, 5, 5, 5 };
            e[2] = new DashPathEffect(inverse, phase);
            // 设置一条自定义的虚线,第一个参数表示自定义的线段的形状,第二个参数表示线段表示线段间的距离
            // 第三个参数表示偏移量,第四个参数表示线段连接处的图形转换方式
            e[3] = new PathDashPathEffect(makePathDash(), 12, phase,
                    PathDashPathEffect.Style.ROTATE);
            e[4]=new ComposePathEffect(e[2], e[1]);
            e[5]=new ComposePathEffect(e[3], e[1]);

        }

        private Path makePathDash() {
            Path p = new Path();
            p.moveTo(4, 0);
            p.lineTo(0, -4);
            p.lineTo(8, -4);
            p.lineTo(12, 0);
            p.lineTo(8, 4);
            p.lineTo(0, 4);
            return p;
        }

        public SampleView(Context context) {
            super(context);
            setFocusable(true);

            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(6);

            mPath = makePath();
            mEffects = new PathEffect[6];
            mclolors = new int[] { Color.BLACK, Color.RED, Color.BLUE,
                    Color.GREEN, Color.MAGENTA, Color.BLACK };

        }

        /*
         * 定制一条路径:每段宽20,在上下35范围内起伏的波浪线
         */
        private Path makePath() {
            Path path = new Path();
            path.moveTo(0, 0);
            for (int i = 1; i <= 15; i++) {
                path.lineTo(i * 20, (float) Math.random() * 35);
            }
            return path;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            RectF bounds = new RectF();
            // 计算控制点边界的坐标并存储到bounds当中,第二个参数已不再使用
            mPath.computeBounds(bounds, false);
            // System.out.println("left:" + bounds.left);
            // System.out.println("top:" + bounds.top);
            canvas.translate(10, 10);

            makeEffects(mEffects, mPhase);
            mPhase++;
            //System.out.println("偏移量:"+mPhase);
            //即使把这个初始化的方法放在此处,也会先将onDraw()方法(即后面的代码)执行完毕
            //再重新调用onDraw()方法。效果同放在最后是一样的。
            //invalidate();

            for (int i = 0; i < mEffects.length; i++) {
                mPaint.setPathEffect(mEffects[i]);
                mPaint.setColor(mclolors[i]);
                canvas.drawPath(mPath, mPaint);
                canvas.translate(0, 28);
            }
            //System.out.println("图形绘制完毕!");
            invalidate();
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值