Android实战简易教程<九>(BitmapFactory.Options对资源图片进行缩放)

我们知道,我们编写的应用程序都是有一定内存限制的,程序占用了过高的内存就容易出现OOM(OutOfMemory)异常。因此在展示高分辨率图片的时候,最好先将图片进行压缩,压缩后的图片大小应该和用来展示它的控件大小相近,这样可以兼顾显示效果和内存占用。

BitmapFactory.Options这个类,有一个字段叫做 inJustDecodeBounds 。SDK中对这个成员的说明是这样的:
If set to true, the decoder will return null (no bitmap), but the out…
也就是说,如果我们把它设为true,那么BitmapFactory.decodeFile(String path, Options opt)并不会真的返回一个Bitmap给你,它仅仅会把它的宽,高取回来给你,这样就不会占用太多的内存,也就不会那么频繁的发生OOM了。

下面我们通过具体实例来展示怎么实现缩略图。

1.布局文件:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ImageView  
  8.         android:id="@+id/imageView1"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:src="@drawable/mei" />  
  12.   
  13.     <ImageView  
  14.         android:id="@+id/imageView2"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_below="@+id/imageView1"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_marginTop="10dp"  
  19.         android:src="@drawable/mei" />  
  20.   
  21. </RelativeLayout>  


2.MainActivity.java代码如下:

[java]  view plain copy
  1. package org.yayun.demo;  
  2.   
  3. import java.io.InputStream;  
  4.   
  5. import android.app.Activity;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.BitmapFactory;  
  8. import android.graphics.Matrix;  
  9. import android.os.Bundle;  
  10. import android.widget.ImageView;  
  11.   
  12. public class MainActivity extends Activity {  
  13.     private ImageView imageView1;  
  14.     private ImageView imageView2;  
  15.     Bitmap mBitmap;  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         initView();  
  21.       
  22.     }  
  23.   
  24.     private void initView(){  
  25.         imageView1=(ImageView)findViewById(R.id.imageView1);  
  26.         imageView2=(ImageView)findViewById(R.id.imageView2);  
  27.         //读取资源图片  
  28.         mBitmap=readBitMap();  
  29.         //对资源图片进行缩放  
  30.         imageView2.setImageBitmap(zoomBitmap(mBitmap, mBitmap.getWidth()/4, mBitmap.getHeight()/4));  
  31.     }  
  32.       
  33.       
  34.     /** 
  35.      * 读取资源图片 
  36.      * @return  
  37.      */  
  38.     private Bitmap readBitMap(){  
  39.         BitmapFactory.Options opt=new BitmapFactory.Options();  
  40.         /* 
  41.          * 设置让解码器以最佳方式解码 
  42.          */  
  43.         opt.inPreferredConfig=Bitmap.Config.RGB_565;  
  44.         //下面两个字段需要组合使用  
  45.         opt.inPurgeable=true;  
  46.         opt.inInputShareable=true;  
  47.         /* 
  48.          * 获取资源图片 
  49.          */  
  50.         InputStream is=this.getResources().openRawResource(R.drawable.mei);  
  51.         return BitmapFactory.decodeStream(is, null, opt);  
  52.     }  
  53.   
  54.       
  55.     /** 
  56.      * 缩放图片 
  57.      * @param bitmap 
  58.      * @param w 
  59.      * @param h 
  60.      * @return 
  61.      */  
  62.     public  Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {  
  63.         int width = bitmap.getWidth();  
  64.         int height = bitmap.getHeight();  
  65.         Matrix matrix = new Matrix();  
  66.         float scaleWidht = ((float) w / width);  
  67.         float scaleHeight = ((float) h / height);  
  68.         /* 
  69.          * 通过Matrix类的postScale方法进行缩放 
  70.          */  
  71.         matrix.postScale(scaleWidht, scaleHeight);  
  72.         Bitmap newbmp = Bitmap.createBitmap(bitmap, 00, width, height, matrix, true);  
  73.         return newbmp;  
  74.     }  
  75.       
  76. }  


3.运行实例:

缩略图效果体现出了了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值