編輯:關於 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 程式設計有所幫助。
Gradle 打包三板斧:在 Eclipse 專案上搞定多渠道與差異化配置
同一個 App,要因應不同應用市場換渠道標識、換包名、換引導頁、換 icon——如果全靠手工改再一個個打包,那是純體力活,而且極易出錯。Gradle 把這件事變成了改幾行
Android 自定義 View 實作圓形 Progress 進度條
先看成果: 目標很明確:在一個圓框內部畫一段會持續增長的扇形。整體可以拆成四個步驟。 一、建立類別與自定義屬性 新增 ProgressView 繼承自 View
Android 學習筆記(25):ViewAnimator 家族與帶轉場動畫的 View 切換
在介面開發裡,「換一個畫面」往往不只是把 A 藏起來、把 B 擺出來這麼簡單。切換若生硬突兀,使用者一眼就能感覺到跳躍;而加上淡入淡出、滑入滑出這類轉場動畫,同樣的操作立
把 Android 開源庫發佈到 JCenter:流程、配置與常見坑
前言 越來越多 Android 開發者會把自己的成果整理成開源庫。透過 JCenter 發佈後,其他人只需要在 gradle 中加入一行依賴,就能快速使用。本文整理從建立