Android简单的油漆

时间:2013-11-18 08:42:18

标签: android paint draw

为什么它不能正常工作?我正在试图“画画”我把手指放在哪里写东西。例如,当我试图制作一个C时,他看起来像这样:http://postimg.org/image/5obyif4o1/ ............................. ...........................................

我认为touch_move函数中的mX和mY与touch_start保持不变。

public class BlackPixel extends View implements OnTouchListener{
    private Bitmap  mBitmap;
    Canvas mCanvas=new Canvas();
    Path    mPath=new Path();
    Paint   mBitmapPaint=new Paint(Paint.DITHER_FLAG);  
    Paint mPaint=new Paint();
    Paint circlePaint=new Paint();
    Path circlePath = new Path();

    Context context;

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

    }


    public BlackPixel(Context context) {
        super(context);
        setFocusable(true);     
        setFocusableInTouchMode(true);
        this.setOnTouchListener(this);
    }



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

        canvas.drawBitmap( mBitmap, 0, 0, mBitmapPaint);

        canvas.drawPath( mPath, mPaint );

        canvas.drawPath( circlePath,circlePaint  );
    }



//  @Override
    public boolean onTouch(View v, MotionEvent event) {

        float x=event.getX();
        float y=event.getY();


           int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x,y);
                invalidate();
                return true;                

            case MotionEvent.ACTION_MOVE:
                touch_move(x,y);             
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up(x,y);
                invalidate();
                break;              
            }
            return true;
    }


    private void touch_start(float x, float y)
    {
           mPath.reset();
           mPath.moveTo(x, y);
           mX = x;
           mY = y;

    }

    private float mX,mY;
    private static final float TOUCH_TOLERANCE = 4;


    private void touch_move(float x, float y)  {

          float dx = Math.abs(x - mX);
          float dy = Math.abs(y - mY);
          if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
              mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
              mX = x;
              mY = y;
          }
    }


    private void touch_up(float x , float y)
    {
          mPath.lineTo(mX, mY);
          // commit the path to our offscreen
          mCanvas.drawPath(mPath, mPaint);
          // kill this so we don't double draw
          mPath.reset();

    }



    class Point
    {
        float x,y;
    }

1 个答案:

答案 0 :(得分:0)

quadTo(float x1, float y1, float x2, float y2) 从最后一个点添加二次贝塞尔曲线,接近控制点(x1,y1),并以(x2,y2)结束。

也就是说,touch_move()中的控制点是(mX,mY)。你总是从(mX,mY)到((x + mX)/ 2,(y + mY)/ 2)画一条线。