Bitmap 代表一张位图,BitmapDrawable里封装的图片就是一个 Bitmap对象.开发者为了把一个Bitmap 对象包装成 BitmapDrawable对象,可用调用
BitmapDrawable的构造器:
Bitmap –> BitmapDrawable :
//把一个Bitmap对象包装成 BitmapDrawable对象
BitmapDrawable drawable = new BitmapDrawable(bitmap);
//如果需要获取 BitmapDrawable 所包装的 bitmap对象 可以调用BitmapDrawable的 getBitmap()方法
// 如下面的代码提示:
//获取一个 BitmapDrawable所包装的 Bitmap对象
Bitmap bitmap = drawable.getBitmap();
除此之外 Bitmap 还提供了一些静态方法,来创建新的Bitmap对象.
另外 BitmapFactory 是一个工具类.它用于提供大量的方法.这些方法可以用于从不同的数据源,来解析.创建Bitmap对象. BitmapFactory 包含了如下方法;
- decodeByteArray(byte[] data, int offset, int length) ; 从指定字节数组的 offset位置开始,将长度为length的字节数据解析成 Bitmap对象.
- decodeFile(String pathName) ; 从 pathName指定文件中解析,创建 Bitmap对象.
- decodeFileDescriptor(FileDescriptor fd) : 用于从FileDescriptor 对应的文件中解析,创建 itmap对象.
- decodeResource(Resources res, int id): 用于根据给定的资源Id从指定资源中解析,创建 itmap对象.
- decodeStream(InputStream is): 用于从指定输入流中 解析,创建 itmap对象.
Android 为Bitmap 提供了两个方法来判断是否已经回收,以及强制 Bitmap回收自己.
- boolean isRecycled(); 返回该 Bitmap对象是否已经回收.
- void recycle() 强制一个 Bitmap 对象立即回收自己.
开发者需要在 /asset/目录下放入 几张图片
package com.test.bitmapdemo;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.IOException;
import java.io.InputStream;
/**
* Bitmap 和 BitmapFactory
*/
public class MainActivity extends AppCompatActivity {
String[] imageIds = null;
AssetManager mAsset = null;
int currentImg = 1;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.iv_image);
mAsset = getAssets();
try {
//获取 /asset/ 目录下的所有文件
imageIds = mAsset.list("");
} catch (IOException e) {
e.printStackTrace();
}
Button btnNext = (Button) findViewById(R.id.next);
if (btnNext != null) {
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//如果发生数组角标越界
if (currentImg >= imageIds.length) {
currentImg = 0;
}
//找到下一个图片文件
while (!imageIds[currentImg].endsWith(".png") &&
!imageIds[currentImg].endsWith(".jpg") && !imageIds[currentImg].endsWith(".gif")) {
currentImg++;
//如果角标越界
if (currentImg >= imageIds.length) {
currentImg = 0;
}
}
InputStream assetFile = null;
//打开指定资源对应的输入流
try {
assetFile = mAsset.open(imageIds[currentImg++]);
} catch (IOException e) {
e.printStackTrace();
}
BitmapDrawable bitmapDrawable = (BitmapDrawable) image.getDrawable();
if (bitmapDrawable != null && !bitmapDrawable.getBitmap().isRecycled()) {
bitmapDrawable.getBitmap().recycle();
}
//改变 image 显示的图片
image.setImageBitmap(BitmapFactory.decodeStream(assetFile));
}
});
}
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context="com.test.bitmapdemo.MainActivity">
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/next"/>
<ImageView
android:id="@+id/iv_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
/>
</LinearLayout>
本文介绍了Android中Bitmap和BitmapDrawable的基本使用方法,包括如何通过BitmapFactory从不同数据源加载Bitmap,以及BitmapDrawable与Bitmap之间的转换过程。同时展示了如何在代码中实现图片资源的管理和释放。
1129

被折叠的 条评论
为什么被折叠?



