简体   繁体   English

ImageView中的图像质量

[英]The quality of image in ImageView

I have a list of bitmap images in asset folder to display(using ViewPager). 我在资产文件夹中有一个要显示的位图图像列表(使用ViewPager)。 I tried to set the width and height of the image based on screen-size(using layout params). 我试图根据屏幕尺寸(使用布局参数)设置图像的宽度和高度。 But the image quality gets disturbed. 但是图像质量受到干扰。 How to make the image quality better? 如何使图像质量更好?

 Drawable drw = Drawable.createFromStream(getAssets().
                            open("Parts/"+drawables[i]),null);

Here, drawables[i] is the String[] (say like ball.bmp and "Parts" is the subfolder of asset). 在这里, drawables[i]String[] (例如ball.bmp,“ Parts”是资产的子文件夹)。 Now, I have set the image in imageview as, 现在,我在imageview中将图像设置为

imageView.setBackgroundDrawable(imageArray[position]);

The image looks good in mobile but looks stretched in tab. 该图片在移动设备上看起来不错,但在标签页中看起来很拉伸。

Try this; 尝试这个; decode your image and find the correct scale value: 解码图像并找到正确的比例值:

private Bitmap getBitmap(String url) {
    // from web
    try {
        Bitmap bitmap = null;
        URL imageUrl = new URL(url);

        HttpURLConnection conn = (HttpURLConnection) imageUrl
                .openConnection();
        conn.setConnectTimeout(1000);
        conn.setReadTimeout(1000);
        conn.setInstanceFollowRedirects(true);
        InputStream is = conn.getInputStream();
        OutputStream os = new FileOutputStream(f);
        Utils.CopyStream(is, os);
        os.close();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

// decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
    try {
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = 150;
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
                    || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM