对于一个小项目,我必须编写一个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”只是一个空的Bitmap,“picture”保存在“mCanvas”中。
那么有没有办法在Bitmap中转换画线?
答案 0 :(得分:0)
您无需为此创建mBitmap
或mCanvas
。您只需致电View.getDrawingCache()
即可获取View
的内容。 More info here
但是如果您打算使用自己的位图进行绘制,则需要mBitmap
和mCanvas
。请按照以下步骤完成此操作:
将以下行移动到构造函数中,因为您只想创建一次位图和画布。
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
在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);
}
永远记住,每个视图都有一个底层位图(共享)。您在canvas
中获得的onDraw()
将帮助您在基础位图中绘制内容。如果要绘制自己的位图,则需要创建位图和画布,并使用您创建的画布进行绘制,而不是onDraw()
中提供给您的画布。