在下载图像之前设置ImageView大小

时间:2013-12-27 15:38:25

标签: android imageview

我想重现Google Plus Android应用程序或9gag Android应用程序中尚未下载图像的图像加载,但ImageView已经具有宽度和高度。我考虑过发送带有URL的维度

{"image":{"url":"http://someurl.com/withanimage.jpg","width":"500","height":"300"}}

然后使用该宽度和高度在下载图像之前将大小设置为ImageView,但问题是Android有许多不同大小的屏幕和DPI,我真的没有找到方法去做这个。感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

answer看到调用BitmapFactory.decode 2X的'decodeFile()'。第一次只是获取文件的边界,您可以将其与设备上的可用像素进行比较,生成介导的BitmapImage大小,如下所示:

   public static 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) {
            if (width > height) {
                inSampleSize = Math.round((float)height / (float)reqHeight);
            } else {
                inSampleSize = Math.round((float)width / (float)reqWidth);
            }
        }
        return inSampleSize;

...

options.inSampleSize = calculateInSampleSize(options, metrics.widthPixels, metrics.heightPixels);

bmp_larg = BitmapFactory.decodeStream(getContentResolver().openInputStream(uris[0]),  null,  options);

answer花时间在这个项目上,看看如何根据设备配置ImageLoader。

相关问题