首页 > 系统 > Android > 正文

Android编程实现等比例显示图片的方法

2019-10-24 20:34:13
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了Android编程实现等比例显示图片的方法,实例分析了Android等比例缩放图片的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Android编程实现等比例显示图片的方法。分享给大家供大家参考,具体如下:

在android中,由于密度的影响,如果想得到图片的宽高是不行的,具体为什么我就大概说一下,具体的请搜索度娘或者古哥吧。 原因是如果你把图片放在drawable-mdpi里,而手机是属于drawable-hdpi的话,图片是被自动放大,就这样取到的宽与高未必就是正确的。那么如何让android上面显示的图片是基于原来图片的比例呢,首先你可以在res目录下创建一个drawable-nodpi的目录,这个目录下的图片是不根据dpi的多少来进行拉伸或者缩小滴。然后,就是根据屏幕的宽 和 图片的宽高 得出图片在屏幕显示的高,宽是固定的,就是屏幕的宽,所以不用算了。

 

 
  1. private void getWidth_Height() { 
  2. Display display = getWindowManager().getDefaultDisplay(); 
  3. int width = display.getWidth(); // deprecated 
  4. int height = display.getHeight(); // deprecated 
  5. Bitmap mBitmap = createImageWithResouce(R.drawable.history4); 
  6. image.setLayoutParams(new LayoutParams(width, width / getBitmapWidth(mBitmap) * getBitmapHeight(mBitmap))); 
  7. image.setImageBitmap(createImageWithResouce(R.drawable.history4)); 
  8. } 
  9. private Bitmap createImageWithResouce(int resourceID) { 
  10. Bitmap bit = BitmapFactory.decodeResource(getResources(), R.drawable.history4); 
  11. return bit; 
  12. } 
  13. private int getBitmapWidth(Bitmap bitmap){ 
  14. return bitmap.getWidth(); 
  15. } 
  16. private int getBitmapHeight(Bitmap bitmap){ 
  17. return bitmap.getHeight(); 
  18. } 
  19. // 释放bitmap 
  20. private void releaseBitmap(Bitmap bitmap){ 
  21. if (bitmap!=null && !bitmap.isRecycled()) { 
  22. bitmap.recycle(); 
  23. bitmap = null; 
  24. } 
  25. } 

建议使用如下的这种,应用了LruCache作为管理

 

 
  1. public class ImageUtil { 
  2. private LruCache<String, Bitmap> mMemoryCache; 
  3. private final Context mContext; 
  4. private static ImageUtil imageUtil; 
  5. private static Object obj = new Object(); 
  6. private int memClass; 
  7. private int cacheSize; 
  8. private ImageUtil(Context mContext) { 
  9. this.mContext = mContext; 
  10. createLruCache(mContext); 
  11. } 
  12. private void createLruCache(Context mContext) { 
  13. memClass = ((ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass(); 
  14. cacheSize = 1024 * 1024 * memClass / 8; 
  15. mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { 
  16. @Override 
  17. protected int sizeOf(String key, Bitmap value) { 
  18. // TODO Auto-generated method stub 
  19. return value.getRowBytes(); 
  20. } 
  21. }; 
  22. } 
  23. public static ImageUtil getInstance(Context mContext) { 
  24. if (imageUtil == null) { 
  25. synchronized (obj) { 
  26. if (imageUtil == null) { 
  27. imageUtil = new ImageUtil(mContext); 
  28. } 
  29. } 
  30. } 
  31. return imageUtil; 
  32. } 
  33. public void adjustImageSize(ImageView imageView, int imageResourceId) { 
  34. Bitmap mBitmap = null; 
  35. Display display = ((MainActivity) mContext).getWindowManager().getDefaultDisplay(); 
  36. int width = display.getWidth(); // deprecated 
  37. int height = display.getHeight(); // deprecated 
  38. Bitmap bitmapCache = mMemoryCache.get(imageResourceId + ""); 
  39. if (bitmapCache != null) { 
  40. mBitmap = bitmapCache; 
  41. } else { 
  42. mBitmap = createImageWithResouce(mContext, imageResourceId); 
  43. mMemoryCache.put(imageResourceId + "", mBitmap); 
  44. } 
  45. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, width 
  46. / getBitmapWidth(mBitmap) * getBitmapHeight(mBitmap)); 
  47. layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
  48. imageView.setLayoutParams(layoutParams); 
  49. imageView.setBackgroundDrawable(new BitmapDrawable(mBitmap)); 
  50. // imageView.setImageBitmap(mBitmap); 
  51. } 
  52. private static Bitmap createImageWithResouce(Context context, int resourceID) { 
  53. Bitmap bit = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher); 
  54. return bit; 
  55. } 
  56. private int getBitmapWidth(Bitmap bitmap) { 
  57. return bitmap.getWidth(); 
  58. } 
  59. private int getBitmapHeight(Bitmap bitmap) { 
  60. return bitmap.getHeight(); 
  61. } 
  62. } 

希望本文所述对大家Android程序设计有所帮助。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表