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

作者:lee0oo0 时间:2022-05-20 03:37:34 

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

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


private void getWidth_Height() {
 Display display = getWindowManager().getDefaultDisplay();
 int width = display.getWidth(); // deprecated
 int height = display.getHeight(); // deprecated
 Bitmap mBitmap = createImageWithResouce(R.drawable.history4);
 image.setLayoutParams(new LayoutParams(width, width / getBitmapWidth(mBitmap) * getBitmapHeight(mBitmap)));
 image.setImageBitmap(createImageWithResouce(R.drawable.history4));
}
private Bitmap createImageWithResouce(int resourceID) {
 Bitmap bit = BitmapFactory.decodeResource(getResources(), R.drawable.history4);
 return bit;
}
private int getBitmapWidth(Bitmap bitmap){
 return bitmap.getWidth();
}
private int getBitmapHeight(Bitmap bitmap){
 return bitmap.getHeight();
}
// 释放bitmap
private void releaseBitmap(Bitmap bitmap){
 if (bitmap!=null && !bitmap.isRecycled()) {
  bitmap.recycle();
  bitmap = null;
 }
}

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


public class ImageUtil {
private LruCache<String, Bitmap> mMemoryCache;
private final Context mContext;
private static ImageUtil imageUtil;
private static Object obj = new Object();
private int memClass;
private int cacheSize;
private ImageUtil(Context mContext) {
 this.mContext = mContext;
 createLruCache(mContext);
}
private void createLruCache(Context mContext) {
 memClass = ((ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
 cacheSize = 1024 * 1024 * memClass / 8;
 mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
  @Override
  protected int sizeOf(String key, Bitmap value) {
   // TODO Auto-generated method stub
   return value.getRowBytes();
  }
 };
}
public static ImageUtil getInstance(Context mContext) {
 if (imageUtil == null) {
  synchronized (obj) {
   if (imageUtil == null) {
    imageUtil = new ImageUtil(mContext);
   }
  }
 }
 return imageUtil;
}
public void adjustImageSize(ImageView imageView, int imageResourceId) {
 Bitmap mBitmap = null;
 Display display = ((MainActivity) mContext).getWindowManager().getDefaultDisplay();
 int width = display.getWidth(); // deprecated
 int height = display.getHeight(); // deprecated
 Bitmap bitmapCache = mMemoryCache.get(imageResourceId + "");
 if (bitmapCache != null) {
  mBitmap = bitmapCache;
 } else {
  mBitmap = createImageWithResouce(mContext, imageResourceId);
  mMemoryCache.put(imageResourceId + "", mBitmap);
 }
 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, width
   / getBitmapWidth(mBitmap) * getBitmapHeight(mBitmap));
 layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
 imageView.setLayoutParams(layoutParams);
 imageView.setBackgroundDrawable(new BitmapDrawable(mBitmap));
 // imageView.setImageBitmap(mBitmap);
}
private static Bitmap createImageWithResouce(Context context, int resourceID) {
 Bitmap bit = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
 return bit;
}
private int getBitmapWidth(Bitmap bitmap) {
 return bitmap.getWidth();
}
private int getBitmapHeight(Bitmap bitmap) {
 return bitmap.getHeight();
}
}

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

标签:Android,图片
0
投稿

猜你喜欢

  • C# 根据表格偶数、奇数加载不同颜色

    2022-01-19 11:46:05
  • java线程之用Thread类创建线程的方法

    2023-02-09 18:35:07
  • Android 8.0升级不跳转应用安装页面的解决方法

    2022-03-20 03:30:13
  • Java带返回值的方法的定义和调用详解

    2022-10-05 21:01:09
  • Java的内部类总结

    2022-08-13 01:33:41
  • flutter中的资源和图片加载示例详解

    2023-08-24 13:19:39
  • springboot项目部署在linux上运行的两种方式小结

    2022-09-17 11:07:37
  • Redis之GEO存储地理位置信息的使用

    2023-12-22 14:29:49
  • spring boot中使用@Async实现异步调用任务

    2023-04-25 13:35:18
  • 模仿美团点评的Android应用中价格和购买栏悬浮固定的效果

    2021-11-12 12:00:42
  • java 实现定时的方法及实例代码

    2023-03-31 22:48:41
  • 浅谈Mybatis分页插件,自定义分页的坑

    2021-12-15 01:28:20
  • SpringBoot服务开启后通过端口访问无反应的解决

    2022-12-11 03:57:17
  • springboot整合shiro多验证登录功能的实现(账号密码登录和使用手机验证码登录)

    2023-05-25 18:53:29
  • spring整合redis缓存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用

    2022-04-25 01:46:14
  • Java中synchronized关键字修饰方法同步的用法详解

    2022-03-05 07:11:22
  • Android重写View实现全新的控件

    2021-08-11 21:49:39
  • jenkins+maven+svn自动部署和发布的详细图文教程

    2023-08-24 05:18:07
  • Android使用Intent的Action和Data属性实现点击按钮跳转到拨打电话和发送短信界面

    2021-09-04 15:39:22
  • SpringRetry重试框架的具体使用

    2022-09-21 00:37:35
  • asp之家 软件编程 m.aspxhome.com