android – 在画布上绘制时自定义路径线条样式

前端之家收集整理的这篇文章主要介绍了android – 在画布上绘制时自定义路径线条样式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在研究地图叠加层,它突出显示沿指定点的路线,我需要实现某些线条样式(类似于截图)

我正在尝试做的是用两条边用黑色笔划线的透明线条突出显示路线

到目前为止,玩不同的填充样式和Paint设置并没有让我找到任何解决方案.

有谁知道我需要找什么方向?

目前我设法只绘制实线,但这不是我想要的:

油漆设置:

mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(COLOR_DEFAULT);
mPaint.setPathEffect(new CornerPathEffect(10));
mPaint.setStrokeWidth(6);
mPaint.setAntiAlias(true);

绘图程序

canvas.drawPath(mPath,mPaint);

解决方法

使用“破折号标记”(两个非常薄的矩形和MORPH样式选项),PathDashPathEffect得到了相当不错的结果.在这里查看最后一行和第三行:

这是通过修改从SDK获取的ApiDemos中的PathEffects示例得出的:

package com.example.android.apis.graphics;

import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

public class PathEffects extends GraphicsActivity {

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

    private static class SampleView extends View {
        private Paint mPaint;
        private Path mPath;
        private PathEffect[] mEffects;
        private int[] mColors;
        private float mPhase = 3;

        private static void makeEffects(PathEffect[] e,float phase) {
            e[0] = null;     // no effect
            e[1] = new CornerPathEffect(10);
            e[2] = new DashPathEffect(new float[] {10,5,5},phase);
            e[3] = new PathDashPathEffect(makePathDash(),12,phase,PathDashPathEffect.Style.MORPH);
            e[4] = new ComposePathEffect(e[2],e[1]);
            e[5] = new ComposePathEffect(e[3],e[1]);
        }

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

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

            mPath = makeFollowPath();

            mEffects = new PathEffect[6];

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

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

            RectF bounds = new RectF();
            mPath.computeBounds(bounds,false);
            canvas.translate(10 - bounds.left,10 - bounds.top);

            makeEffects(mEffects,mPhase);
            invalidate();

            for (int i = 0; i < mEffects.length; i++) {
                mPaint.setPathEffect(mEffects[i]);
                mPaint.setColor(mColors[i]);
                canvas.drawPath(mPath,mPaint);
                canvas.translate(0,28);
            }
        }

        @Override public boolean onKeyDown(int keyCode,KeyEvent event) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_DPAD_CENTER:
                    mPath = makeFollowPath();
                    return true;
            }
            return super.onKeyDown(keyCode,event);
        }

        private static Path makeFollowPath() {
            Path p = new Path();
            p.moveTo(0,0);
            for (int i = 1; i <= 15; i++) {
                p.lineTo(i*20,(float)Math.random() * 35);
            }
            return p;
        }

        private static Path makePathDash() {
            Path p = new Path();
            p.moveTo(-6,4);
            p.lineTo(6,3);
            p.lineTo(-6,3);
            p.close();
            p.moveTo(-6,-4);
            p.lineTo(6,-3);
            p.lineTo(-6,-3);
            return p;
        }
    }
}
原文链接:/android/317715.html

猜你在找的Android相关文章