如何调整 Android 位图中的图像大小?
How to resize the image in Android bitmap?
我想在类似于下图的图像上显示心形图像:
我尝试了 createScaledBitmap
但它不起作用。这是我的代码:
@Override
public Bitmap transform(Bitmap bitmap) {
// TODO Auto-generated method stub
synchronized (ImageTransform.class) {
if (bitmap == null) {
return null;
}
Bitmap resultBitmap = bitmap.copy(bitmap.getConfig(), true);
Bitmap bitmapImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_heart);
Bitmap.createScaledBitmap(
bitmapImage, 1, 1, true);
Canvas canvas = new Canvas(resultBitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(40);
paint.setShadowLayer(2.0f, 1.0f, 1.0f, Color.BLACK);
canvas.drawText("0", 10, 400, paint);
canvas.drawBitmap(bitmapImage, 460, 45, null);
bitmap.recycle();
return resultBitmap;
}
}
图像未缩放我可以看到非常大的图像。上面的代码在 Transformation
class of Picasso
.
为什么要缩放位图图像?
让我们检查一下您的代码:
Bitmap resultBitmap = bitmap.copy(bitmap.getConfig(), true);
好的,这是将添加壁炉的位图
Bitmap bitmapImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_heart);
您在此处阅读您的壁炉图像,以便稍后将其添加到 "main" 图片中
Bitmap.createScaledBitmap(
bitmapImage, 1, 1, true);
这是多余的调用,因为你在这里省略了方法调用的结果...
Canvas canvas = new Canvas(resultBitmap);
这里我们得到了新的canvas,带有用于修改的可变位图
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(40);
paint.setShadowLayer(2.0f, 1.0f, 1.0f, Color.BLACK);
canvas.drawText("0", 10, 400, paint);
你在这里画价就可以了
canvas.drawBitmap(bitmapImage, 460, 45, null);
您在此处绘制了您从资源中读取的位图图像没有修改
bitmap.recycle();
这是 Android 3.0
的冗余
return resultBitmap;
Return 你的图片...
如您所见,您有一个方法调用:Bitmap.createScaledBitmap(bitmapImage, 1, 1, true);
这实际上什么都不做。将其替换为:bitmapImage = Bitmap.createScaledBitmap(bitmapImage, 1, 1, true);
应该没问题。
如果你想在这里优化你的内存使用(因为你在这里创建了 3 个位图而不是一个)阅读 THIS ARTICLE。希望有所帮助:)
我想在类似于下图的图像上显示心形图像:
我尝试了 createScaledBitmap
但它不起作用。这是我的代码:
@Override
public Bitmap transform(Bitmap bitmap) {
// TODO Auto-generated method stub
synchronized (ImageTransform.class) {
if (bitmap == null) {
return null;
}
Bitmap resultBitmap = bitmap.copy(bitmap.getConfig(), true);
Bitmap bitmapImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_heart);
Bitmap.createScaledBitmap(
bitmapImage, 1, 1, true);
Canvas canvas = new Canvas(resultBitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(40);
paint.setShadowLayer(2.0f, 1.0f, 1.0f, Color.BLACK);
canvas.drawText("0", 10, 400, paint);
canvas.drawBitmap(bitmapImage, 460, 45, null);
bitmap.recycle();
return resultBitmap;
}
}
图像未缩放我可以看到非常大的图像。上面的代码在 Transformation
class of Picasso
.
为什么要缩放位图图像?
让我们检查一下您的代码:
Bitmap resultBitmap = bitmap.copy(bitmap.getConfig(), true);
好的,这是将添加壁炉的位图
Bitmap bitmapImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_heart);
您在此处阅读您的壁炉图像,以便稍后将其添加到 "main" 图片中
Bitmap.createScaledBitmap(
bitmapImage, 1, 1, true);
这是多余的调用,因为你在这里省略了方法调用的结果...
Canvas canvas = new Canvas(resultBitmap);
这里我们得到了新的canvas,带有用于修改的可变位图
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(40);
paint.setShadowLayer(2.0f, 1.0f, 1.0f, Color.BLACK);
canvas.drawText("0", 10, 400, paint);
你在这里画价就可以了
canvas.drawBitmap(bitmapImage, 460, 45, null);
您在此处绘制了您从资源中读取的位图图像没有修改
bitmap.recycle();
这是 Android 3.0
的冗余 return resultBitmap;
Return 你的图片...
如您所见,您有一个方法调用:Bitmap.createScaledBitmap(bitmapImage, 1, 1, true);
这实际上什么都不做。将其替换为:bitmapImage = Bitmap.createScaledBitmap(bitmapImage, 1, 1, true);
应该没问题。
如果你想在这里优化你的内存使用(因为你在这里创建了 3 个位图而不是一个)阅读 THIS ARTICLE。希望有所帮助:)