如何在不失去图像质量的情况下将图像放在画布上

时间:2014-01-08 12:01:20

标签: android canvas bitmap

我在画布上绘制图像。但它在中心显示图像。但我想在整个画布上放置图像而不缩放图像。

@Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        canvas.drawBitmap(mBitmap, 
                 (BaseActivity.width / 2) - mBitmap.getWidth()/2, 
                 (BaseActivity.height / 2) - mBitmap.getHeight()/2,
                 mPaint);

        canvas.restore();
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        paint.setTextSize(12.0f);

    }

如果有人有想法。请回复。谢谢。

1 个答案:

答案 0 :(得分:0)

我希望你扩展View类...... 使用getWidth()getHeight()获取相应的尺寸并绘制位图...

    private Bitmap bitmap;
    private Paint mPaint;
    private Rect imageRect;


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(imageRect == null) { // I think it is always false as we are intializing it onSizeChanged
            imageRect = new Rect(0, 0, getWidth(), getHeight());
        }
        canvas.drawBitmap(bitmap, null, imageRect, mPaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        imageRect = new Rect(0, 0, w, h);
    }