在画布中绘制时如何调整位图的大小? [英] How to resize bitmap when drawing in canvas?

查看:466
本文介绍了在画布中绘制时如何调整位图的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的android应用中,我具有此功能,可以通过画布从旧的位图创建新的位图.

In my android app, I have this function that creates a new bitmap from an old one via canvas.

private static Bitmap convert(Bitmap bitmap, Bitmap.Config config, int width, int height) {
    Bitmap convertedBitmap = Bitmap.createBitmap(width, height, config);
    Canvas canvas = new Canvas(convertedBitmap);
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    bitmap.recycle();
    return convertedBitmap;
}

问题是当我将旧位图绘制到画布上时,由于旧位图的尺寸大于画布,因此只能在画布上绘制位图的左上部分.有什么方法可以使它在画布上绘制位图时缩放位图,使其完全适合画布吗?

The problem is when I draw the old bitmap onto the canvas, since the old bitmap is bigger in dimensions than the canvas, only the top left part of the bitmap gets drawn on the canvas. Is there a way I can make it so when it draws the bitmap on the canvas, it scales the bitmap so it fits perfectly in the canvas?

我不想使用createScaledBitmap调整位图的大小.有没有更快的方法?

I don't want to resize the bitmap using createScaledBitmap. Is there a faster way?

或者我可以做createScaledBitmap但同时使其可变吗?这就是我要整体上进行的工作,同时调整大小和使其可变.

OR Can I do createScaledBitmap but make it mutable at the same time? That is what I am trying to do overall, resize and make mutable at same time.

谢谢

推荐答案

您可以为此功能调用public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint)(

You can call public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint) for this function (Android Doc). The code below should accomplish what you are trying to do:

canvas.drawBitmap(bitmap, null, new RectF(0, 0, canvasWidth, canvasHeight), null);

这篇关于在画布中绘制时如何调整位图的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆