Android 关于BitmapFactory 的使用

本文介绍Android中Bitmap的使用及内存管理方法,包括BitmapDrawable的创建与Bitmap的回收机制,通过实例展示了如何避免内存泄漏及OOM问题。

 1  BitMap 代表一张位图,BitmapDrawable 里封装的图片就是一个Bitmap 对象,把一个Bitmap对象包装秤一个BitmapDrawable 对象,可以调用BitmapDrawable 构造器

     BitmapDrawable drawable = new BitmapDrawable(bitmap);
  

2  获取BitmapDrawable 里的bitmap 可以调用BitmapDrawable 的 getBitmap() 方法,

   

	  Bitmap  bitmap = drawable.getBitmap();

3  关于图片内存的回收

   由于手机系统内存比较小,如果系统不停得去解析,创建Bitmap对象,可能由于前面的Bitmap所占有的手机内存还没有回收,导致程序运行引发OutOfMemory错误

   Android 为Bitamp提供了两个方法判断他是否已被回收,以及强制回收Bitamp

      1  boolean  isRecyled(); 返回一个boolan 对象,判断该Bitmap是否已被回收

     2 void recycle();强制一个Bitmap对象立即回收自己


代码:(实现一个查看assets目录下图片的图片查看)

 xml

 

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  >
    
     <Button
         android:id="@+id/btn1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text = "下一张"/>
     
     
    <ImageView 
        android:id = "@+id/view1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:layout_below="@id/btn1"
        android:src="@drawable/a"/>"

</RelativeLayout>
java

   

package com.example.bitmaptest;

import java.io.IOException;
import java.io.InputStream;

import org.xml.sax.InputSource;

import android.R.drawable;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    
	String[]  images = null;
	AssetManager asset;
	int currentImg = 0;
	ImageView image;
	public Button btn1; 
	public  ImageView imageview;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn1   = (Button)findViewById(R.id.btn1);
		imageview  = (ImageView)findViewById(R.id.view1);
		
		try
		{
			asset = getAssets();
			images = asset.list("");// 获取asset目录下的所有文件
			
		}
		catch (IOException  e)
		{
			e.printStackTrace();
		}
		btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0)
			{
				// TODO Auto-generated method stub
				if(currentImg >= images.length)//发生数组越界
				{
					currentImg  = 0;
					
				}
				while(!images[currentImg].endsWith(".png") && !images[currentImg].endsWith(".jpg")
						 && !images[currentImg].endsWith(".gif"))
				{
					currentImg++;
					if(currentImg >= images.length)
					{
						currentImg = 0;
					}
				}
				
				InputStream  assetFile = null;
				try
				{
					//打开制定资源对应的输出流
					assetFile = asset.open(images[currentImg]);
					
				}
				catch(IOException e)
				{
					e.printStackTrace();
				}
				BitmapDrawable drawable = (BitmapDrawable)imageview.getDrawable();
				if(drawable != null && drawable.getBitmap().isRecycled() == false)
				{
					drawable.getBitmap().recycle();//如果图片没有回收就回收资源,防止引发OutOfMemory错误
				}
				imageview.setImageBitmap(BitmapFactory.decodeStream(assetFile));
				
				
			 }
			
			
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}


	


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值