編輯:關於android開發
直接使用ImageView顯示bitmap會占用較多資源,特別是圖片較大的時候,可能導致崩潰。
使用BitmapFactory.Options設置inSampleSize, 這樣做可以減少對系統資源的要求。
屬性值inSampleSize表示縮略圖大小為原始圖片大小的幾分之一,即如果這個值為2,則取出的縮略圖的寬和高都是原始圖片的1/2,圖片大小就為原始大小的1/4。
Options中有個屬性inJustDecodeBounds,SDK中是這麼說的
If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.
通過設置inJustDecodeBounds為true,獲取到outHeight(圖片原始高度)和outWidth(圖片的原始寬度),然後計算一個inSampleSize(縮放值)
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView
android:id="@+id/imageview"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
/>
</LinearLayout>
java源碼
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;
public class AndroidImage extends Activity {
private String imageFile = "/sdcard/AndroidSharedPreferencesEditor.png";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView myImageView = (ImageView)findViewById(R.id.imageview);
//Bitmap bitmap = BitmapFactory.decodeFile(imageFile);
//myImageView.setImageBitmap(bitmap);
Bitmap bitmap;
float imagew = 300;
float imageh = 300;
BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options();
bitmapFactoryOptions.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeFile(imageFile, bitmapFactoryOptions);
int yRatio = (int)Math.ceil(bitmapFactoryOptions.outHeight/imageh);
int xRatio = (int)Math.ceil(bitmapFactoryOptions.outWidth/imagew);
if (yRatio > 1 || xRatio > 1){
if (yRatio > xRatio) {
bitmapFactoryOptions.inSampleSize = yRatio;
Toast.makeText(this,
"yRatio = " + String.valueOf(yRatio),
Toast.LENGTH_LONG).show();
}
else {
bitmapFactoryOptions.inSampleSize = xRatio;
Toast.makeText(this,
"xRatio = " + String.valueOf(xRatio),
Toast.LENGTH_LONG).show();
}
}
else{
Toast.makeText(this,
"inSampleSize = 1",
Toast.LENGTH_LONG).show();
}
bitmapFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(imageFile, bitmapFactoryOptions);
myImageView.setImageBitmap(bitmap);
}
}
【CityHunter】基於LBS的AR體感游戲設計理念,cityhunterar 本人目前還不是游戲行業的圈內人士,並不懂得,游戲行業的生態圈,也不懂得,所謂的什麼&l
Android系統之路(初識MTK) ------ System-Bluetooth name/WiFi AP name/sleep add never/Notifica
Android MediaPlayer 音樂播放 主要使用 android.media.MediaPlayer; android.widget.SeekBar; &nbs
android:Activity啟動模式之singleInstance 先看一下singleInstance啟動模式的說明: 只有一個實例,並且這個實例獨立運行在一個