安卓:发行图像分辨率 [英] Android:Issue Image resolution

查看:102
本文介绍了安卓:发行图像分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的问题,同时在设备使用高分辨率图像出现。

Problem occurs while using high resolution image in device.

 imageview a;
InputStream ims = getAssets().open("sam.png");//sam.png=520*1400 device=320*480 or 480*800
Drawable d=Drawable.createFromStream(ims, null);
a.setLayoutParams(new        LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
a.setImageDrawable(d);

通过上面的code图像叶顶部和底部的下一个内容空间或者如果我通过给固定的PX其规模,其get模糊图像缩小图像。无论如何,以解决这一问题?

by using above code image leaves spaces on top and bottom to next contents or If I shrink an image by giving fixed px, its get blur image on its size. Anyway to solve this issue?

推荐答案

希望下面的解决方案帮助。 你可以让你的固定大小的ImageView的和ImageView的宽度和高度传递给 calculateInSampleSize 方法。基于图像大小,将决定是否向下采样的图像或不

Hope following solution helps. You can make your imageView of fixed size and pass that imageView's width and height to calculateInSampleSize method. Based on image size, it will decide whether to down sample the image or not.

public Bitmap getBitmap(Context context, final String imagePath)
{
    AssetManager assetManager = context.getAssets();
    InputStream inputStream = null;
    Bitmap bitmap = null;
    try
    {
        inputStream = assetManager.open(imagePath);         

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = true;
        options.inJustDecodeBounds = true;

        // First decode with inJustDecodeBounds=true to check dimensions
        bitmap = BitmapFactory.decodeStream(inputStream);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight);

        options.inJustDecodeBounds = false;

        bitmap = BitmapFactory.decodeStream(inputStream);
    }
    catch(Exception exception) 
    {
        exception.printStackTrace();
        bitmap = null;
    }

    return bitmap;
}


public int calculateInSampleSize(BitmapFactory.Options options, final int requiredWidth, final int requiredHeight) 
{
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if(height > requiredHeight || width > requiredWidth) 
    {
        if(width > height) 
        {
            inSampleSize = Math.round((float)height / (float)requiredHeight);
        } 
        else 
        {
            inSampleSize = Math.round((float)width / (float)requiredWidth);
        }
    }

    return inSampleSize;
}

这篇关于安卓:发行图像分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆