Android Studio | Java——用手指绘制位图



对于一个小项目,我必须编写一个Android应用程序,用手指在屏幕上画一条线。目前我使用这段代码

´
public class MainActivity extends Activity {
   DrawingView dv ;   
   private Paint       mPaint;

   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dv = new DrawingView(this);
    setContentView(dv);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(Color.GREEN);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(12);  
}
 public class DrawingView extends View {
    public int width;
    public  int height;
    private Bitmap  mBitmap;
    private Canvas  mCanvas;
    private Path    mPath;
    private Paint   mBitmapPaint;
    Context context;
    private Paint circlePaint;
    private Path circlePath;
    public DrawingView(Context c) {
    super(c);
    context=c;
    mPath = new Path();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);  
    circlePaint = new Paint();
    circlePath = new Path();
    circlePaint.setAntiAlias(true);
    circlePaint.setColor(Color.BLUE);
    circlePaint.setStyle(Paint.Style.STROKE);
    circlePaint.setStrokeJoin(Paint.Join.MITER);
    circlePaint.setStrokeWidth(4f); 

    }
    @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);
    }
    @Override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap( mBitmap, 0, 0, mBitmapPaint);
    canvas.drawPath( mPath,  mPaint);
    canvas.drawPath( circlePath,  circlePaint);
    }
    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;
    private void touch_start(float x, float y) {
    mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;
    }
    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;
        circlePath.reset();
        circlePath.addCircle(mX, mY, 30, Path.Direction.CW);
    }
    }
    private void touch_up() {
    mPath.lineTo(mX, mY);
    circlePath.reset();
    // commit the path to our offscreen
    mCanvas.drawPath(mPath,  mPaint);
    // kill this so we don't double draw
    mPath.reset();
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();
            break;
    }
    return true;
    }  
    }

}

有了这个,我可以在屏幕上画一条线,但我不知道如何将绘制的线转换为位图。我想我理解错了,但我认为"mBitmap"只是一个空位图,"图片"保存在"mCanvas"中。

那么,有什么方法可以转换位图中绘制的线条吗?

您不需要为此创建mBitmapmCanvas。您可以简单地调用View.getDrawingCache()来获取View的内容。点击此处了解更多信息。

但是,如果您计划绘制自己的位图,那么您需要有一个mBitmapmCanvas。按照以下步骤完成:

  1. 将以下行移到构造函数中,因为您只需要创建一次位图和画布。

    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);
    
  2. onDraw()方法中,使用mCanvas进行绘制,而不是使用canvas。最后在视图的画布上绘制mBitmap。如下所示:

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // This will use the mCanvas and draw the `path` in the mBitmap.
        mCanvas.drawPath( mPath,  mPaint);
        mCanvas.drawPath( circlePath,  circlePaint);
        // Now once everything is drawn on mBitmap, we will draw the contents of this mBitmap onto the underlying bitmap of the View via `canvas`.
        canvas.drawBitmap( mBitmap, 0, 0, mBitmapPaint);
    }
    

请永远记住,每个视图都有一个底层位图(共享)。onDraw()中的canvas将帮助您在底层位图中绘制内容。如果你想在自己的位图中绘制,那么你需要创建一个位图和一个画布,并使用你创建的画布而不是onDraw()中提供给你的画布进行绘制。

最新更新