【问题标题】:The quality of image in ImageViewImageView 中的图像质量
【发布时间】:2013-09-23 18:09:47
【问题描述】:

我在资产文件夹中有一个要显示的位图图像列表(使用 ViewPager)。我尝试根据屏幕尺寸(使用布局参数)设置图像的宽度和高度。但是图像质量会受到干扰。如何让画质更好?

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

这里,drawables[i]String[](比如 ball.bmp 和“Parts”是资产的子文件夹)。 现在,我已将 imageview 中的图像设置为,

imageView.setBackgroundDrawable(imageArray[position]);

图片在移动设备上看起来不错,但在标签中看起来被拉伸了。

【问题讨论】:

  • 你应该用新的宽度和高度转换你的位图对象

标签: android android-imageview bitmapimage


【解决方案1】:

试试这个;解码您的图像并找到正确的比例值:

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 2011-07-31
    相关资源
    最近更新 更多