Android 关于BitmapFactory 的使用
来源:互联网 发布:绝地求生n卡优化设置 编辑:程序博客网 时间:2024/05/20 09:48
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;@Overrideprotected 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() {@Overridepublic void onClick(View arg0){// TODO Auto-generated method stubif(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)); }});}@Overridepublic 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;}}
0 0
- Android 关于BitmapFactory 的使用
- Android的BitmapFactory的使用
- android之BitmapFactory.Options的使用
- android之BitmapFactory.Options的使用
- android之BitmapFactory.Options的使用
- android之BitmapFactory.Options的使用
- android之BitmapFactory.Options的使用
- 关于android缩放bitmap,BitmapFactory.Options.inSampleSize的优化算法
- Android --- bitmapfactory 使用getResources()小结
- android的Bitmap和BitmapFactory类的使用
- android之BitmapFactory.Options的使用,避免oom
- android BitmapFactory的OutOfMemoryError 解决
- android BitmapFactory的内存溢出
- android BitmapFactory的OutOfMemoryError: bitmap ...
- 浅谈BitmapFactory.Options的使用
- BitmapFactory的使用获取Bitmap
- Android BitmapFactory的OutOfMemoryError问题,最近看到
- Android BitmapFactory支持解析的图片格式
- Member Single Round Match 461 Round 1 - Division I, Level Two BuildingCities
- UVA - 10716 Evil Straw Warts Live 贪心
- 【2120】数据结构实验之链表五:单链表的拆分
- 输入/输出函数
- 【Web 前端】笔记1
- Android 关于BitmapFactory 的使用
- 代理模式
- fhfehg
- index optimized
- sfsf
- Command设计模式2
- C++类模板与异常处理
- 什么叫裸机
- 大型机半个世纪发展史