Wrong size from Drawable.getIntrinsicWidth()
2013-02-05 23:42
579 查看
I download image with this code:ImageGetter imageGetter =newImageGetter(){@OverridepublicDrawable getDrawable(String source){Drawable drawable =null;try{ URL url =new URL(source);String path =Environment.getExternalStorageDirectory().getPath()+"/Android/data/com.my.pkg/"+url.getFile();File f=newFile(path);if(!f.exists()){URLConnection connection = url.openConnection();InputStreamis= connection.getInputStream(); f=newFile(f.getParent()); f.mkdirs();FileOutputStream os =newFileOutputStream(path);byte[] buffer =newbyte[4096];int length;while((length =is.read(buffer))>0){ os.write(buffer,0, length);} os.close();is.close();} drawable =Drawable.createFromPath(path);}catch(MalformedURLException e){ e.printStackTrace();}catch(Throwable t){ t.printStackTrace();}if(drawable !=null){ drawable.setBounds(0,0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());}return drawable;}}; This image has size 20x20. But drawable.getIntrinsicWidth() and drawable.getIntrinsicHeight() return 27. And image looks larger. How I can fix it? android image
| |||
feedback |
1 Answer
BitmapDrawable must scale bitmap to compensate for different screen densities. If you need it to draw pixel-for-pixel, try setting Drawable's source density & target density to same value. To do this, you need slightly different objects to work with. Instead of drawable =Drawable.createFromPath(path); use Bitmap bmp =BitmapFactory.decodeFile(path);DisplayMetrics dm = context.getResources().getDisplayMetrics(); bmp.setDensity(dm.densityDpi); drawable =newBitmapDrawable(bmp, context.getResources()); If you don't have context (which you should), you can use application context, see e.g. Using Application context everywhere? Since bitmap's density is set to resources' density, which is actual device's screen's density, it should draw without scaling. |
相关文章推荐
- Drawable之getIntrinsicWidth()和getIntrinsicHeight()?
- Drawable的getIntrinsicHeight()和getIntrinsicWidth()
- Drawable的getIntrinsicHeight()和getIntrinsicWidth()
- Android Drawable之getIntrinsicWidth()和getIntrinsicHeight()
- Drawable之getIntrinsicWidth()和getIntrinsicHeight()
- Drawable getIntrinsicWidth() and getIntrinsicHeight()
- 通过new BitmapDrawable()方法将Bitmap转化为Drawable的getIntrinsicHeight()和getIntrinsicWidth() 问题。
- Android : Drawable之getIntrinsicWidth()和getIntrinsicHeight()
- getintrinsicwidth()/getIntrinsicHeight()大于或者小于图片实际像素大小
- how to get the default maximum heap size for Sun's JVM from Java SE 6 or 7
- 关于BitMap中getWidth()与系统如何加载Drawable资源的问题
- 关于android Drawable对象 getIntrinsicHeight获取高度变化的问题
- getimagesizefromstring
- driver.manage().window().getSize().width 获取屏幕分辨率报错问题
- [日常问题] E/OpenGLRenderer: getCachedTexture Texture got from cache, but check failed(4). bitmap(width=
- How can i specify the resource to get from R.res.drawable dynamically?
- 获取显示窗口警告 android The method getWidth() from the type Display is deprecated
- Android getIntrinsicWidth()获取到的值在不同的项目不同
- Get the window data(lamda,flux, true) from a center point and the window width
- Andriod: The method getDrawable(int) from the type Resources is deprecated,一系列问题