如何将fullHD图像设置为ImageView

时间:2016-01-28 07:07:28

标签: android

我想将全高清(1080x1920)图像设置为ImageView。 我搜索但没有有效的方法。

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                     int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

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;
    Log.d("DUE","Options height: " + height + " - width: " + width);
    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;
}

或者:

private Bitmap resize(int resId, int width, int height){

    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
    bmpFactoryOptions.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId, bmpFactoryOptions);

    int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
    int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

    if (heightRatio > 1 || widthRatio > 1)
    {
        if (heightRatio > widthRatio)
        {
            bmpFactoryOptions.inSampleSize = heightRatio;
        } else {
            bmpFactoryOptions.inSampleSize = widthRatio;
        }
    }else{
    }

    bmpFactoryOptions.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeResource(getResources(), resId, bmpFactoryOptions);
    return bitmap;
}

我希望在不缩小图像尺寸的情况下显示原始图像尺寸

你能帮助我吗?

全部谢谢!

3 个答案:

答案 0 :(得分:1)

void Main()
{
    var cache = new Dictionary<int, int>(1000000);
    var sw = Stopwatch.StartNew();

    for (var i = 0; i < 1000000; i++)
    {
        lock (cache)
        {
            int d;
            if (cache.TryGetValue(i, out d) == false)
            {
                cache.Add(i, i);
            }
        }
    }

    $"{sw.ElapsedMilliseconds:N0}ms".Dump();

    var sum = 0L;
    foreach (var kvp in cache)
    {
        sum += kvp.Value;
    }
    sum.Dump();
}

答案 1 :(得分:0)

您发布的两个示例都有助于缩小图像大小(即节省内存)。

  

我希望在不缩小图像尺寸的情况下显示原始图像尺寸

如果您只想从资源中设置图像,则可以使用更简单的替代方法。

将代码中的图像分配给ImageView:

ImageView img;
img.setImageResource(R.drawable.image);

或者从资源中创建一个位图:

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);

或者将图像设置为xml:

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:src="@drawable/image"
    android:scaleType="center"/>    

答案 2 :(得分:0)

您可以使用滑行

Glide是一个快速高效的Android开源媒体管理和图像加载框架,它将媒体解码,内存和磁盘缓存以及资源池包装成一个简单易用的界面。

full documantation

Downloading custom sizes with Glide 允许添加hight&amp; iamge的宽度

Glide的ModelLoader界面为开发人员提供了他们正在加载图像的视图大小,并允许他们使用该大小来选择下载适当大小的图像版本的URL。 使用适当大小的图像可以节省带宽,设备上的存储空间,并提高应用程序的性能。