在Android中显示位图图像的最佳实践
我对Android显示位图图像的最佳实践有点困惑。我想在屏幕上显示一个图像。我对图像的属性(如分辨率、高度、宽度等)一无所知。我想图像应该显示在任何设备和任何模式(端口或土地) 这就是我目前正在做的:在Android中显示位图图像的最佳实践,android,image,bitmap,imageview,Android,Image,Bitmap,Imageview,我对Android显示位图图像的最佳实践有点困惑。我想在屏幕上显示一个图像。我对图像的属性(如分辨率、高度、宽度等)一无所知。我想图像应该显示在任何设备和任何模式(端口或土地) 这就是我目前正在做的: 首先找出设备的高度和宽度 然后,如果相应地设置ImageView的高度和宽度。我的要求是,我想使用50%的屏幕来显示图像 最后使用下面的方法显示位图 private Bitmap checkBitmapDetails(){ Bitmap bitmap; BitmapFactory.
private Bitmap checkBitmapDetails(){
Bitmap bitmap;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inScreenDensity = 0;
bitmap = BitmapFactory.decodeResource(getResources(),
whichImage, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, PREFERRED_WIDTH,
PREFERRED_HEIGHT);
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeResource(getResources(),
whichImage, options);
bitmap = Bitmap.createScaledBitmap(bitmap, PREFERRED_WIDTH, PREFERRED_HEIGHT, true);
return bitmap;
}
private int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and
// keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
请让我知道你认为什么是最好的方法。谢谢。您制作此图像的目的是什么?它总是一样的吗?如果不是,有多少?这是应用程序要下载的图像吗?为什么只有50%的屏幕?更多关于您实际用例的背景信息会很有帮助。与其缩放位图,为什么不使
ImageView
与所有设备兼容?你应该暂时避免混合设计和代码,我不是在下载图片。这意味着图像是静态的。图像总数:5。我的应用程序将在50%的屏幕和图像下方显示5张名人的照片(在纵向模式下),我将放置一个文本视图以显示此人的一些信息。@Anirudh:如何使ImageView与所有设备兼容?@SudiptaDeb