首页 > 系统 > 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. private Bitmap createImageWithResouce(int resourceID) { 
  9. Bitmap bit = BitmapFactory.decodeResource(getResources(), R.drawable.history4); 
  10. return bit; 
  11. private int getBitmapWidth(Bitmap bitmap){ 
  12. return bitmap.getWidth(); 
  13. private int getBitmapHeight(Bitmap bitmap){ 
  14. return bitmap.getHeight(); 
  15. // 释放bitmap 
  16. private void releaseBitmap(Bitmap bitmap){ 
  17. if (bitmap!=null && !bitmap.isRecycled()) { 
  18. bitmap.recycle(); 
  19. bitmap = null

建议使用如下的这种,应用了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. private void createLruCache(Context mContext) { 
  12. memClass = ((ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass(); 
  13. cacheSize = 1024 * 1024 * memClass / 8; 
  14. mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { 
  15. @Override 
  16. protected int sizeOf(String key, Bitmap value) { 
  17. // TODO Auto-generated method stub 
  18. return value.getRowBytes(); 
  19. }; 
  20. public static ImageUtil getInstance(Context mContext) { 
  21. if (imageUtil == null) { 
  22. synchronized (obj) { 
  23. if (imageUtil == null) { 
  24. imageUtil = new ImageUtil(mContext); 
  25. return imageUtil; 
  26. public void adjustImageSize(ImageView imageView, int imageResourceId) { 
  27. Bitmap mBitmap = null
  28. Display display = ((MainActivity) mContext).getWindowManager().getDefaultDisplay(); 
  29. int width = display.getWidth(); // deprecated 
  30. int height = display.getHeight(); // deprecated 
  31. Bitmap bitmapCache = mMemoryCache.get(imageResourceId + ""); 
  32. if (bitmapCache != null) { 
  33. mBitmap = bitmapCache; 
  34. else { 
  35. mBitmap = createImageWithResouce(mContext, imageResourceId); 
  36. mMemoryCache.put(imageResourceId + "", mBitmap); 
  37. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, width 
  38. / getBitmapWidth(mBitmap) * getBitmapHeight(mBitmap)); 
  39. layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
  40. imageView.setLayoutParams(layoutParams); 
  41. imageView.setBackgroundDrawable(new BitmapDrawable(mBitmap)); 
  42. // imageView.setImageBitmap(mBitmap); 
  43. private static Bitmap createImageWithResouce(Context context, int resourceID) { 
  44. Bitmap bit = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher); 
  45. return bit; 
  46. private int getBitmapWidth(Bitmap bitmap) { 
  47. return bitmap.getWidth(); 
  48. private int getBitmapHeight(Bitmap bitmap) { 
  49. return bitmap.getHeight(); 

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


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