我使用以下代码将位图图像绘制到画布中:位图来自用户厨房
public class CanvasView extends View {
Bitmap canvasBitmap;
Canvas drawCanvas;
public CanvasView(Context context,AttributeSet attrs) {
super(context,attrs);
}
public void setCanvasPath(String bitmap_path) {
BitmapFactory.Options decode_options = new BitmapFactory.Options();
decode_options.inMutable = true;
canvasBitmap = BitmapFactory.decodeFile(bitmap_path,decode_options);
drawCanvas = new Canvas(canvasBitmap);
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (canvasBitmap != null) {
Matrix matrix=new Matrix();
Matrix m = drawCanvas.getMatrix();
RectF drawableRect = new RectF(0, 0, canvasBitmap.getWidth(), canvasBitmap.getHeight());
RectF viewRect = new RectF(0, 0, drawCanvas.getWidth(),drawCanvas.getHeight());
m.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
canvas.drawBitmap(canvasBitmap,m , null);
}
}
}
但是当我绘制位图时,它会离开画布视图,我不想拉伸图像,我想我的画布就像Android中的图像视图一样。