Android - createScaledBitmap问题

时间:2014-04-30 13:22:46

标签: java android bitmapfactory

我使用以下代码:

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 = 75;
        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;
        o2.inScaled = false;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}



public Drawable getProfileImageJSON(String id) {
    String url = "http://developer-static.se-mc.com/wp-content/blogs.dir/1/files/2011/05/image_scaling_android.jpg";
    Bitmap b;
    MemoryCache memoryCache = new MemoryCache();

    try {

        Bitmap memCacheBitmap = memoryCache.get(url);

        if (memCacheBitmap != null)
            b = memCacheBitmap;

        else {
            FileCache fileCache = new FileCache(context);
            File f = fileCache.getFile(url);
            Bitmap fileCacheBitmap = decodeFile(f);

            if (fileCacheBitmap != null)
                b = fileCacheBitmap;
            else {
                InputStream is = (InputStream) new URL(url).getContent();
                OutputStream os = new FileOutputStream(f);
                Utils.CopyStream(is, os);
                os.close();
                b = decodeFile(f);
                memoryCache.put(url, b);
            }
        }

        b.setDensity(Bitmap.DENSITY_NONE);


        Drawable d = new BitmapDrawable(context.getResources(),
                Bitmap.createScaledBitmap(b, 75, 75, true));


        // Drawable d = Drawable.createFromStream(is, "src");

        if (d.getIntrinsicHeight() == 0 || d.getIntrinsicHeight() == -1) {
            d = context.getResources().getDrawable(
                    R.drawable.defaultprofile);
            return d;
        }

        else {
            return d;
        }

    }

    catch (Exception e) {
        // Drawable d2 = getResources().getDrawable( R.drawable.icon );


        Drawable d = context.getResources().getDrawable(
                R.drawable.defaultprofile);
        return d;
    }
}

如果网址无效,则会从drawable文件夹中获取75 * 75px的默认图片。

在URL有效的情况下 - 代码在Sony Xperia手机中显示75 * 75px图像。但是,谷歌Nexus手机的尺寸减小了(看起来像~50 * 50像素)

所以问题在于屏幕密度,我想。我需要在所有屏幕上将图像设置为75 * 75像素。我该如何解决这个问题?

解决方案 - 使用DisplayMetrics.Convert dp到像素。获取默认图像的密度,并将像素值除以图像的dpi值(在这种情况下,它位于hdpi文件夹中,因此为1.5)

public float convertDpToPixel(float dp, Activity context)

{     Resources resources = context.getResources();     DisplayMetrics metrics = resources.getDisplayMetrics();     float px = dp *(metrics.densityDpi / 160f)/1.5f;     返回px; }

1 个答案:

答案 0 :(得分:1)

使用以下方法: -

 Drawable d = new BitmapDrawable(context.getResources(),
                Bitmap.createScaledBitmap(b, ((int) convertDpToPixel(75)),  ((int) convertDpToPixel(75)), true));

function convertDpToPixel

public float convertDpToPixel(float dp, Activity context)
{
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return px;
}

您的静态值会根据每个设备的ppi而变化,因此请使用上面的代码。

更多信息: -

getting the screen density programmatically in android?