在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

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

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

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

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

下面我們通過具體實例來展示怎么實現(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();
        //對資源圖片進行縮放
        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;
        //下面兩個字段需要組合使用
        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);
        /*
         * 通過Matrix類的postScale方法進行縮放
         */
        matrix.postScale(scaleWidht, scaleHeight);
        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        return newbmp;
    }

}

3.運行實例:

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