Android简单绘制

时间:2013-11-17 17:20:59

标签: java android multithreading path draw

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

 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)

编辑:这反映了您在代码中所做的更改。

创建mPaint时,您没有设置其样式,默认情况下为FILL。将其更改为STROKE。您还可以设置笔触宽度。在你的构造函数中执行:

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

    //change paint style to STROKE
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeWidth(12);
}

另外,您可以在onSizeChanged中设置背景颜色:

@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);
    //set background color
    mBitmap.eraseColor(0xFFAAAAAA);
    mCanvas = new Canvas(mBitmap);
}

检查Android SDK示例文件夹中的FingerSample。