Android編程實(shí)現(xiàn)等比例顯示圖片的方法
本文實(shí)例講述了Android編程實(shí)現(xiàn)等比例顯示圖片的方法。分享給大家供大家參考,具體如下:
在android中,由于密度的影響,如果想得到圖片的寬高是不行的,具體為什么我就大概說(shuō)一下,具體的請(qǐng)搜索度娘或者古哥吧。 原因是如果你把圖片放在drawable-mdpi里,而手機(jī)是屬于drawable-hdpi的話,圖片是被自動(dòng)放大,就這樣取到的寬與高未必就是正確的。那么如何讓android上面顯示的圖片是基于原來(lái)圖片的比例呢,首先你可以在res目錄下創(chuàng)建一個(gè)drawable-nodpi的目錄,這個(gè)目錄下的圖片是不根據(jù)dpi的多少來(lái)進(jìn)行拉伸或者縮小滴。然后,就是根據(jù)屏幕的寬 和 圖片的寬高 得出圖片在屏幕顯示的高,寬是固定的,就是屏幕的寬,所以不用算了。
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;
}
}
建議使用如下的這種,應(yīng)用了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();
}
}
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android使用AsyncTask下載圖片并顯示進(jìn)度條功能
- Android實(shí)現(xiàn)多線程下載圖片的方法
- Android使用okHttp(get方式)下載圖片
- Android中使用HttpURLConnection實(shí)現(xiàn)GET POST JSON數(shù)據(jù)與下載圖片
- Android編程實(shí)現(xiàn)圖片的上傳和下載功能示例
- Android中使用七牛云存儲(chǔ)進(jìn)行圖片上傳下載的實(shí)例代碼
- Android使用緩存機(jī)制實(shí)現(xiàn)文件下載及異步請(qǐng)求圖片加三級(jí)緩存
- Android sdcard實(shí)現(xiàn)圖片存儲(chǔ) 、聯(lián)網(wǎng)下載
- Android編程滑動(dòng)效果之Gallery+GridView實(shí)現(xiàn)圖片預(yù)覽功能(附demo源碼下載)
- Android編程實(shí)現(xiàn)手繪及保存為圖片的方法(附demo源碼下載)
- Android 利用ViewPager實(shí)現(xiàn)圖片可以左右循環(huán)滑動(dòng)效果附代碼下載
- Android顯示網(wǎng)絡(luò)圖片實(shí)例
- Android編程實(shí)現(xiàn)下載圖片及在手機(jī)中展示的方法
相關(guān)文章
Android中RecyclerView實(shí)現(xiàn)多級(jí)折疊列表效果(二)
這篇文章主要給大家介紹了Android中RecyclerView實(shí)現(xiàn)多級(jí)折疊列表的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05
Android實(shí)現(xiàn)隨機(jī)生成驗(yàn)證碼
在登錄注冊(cè)軟件時(shí),經(jīng)常會(huì)要求填寫隨機(jī)驗(yàn)證碼,這篇文章為大家詳細(xì)主要介紹了Android實(shí)現(xiàn)隨機(jī)生成驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Android實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)攔截讀取功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)攔截讀取功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android音視頻之視頻采集(系統(tǒng)API預(yù)覽)
這篇文章主要為大家詳細(xì)介紹了Android音視頻之視頻采集,系統(tǒng)API預(yù)覽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android?ASM插樁探索實(shí)戰(zhàn)詳情
這篇文章主要介紹了Android?ASM插樁探索實(shí)戰(zhàn)詳情,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容戒殺,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
Android實(shí)現(xiàn)動(dòng)態(tài)高斯模糊背景效果
在現(xiàn)代 Android UI 中,動(dòng)態(tài)高斯模糊背景 常見(jiàn)于對(duì)話框或彈窗后面的模糊遮罩,相比靜態(tài)模糊圖,動(dòng)態(tài)模糊可隨著內(nèi)容滾動(dòng)或變化實(shí)時(shí)更新,使界面更具層次感與沉浸感,所以本文給大家介紹了Android實(shí)現(xiàn)動(dòng)態(tài)高斯模糊背景效果,需要的朋友可以參考下2025-04-04

