java - Android - createScaledBitmap 问题

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

如果 URL 无效,它会从可绘制文件夹中获取 75 * 75 像素的默认图像。

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

所以我猜问题出在屏幕密度上。我需要图像在所有屏幕上都是 75 * 75 像素。我该如何解决这个问题?

解决方案 - 使用 DisplayMetrics。将 dp 转换为像素。获取默认图像的密度并将像素值除以图像的 dpi 值(在本例中它位于 hdpi 文件夹中,因此为 1.5)

public float convertDpToPixel(float dp, Activity context)

{ 资源 resources = context.getResources(); DisplayMetrics 指标 = resources.getDisplayMetrics(); float px = dp * (metrics.densityDpi/160f)/1.5f; 返回像素;

最佳答案

使用以下方法:-

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

函数 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?

关于java - Android - createScaledBitmap 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23389305/

相关文章:

java - 将数据存储在动态二维数组中

android - 使用 JavaMail API 在 Android 中发送电子邮件

android - 找不到资源 ID?登录猫

java - 错误无法将字符串转换为整数

java - 查找 graphics2D 的 Android 等效代码

android - 如何在TextView上显示bmp颜色值

java - android studio Viewpager OnClickListener 不工作

java - 将 Excel 模板与 Apache POI 结合使用

java - 如何在 Java 中创建 Type 5 UUID?

java - Android BitmapFactory.decodeFile skia 错误