本文介绍了在画布上绘制虚线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在画布上绘制虚线。我已经尝试过了:

  Paint dashPaint = new Paint 
dashPaint.setARGB(255,0,0,0);
dashPaint.setStyle(Paint.Style.STROKE);
dashPaint.setPathEffect(new DashPathEffect(new float [] {5,10,15,20},0));
canvas.drawLine(0,canvas.getHeight()/ 2,canvas.getWidth(),canvas.getHeight()/ 2,dashPaint);

它给了我一条简单的线条。



  canvas.drawLine ,canvas.getHeight()/ 2,canvas.getWidth(),canvas.getHeight()/ 2,dashPaint)


$ b b

这将画一行



解决方案

 路径mPath; 
mPath = new Path();
mPath.moveTo(0,h / 2);
mPath.quadTo(w / 2,h / 2,w,h / 2);
h和w是屏幕的高度和宽度
paint mPaint = new Paint();
mPaint.setARGB(255,0,0,0);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setPathEffect(new DashPathEffect(new float [] {5,10,15,20},0));

在onDraw()

  canvas.drawPath(mPath,mPaint) 

快照





我有背景和虚线。



示例

  public class FingerPaintActivity extends Activity {
MyView mv;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mv = new MyView(this);
setContentView(mv);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xffFF0000);
mPaint.setARGB(255,0,0,0);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setPathEffect(new DashPathEffect(new float [] {10,40,},0));
mPaint.setStrokeWidth(12);
}

private Paint mPaint;

public class MyView extends View {
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
上下文上下文;

public MyView(Context c){
super(c);
context = c;
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}

@Override
protected void onSizeChanged(int w,int h,int oldw,int oldh){
super.onSizeChanged(w,h,oldw, oldh);
mBitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath.moveTo(0,h / 2);
mPath.quadTo(w / 2,h / 2,w,h / 2);
}

@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
canvas.drawPath(mPath,mPaint)
}
}
}

How can i draw dash line on a canvas. I already tried this:

Paint dashPaint = new Paint();
dashPaint.setARGB(255, 0, 0, 0);
dashPaint.setStyle(Paint.Style.STROKE);
dashPaint.setPathEffect(new DashPathEffect(new float[]{5, 10, 15, 20}, 0));
canvas.drawLine(0, canvas.getHeight() / 2, canvas.getWidth(), canvas.getHeight() / 2, dashPaint);

And it gave me not dash line but a simple one.

解决方案

You are drawing a line

 canvas.drawLine(0, canvas.getHeight() / 2, canvas.getWidth(), canvas.getHeight() / 2, dashPaint)

This will draw a line

Solution

       private Path    mPath;
       mPath = new Path();
       mPath.moveTo(0, h / 2);
       mPath.quadTo(w/2, h/2, w, h/2);
       h and w are height and width of the screen
       Paint mPaint = new Paint();
       mPaint.setARGB(255, 0, 0, 0);
       mPaint.setStyle(Paint.Style.STROKE);
       mPaint.setPathEffect(new DashPathEffect(new float[]{5, 10, 15, 20}, 0));

In onDraw()

       canvas.drawPath(mPath, mPaint);

Snap shot

I have background and dashed line drew over it.

Example

public class FingerPaintActivity extends Activity {
    MyView mv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mv = new MyView(this);
        setContentView(mv);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFFFF0000);
        mPaint.setARGB(255, 0, 0, 0);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setPathEffect(new DashPathEffect(new float[]{10, 40,}, 0));
        mPaint.setStrokeWidth(12);
    }

    private Paint mPaint;

    public class MyView extends View {
        private Bitmap mBitmap;
        private Canvas mCanvas;
        private Path mPath;
        private Paint mBitmapPaint;
        Context context;

        public MyView(Context c) {
            super(c);
            context = c;
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
            mPath.moveTo(0, h / 2);
            mPath.quadTo(w / 2, h / 2, w, h / 2);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
            canvas.drawPath(mPath, mPaint);
        }
    }
}

Modify the above according to your needs.

这篇关于在画布上绘制虚线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 16:17