2013-05-13 17 views
15

Jak mogę narysować kreskę na płótnie. Próbowałem już:Rysuj linię kreskową na płótnie

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

I nie dałem mi prostej linii, ale prostej.

+0

Czy chcesz narysować linie przerywane za pomocą palca? – Raghunandan

+0

Napisałem widok, który rysuje linię kreski. Możesz zobaczyć szczegóły [tutaj] (http://stackoverflow.com/a/15492685/1251276) – ruidge

Odpowiedz

47

Jesteś rysując linię

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

Będzie to narysować linię

Rozwiązanie

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

W OnDraw()

 canvas.drawPath(mPath, mPaint); 

snap shot

enter image description here

Mam tło i narysowałem linię przerywaną.

Przykład

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

modyfikowanie powyższych w zależności od potrzeb.