使用此代码:
Drawable blankDrawable = context.getResources().getDrawable(image);
Bitmap blankBitmap=((BitmapDrawable)blankDrawable).getBitmap();
Run Code Online (Sandbox Code Playgroud)
我得到一个缩放到上下文密度的位图,保留位图的物理大小(基于其dpi值).例如,我有一个405x500位图(dpi = 96)作为资源.但是当我将它加载到设备上时,我得到一张密度= 240的608x750图像.我想加载位图而不进行缩放.我怎么做?
这个问题非常类似于:
但是,在我的情况下不能使用该解决方案,因为我没有输入流.我所拥有的只是一个资源ID,而getDrawable()方法没有密度参数.加载位图后,为时已晚 - 它已经调整大小.
谢谢.
Raj*_*ddy 28
用这个
InputStream is = this.getResources().openRawResource(imageId);
Bitmap originalBitmap = BitmapFactory.decodeStream(is);
imageview.setImageBitmap(originalBitmap);
Run Code Online (Sandbox Code Playgroud)
Chi*_*rry 15
使用时解码位图
BitmapFactory.decodeResource (Resources res, int id, BitmapFactory.Options opts)
Run Code Online (Sandbox Code Playgroud)
首先将BitmapFactory.Options中的标记inScaled设置为false.
例:
/* Set the options */
Options opts = new Options();
opts.inDither = true;
opts.inPreferredConfig = Bitmap.Config.RGB_565;
opts.inScaled = false; /* Flag for no scalling */
/* Load the bitmap with the options */
bitmapImage = BitmapFactory.decodeResource(context.getResources(),
R.drawable.imageName, opts);
Run Code Online (Sandbox Code Playgroud)
在 res 中创建一个可绘制的(没有 hdpi/mdpi 等)文件夹。将可绘制对象保留在该文件夹中。现在尝试一下。这可能对你有帮助。
| 归档时间: |
|
| 查看次数: |
28388 次 |
| 最近记录: |