Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中显示位图图像的最佳实践_Android_Image_Bitmap_Imageview - Fatal编程技术网

在Android中显示位图图像的最佳实践

在Android中显示位图图像的最佳实践,android,image,bitmap,imageview,Android,Image,Bitmap,Imageview,我对Android显示位图图像的最佳实践有点困惑。我想在屏幕上显示一个图像。我对图像的属性(如分辨率、高度、宽度等)一无所知。我想图像应该显示在任何设备和任何模式(端口或土地) 这就是我目前正在做的: 首先找出设备的高度和宽度 然后,如果相应地设置ImageView的高度和宽度。我的要求是,我想使用50%的屏幕来显示图像 最后使用下面的方法显示位图 private Bitmap checkBitmapDetails(){ Bitmap bitmap; BitmapFactory.

我对Android显示位图图像的最佳实践有点困惑。我想在屏幕上显示一个图像。我对图像的属性(如分辨率、高度、宽度等)一无所知。我想图像应该显示在任何设备和任何模式(端口或土地)

这就是我目前正在做的:

  • 首先找出设备的高度和宽度
  • 然后,如果相应地设置ImageView的高度和宽度。我的要求是,我想使用50%的屏幕来显示图像
  • 最后使用下面的方法显示位图

    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;
    }
    
  • 现在,如果源图像太大,我将首先缩小图像,然后调用Bitmap.createScaledBitmap将图像缩放到我喜欢的高度和宽度

    当我阅读开发者Android网站的最佳实践时,硬编码高度和宽度是不好的。我不是在做硬编码,而是在用编程的方式操作高度和宽度。你觉得我做的对吗

    我可以为可绘制hdpi、ldpi、mdpi、xhdpi、xxhdpi提供不同大小的图像。但是不同的可绘制图像的大小是多少


    请让我知道你认为什么是最好的方法。谢谢。

    您制作此图像的目的是什么?它总是一样的吗?如果不是,有多少?这是应用程序要下载的图像吗?为什么只有50%的屏幕?更多关于您实际用例的背景信息会很有帮助。与其缩放位图,为什么不使
    ImageView
    与所有设备兼容?你应该暂时避免混合设计和代码,我不是在下载图片。这意味着图像是静态的。图像总数:5。我的应用程序将在50%的屏幕和图像下方显示5张名人的照片(在纵向模式下),我将放置一个文本视图以显示此人的一些信息。@Anirudh:如何使ImageView与所有设备兼容?@SudiptaDeb