鍍金池/ 教程/ Android/ Android 實(shí)戰(zhàn)簡(jiǎn)易教程-第九槍(BitmapFactory.Options 對(duì)資源圖片進(jìn)行縮放)
Android 實(shí)戰(zhàn)簡(jiǎn)易教程-第九槍(BitmapFactory.Options 對(duì)資源圖片進(jìn)行縮放)
Android 實(shí)戰(zhàn)簡(jiǎn)易教程-第三槍(實(shí)現(xiàn)簡(jiǎn)單繪圖組件)
Android 用的最多的,也最難用的應(yīng)該就是 ListView 了,下面我們研究一下它的用法。
Android 實(shí)戰(zhàn)簡(jiǎn)易教程-第四槍(ScrollView 和 HorizontalScrollView 動(dòng)態(tài)添加控件并提供事件
Android 實(shí)戰(zhàn)簡(jiǎn)易教程-第一槍(Spinner 控件詳解)
Android 實(shí)戰(zhàn)簡(jiǎn)易教程-第六槍(各種對(duì)話框 Dialog 用法研究大全)
Android 實(shí)戰(zhàn)簡(jiǎn)易教程-第七槍(Activity 的啟動(dòng)模式)
Android 實(shí)戰(zhàn)簡(jiǎn)易教程-第二槍(Spinner 下拉級(jí)聯(lián)效果)
Android 實(shí)戰(zhàn)簡(jiǎn)易教程-第八槍(ImageSwitcher 用法實(shí)例)
Android 實(shí)戰(zhàn)簡(jiǎn)易教程-第十槍(畫(huà)廊組件 Gallery 實(shí)用研究)
作者簡(jiǎn)介

Android 實(shí)戰(zhàn)簡(jiǎn)易教程-第九槍(BitmapFactory.Options 對(duì)資源圖片進(jìn)行縮放)

我們知道,我們編寫(xiě)的應(yīng)用程序都是有一定內(nèi)存限制的,程序占用了過(guò)高的內(nèi)存就容易出現(xiàn) OOM(OutOfMemory)異常。因此在展示高分辨率圖片的時(shí)候,最好先將圖片進(jìn)行壓縮,壓縮后的圖片大小應(yīng)該和用來(lái)展示它的控件大小相近,這樣可以兼顧顯示效果和內(nèi)存占用。

BitmapFactory.Options 這個(gè)類,有一個(gè)字段叫做 inJustDecodeBounds 。SDK中對(duì)這個(gè)成員的說(shuō)明是這樣的: If set to true, the decoder will return null (no bitmap), but the out… 也就是說(shuō),如果我們把它設(shè)為true,那么BitmapFactory.decodeFile(String path, Options opt)并不會(huì)真的返回一個(gè) Bitmap 給你,它僅僅會(huì)把它的寬,高取回來(lái)給你,這樣就不會(huì)占用太多的內(nèi)存,也就不會(huì)那么頻繁的發(fā)生 OOM 了。

下面我們通過(guò)具體實(shí)例來(lái)展示怎么實(shí)現(xiàn)縮略圖。

1.布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/mei" ></ImageView>

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:src="@drawable/mei" ></ImageView>

</RelativeLayout>

2.MainActivity.java 代碼如下:

package org.yayun.demo;

import java.io.InputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private ImageView imageView1;
    private ImageView imageView2;
    Bitmap mBitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();

    }

    private void initView(){
        imageView1=(ImageView)findViewById(R.id.imageView1);
        imageView2=(ImageView)findViewById(R.id.imageView2);
        //讀取資源圖片
        mBitmap=readBitMap();
        //對(duì)資源圖片進(jìn)行縮放
        imageView2.setImageBitmap(zoomBitmap(mBitmap, mBitmap.getWidth()/4, mBitmap.getHeight()/4));
    }

    /**
     * 讀取資源圖片
     * @return 
     */
    private Bitmap readBitMap(){
        BitmapFactory.Options opt=new BitmapFactory.Options();
        /*
         * 設(shè)置讓解碼器以最佳方式解碼
         */
        opt.inPreferredConfig=Bitmap.Config.RGB_565;
        //下面兩個(gè)字段需要組合使用
        opt.inPurgeable=true;
        opt.inInputShareable=true;
        /*
         * 獲取資源圖片
         */
        InputStream is=this.getResources().openRawResource(R.drawable.mei);
        return BitmapFactory.decodeStream(is, null, opt);
    }

    /**
     * 縮放圖片
     * @param bitmap
     * @param w
     * @param h
     * @return
     */
    public  Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidht = ((float) w / width);
        float scaleHeight = ((float) h / height);
        /*
         * 通過(guò)Matrix類的postScale方法進(jìn)行縮放
         */
        matrix.postScale(scaleWidht, scaleHeight);
        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        return newbmp;
    }

}

3.運(yùn)行實(shí)例:

縮略圖效果體現(xiàn)出了。