Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 中實現圖片等比例顯示的方法

編輯:關於 Android 程式設計

本文透過實例,講解如何在 Android 上讓圖片按照原始比例顯示,避免變形。

在 Android 中,由於螢幕密度的關係,直接取得圖片的寬高往往並不準確。原因大致是這樣:如果你把圖片放在 drawable-mdpi 目錄裡,而手機屬於 drawable-hdpi 的類型,系統就會自動放大圖片,此時取到的寬高未必等於原圖的真實尺寸。

那麼要如何讓圖片依原始比例呈現呢?首先,可以在 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 來管理圖片快取,這樣能有效降低記憶體壓力,避免大量圖片同時載入造成 OutOfMemoryError:

 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 程式設計有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved