【发布时间】:2016-06-28 18:15:01
【问题描述】:
我需要显示存储在数据库中的图像,但图像(位图)的宽度/高度和 ImageView 存在问题...
仅用于测试 - 当我在项目的可绘制对象中添加相同的图像时,我可以使用它:
作品:
Bitmap b = BitmapFactory.decodeResource(context.getResources(), R.drawable.menu_image);
BitmapDrawable bd = new BitmapDrawable(context.getResources(), b);
imageView.setImageDrawable(bd);
同
imageView.setImageResource(R.drawable.menu_image);
以下不起作用,因为图片未调整大小:
imageView.setImageBitmap(image);
同
imageView.setImageDrawable(new BitmapDrawable(context.getResources(), image));
使用此构造图像的位置:
public static Bitmap base64ToBitmap(String b64) {
byte[] imageAsBytes = Base64.decode(b64.getBytes(), Base64.DEFAULT);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDensity = context.getResources().getDisplayMetrics().densityDpi;
options.inTargetDensity = context.getResources().getDisplayMetrics().densityDpi;
Bitmap bitmap = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length, options);
return bitmap;
}
图片原始尺寸为 338x94。
676x188,这是我使用项目可绘制目录中的图像时的图像大小。在这种情况下,这是我正在寻找的尺寸。我想一个快速的解决方法是使用 Bitmap.createScaledBitmap(),但我有几种不同的图像格式,我想使用 imageView.setImageBitmap 或 imageView.setImageDrawable 来表现我从项目的 drawables 目录加载 Bitmap 的行为。
【问题讨论】:
标签: android android-bitmap bitmapfactory