Android編程實(shí)現(xiàn)等比例顯示圖片的方法
本文實(shí)例講述了Android編程實(shí)現(xiàn)等比例顯示圖片的方法。分享給大家供大家參考,具體如下:
在android中,由于密度的影響,如果想得到圖片的寬高是不行的,具體為什么我就大概說一下,具體的請(qǐng)搜索度娘或者古哥吧。 原因是如果你把圖片放在drawable-mdpi里,而手機(jī)是屬于drawable-hdpi的話,圖片是被自動(dòng)放大,就這樣取到的寬與高未必就是正確的。那么如何讓android上面顯示的圖片是基于原來圖片的比例呢,首先你可以在res目錄下創(chuàng)建一個(gè)drawable-nodpi的目錄,這個(gè)目錄下的圖片是不根據(jù)dpi的多少來進(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實(shí)現(xiàn)原生鎖屏頁面音樂控制
這篇文章主要介紹了Android實(shí)現(xiàn)原生鎖屏頁面音樂控制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12Android 中自定義ContentProvider與ContentObserver的使用簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android 中自定義ContentProvider與ContentObserver的使用簡(jiǎn)單實(shí)例的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-09-09Android開發(fā):淺談MVP模式應(yīng)用與內(nèi)存泄漏問題解決
本篇文章主要介紹了Android開發(fā):MVP模式應(yīng)用與內(nèi)存泄漏問題解決,具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11一文理解Android系統(tǒng)中強(qiáng)指針的實(shí)現(xiàn)
因?yàn)锳ndroid中很多地方代碼是用C++編寫,為了能夠保證C++中指針能夠被正確的釋放,于是Android引入了其實(shí)在C++中已經(jīng)有的智能指針技術(shù)2021-10-10Android App中實(shí)現(xiàn)相冊(cè)瀑布流展示的實(shí)例分享
這篇文章主要介紹了Android App中實(shí)現(xiàn)相冊(cè)瀑布流展示的實(shí)例分享,例子中利用到了緩存LruCache類的相關(guān)算法來解決大量加載問題,需要的朋友可以參考下2016-04-04Android Studio進(jìn)行APP圖標(biāo)更改的兩種方式總結(jié)
這篇文章主要介紹了Android Studio進(jìn)行APP圖標(biāo)更改的兩種方式總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Android使用post方式上傳圖片到服務(wù)器的方法
這篇文章主要介紹了Android使用post方式上傳圖片到服務(wù)器的方法,結(jié)合實(shí)例形式分析了Android文件傳輸?shù)南嚓P(guān)技巧,需要的朋友可以參考下2016-03-03