編輯:關於Android編程
圖片可能會有不同的大小。在許多情況下,圖片的大小會超出我們需要的大小。例如,我們的Camera應用,我們所拍的照片的大小遠大於屏幕顯示的大小
假如你的應用被限制了內存使用,顯而易見,你會選擇加載一個低分辨率的圖片。這張低分辨率的圖片應該匹配屏幕的尺寸。更高分辨率的圖像沒有提供任何可見的好處,但仍占用寶貴的內存,而且由於額外的動態縮放,會帶來額外的性能開銷。
本篇文章教你通過加載一個小尺寸的圖片樣本,來修飾一張大圖,並且沒有超過應用的內存限制。
原文地址http://wear.techbrood.com/training/displaying-bitmaps/load-bitmap.html
BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), R.id.myimage, options); int imageHeight = options.outHeight; int imageWidth = options.outWidth; String imageType = options.outMimeType;為了避免
java.lang.OutOfMemory exceptions,在解碼圖片之前,預先檢查Bitmap的大小,除非你有絕對的把握你的bitmap有合適的大小,並且在內存限制大小范圍之內。
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}inJustDecodeBounds為true,然後我們計算inSample的值,然後設置inJustDecodeBounds為false,最後解碼圖片。
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}mImageView.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));
BitmapFactory.decode* as
need。
下面還有幾篇從android Developer上面翻譯過來的,感覺挺有用的,加個關注吧,(*^__^*)
嘻嘻
Android開發 ----------如何真機調試?
一般來說 真機調試 是最快的, 所以建議 大家 直接用真機調試。
Android百度地圖定位後獲取周邊位置的實現代碼
本文實例講解Android百度地圖定位後獲取周邊位置的實現代碼,分享給大家供大家參考,具體內容如下效果圖:具體代碼:1.布局文件<?xml version=
Android Property Animation
1、概述Android提供了幾種動畫類型:View Animation 、Drawable Animation 、Property Animation 。View An
Android數據存儲之IO
Android開發中免不了數據本地的存儲,今天我們來說一說如何利用IO流來進行數據存儲。這裡我們通過模擬一個QQ登陸界面的小demo來實際操作IO流。功能描述:點擊按鈕能