Android学习之Bitmap

简介: Android系统提供了ImageView显示普通静态图片,也提供了AnimationDrawable来开发逐帧动画。 Bitmap和BitmapFactory Bitmap代表一张位图,BitmapDrawable里封装的图片就是一个Bitmap对象。 如果android应用需要访问其他存储路径如SD卡的图片,则需要使用BitmapFactory来解析、创建Bitmap对象。 由

Android系统提供了ImageView显示普通静态图片,也提供了AnimationDrawable来开发逐帧动画。

Bitmap和BitmapFactory

Bitmap代表一张位图,BitmapDrawable里封装的图片就是一个Bitmap对象。

如果android应用需要访问其他存储路径如SD卡的图片,则需要使用BitmapFactory来解析、创建Bitmap对象。

由于手机内存有限所以创建的Bitmap对象要及时回收,避免OutOfMemory错误,isRecycled()用于判断是否回收,recycle()用于回收。

import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class BitmapTest extends Activity
{
	String[] images = null;
	AssetManager assets = null;
	int currentImg = 0;
	ImageView image;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		image = (ImageView) findViewById(R.id.image);
		try
		{
			assets = getAssets();
			// 获取/assets/目录下所有文件
			images = assets.list("");
			System.out.println(images.length);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		// 获取bn按钮
		final Button next = (Button) findViewById(R.id.next);
		// 为bn按钮绑定事件监听器,该监听器将会查看下一张图片
		next.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View sources)
			{
				// 如果发生数组越界
				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 = assets.open(images[currentImg++]);
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				BitmapDrawable bitmapDrawable = (BitmapDrawable) image
						.getDrawable();
				// 如果图片还未回收,先强制回收该图片
				if (bitmapDrawable != null
					&& !bitmapDrawable.getBitmap().isRecycled()) //①
				{
					bitmapDrawable.getBitmap().recycle();
				}
				// 改变ImageView显示的图片
				image.setImageBitmap(BitmapFactory
					.decodeStream(assetFile)); //②
			}
		});
	}
}


目录
相关文章
|
2月前
|
XML 缓存 Android开发
Android开发,使用kotlin学习多媒体功能(详细)
Android开发,使用kotlin学习多媒体功能(详细)
105 0
|
4月前
|
安全 Linux Android开发
Android安全启动学习(一):AVB校验是什么?
Android安全启动学习(一):AVB校验是什么?
114 0
|
4月前
|
存储 安全 Linux
Android安全启动学习(四):device-mapper-verity (dm-verity)和哈希树
Android安全启动学习(四):device-mapper-verity (dm-verity)和哈希树
127 0
|
12天前
|
网络协议 Shell Android开发
Android 深入学习ADB调试原理(1)
Android 深入学习ADB调试原理(1)
26 1
|
12天前
|
缓存 网络协议 编译器
针对Android系统工程师的C/C++学习目录
针对Android系统工程师的C/C++学习目录
2 0
|
2月前
|
Android开发
[Android jni] Bitmap与Mat对象的相互转换
[Android jni] Bitmap与Mat对象的相互转换
57 0
|
4月前
|
开发框架 物联网 数据库
89个android学习样例源码
89个android学习样例源码
76 0
|
4月前
|
Java Android开发 C++
2023安卓逆向 -- JNI学习(从开发到反编译)
2023安卓逆向 -- JNI学习(从开发到反编译)
26 0
|
4月前
|
存储 缓存 编解码
Android 性能优化: 解释Bitmap的优化策略。
Android 性能优化: 解释Bitmap的优化策略。
42 1
|
4月前
|
Android开发
Android源码学习(五):AVB2.0-libavb库介绍2
Android源码学习(五):AVB2.0-libavb库介绍2
113 0