图像不适合画布android

时间:2015-03-29 08:17:07

标签: android canvas bitmap

我使用以下代码将位图图像绘制到画布中:位图来自用户厨房

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中的图像视图一样。

0 个答案:

没有答案