Android编程实现等比例显示图片的方法
作者:lee0oo0 发布时间:2022-05-20 03:37:34
标签:Android,图片
本文实例讲述了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程序设计有所帮助。


猜你喜欢
- 背景Java8的stream接口极大地减少了for循环写法的复杂性,stream提供了map/reduce/collect等一系列聚合接口,
- 先上效果图,如果大家感觉不错,请参考实现代码。 重要的是如何实现自定义的
- 前言JAVA缓存实现方案有很多,最基本的自己使用Map去构建缓存,或者使用memcached或Redis,但是上述两种缓存框架都要搭建服务器
- 前言在之前学习如何使用MediaPlayer后,了解到Android系统提供开发者播放多媒体全家桶能力,但对于开发者希望DIY自由度更高的播
- 本文实例讲述了Java排序算法总结之希尔排序。分享给大家供大家参考。具体分析如下:前言:希尔排序(Shell Sort)是插入排序的一种。是
- 一、背景介绍:我们在进行数据存储的时候,有时候会加入本地缓存、分布式缓存以及数据库存储 * 的结构,当我们取值的时候经常是像下面这样的流程:1
- SqlssionFactory1.SqlSessionFactory是MyBatis的关键对象,它是个单个数据库映射关系经过编译后的内存镜像
- 下载Android SDK两种方式:(1)官网下载(需翻墙):https://developer.android.com/studio/in
- 声明类定义类:class MyClass { // 字段、构造函数和 // 方法声明}
- 一、线程间等待与唤醒机制wait()和notify()是Object类的方法,用于线程的等待与唤醒,必须搭配synchronized 锁来使
- 1. 前言Spring的核心技术IOC(Intorol of Converse控制反转)的实现途径是DI(dependency Insert
- 本文实例讲述了C#实现基于XML配置MenuStrip菜单的方法。分享给大家供大家参考。具体如下:1.关于本程序的说明用XML配置MenuS
- 多说无益,贴代码:/** * 校验银行卡卡号 * * @param cardId &nbs
- 在app中图片的轮播显示可以说是非常常见的实现效果了,其实现原理不过是利用ViewPager,然后利用handler每隔一定的时间将View
- 如果所有的键都是小整数,我们可以使用一个数组来实现无序的符号表,将键作为数组的索引而数组中键 i 处存储的就是它对应的值。散列表就是用来处理
- SpringSecurity基本原理在之前的文章《SpringBoot + Spring Security 基本使用及个性化登录配置》中对S
- 前言: Java 8已经公布有一段时间了,种种迹象表明Java 8是一个有重大改变的发行版。在Java Code Geeks上已经有很多介绍
- 今天从网上找了个例子实现了语音识别,个人感觉挺好玩的,就把代码贴出来与大家分享下: &nbs
- Note:这篇文章是基于Android Studio 3.01版本的,NDK是R16。step1:创建一个包含C++的项目其他默认就可以了。
- 本文实例讲述了Java数组高级算法与Arrays类常见操作。分享给大家供大家参考,具体如下:冒泡排序冒泡排序原理冒泡排序代码:package