从资源图像正确缩放但SQLite的图像一滴不 - 的Andr​​oid [英] Image from resources is scaled properly but image blob from SQlite isn't - Android

查看:196
本文介绍了从资源图像正确缩放但SQLite的图像一滴不 - 的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的问题。我在我的Andr​​oid项目多张图片,我下水库\\绘制保存为.png文件。我能够很容易地在运行时提取图像并将其转换为这样的位图:

I am running into a strange issue. I have multiple images in my Android project which I stored as .png files under res\drawable. I was able to easily extract the images at runtime and convert them to a bitmap like this:

Drawable d = getResources().getDrawable(R.drawable.imageId);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();

这伟大工程和形象得到正确缩放不管设备具有的屏幕密度。我的所有图片均为200像素由200像素和我的形象布局配置为200浸×200畅游。

This works great and the image gets scaled correctly no matter what screen density the device has. All my images are 200 pixels by 200 pixels and my image layout is configured as 200 dip x 200 dip.

现在,我已存储的所有图像在SQLite数据库由于可扩展性问题的斑点,我在运行时提取它们并转换为位图是这样的:

Now, I have stored all images as blobs in an SQlite database due to scalability issues and I am extracting them at runtime and converting to a bitmap like this:

byte[] bb = cursor.getBlob(columnIndex);
Bitmap bitmap = BitmapFactory.decodeByteArray(bb, 0, bb.length);

图片显示细腻如果屏幕密度为标准160畅游。但是,如果密度为任何少跌多,图像不结垢,并保持200像素×200像素160畅游。因此,基本上,一个小屏幕(120 DIP),图像需要更多的空间比它应该和一个更大的屏幕(240 DIP)上,它需要较少的空间比它应该。

The image displays fine if the screen density is standard 160 dip. But if the density is any less or more, the image doesn't scale and remains 200 pixels x 200 pixels for 160 dip. So basically, on a smaller screen (120 dip), the image takes more space than it should and on a larger screen (240 dip), it takes less space than it should.

有没有其他人遇到了这个奇怪的问题?任何解释,解决方法,解决方案将是真正的AP preciated。

Has anyone else run into this bizarre issue? Any explanation, workaround, solution will be really appreciated.

感谢很多提前!

推荐答案

好吧,我终于得到它通过使用createScaledBitmap工作()。

Okay, I finally got it to work by using createScaledBitmap().

创建从斑的位图后,我计算缩放因子,然后计算新的宽度和高度。然后我用那些在createScaledBitmap()函数。这里的code的作品:

After creating a bitmap from the blob, I calculate the scaling factor and then calculate the new width and height. I then use those in the createScaledBitmap() function. Here's the code that works:

public static Bitmap getImageFromBlob(byte[] mBlob) 
{
    byte[] bb = mBlob;
    Bitmap b = BitmapFactory.decodeByteArray(bb, 0, bb.length); 
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int newWidth = b.getWidth()*metrics.densityDpi)/DisplayMetrics.DENSITY_DEFAULT;
    int newHeight = b.getHeight()*metrics.densityDpi)/DisplayMetrics.DENSITY_DEFAULT;
    return Bitmap.createScaledBitmap(b, newWidth, newHeight,  true);
}

这个链接提供的线索:
<一href=\"http://stackoverflow.com/questions/3976389/difference-between-methods-to-scale-a-bitmap\">difference方法之间缩放位图

This link provided the clue: difference between methods to scale a bitmap

这篇关于从资源图像正确缩放但SQLite的图像一滴不 - 的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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