Bitmap 和 BitmapFactory 使用示例

作者: 赵者也 | 来源:发表于2017-04-09 10:03 被阅读79次

大部分时候,我们只需要把图片放在 drawable 目录中就可以了。但是由于手机系统的内存比较小,如果系统不停地去解析、创建 Bitmap 对象,可能由于由于前面创建的 Bitmap 所占用的内存还没有回收,而导致程序运行时引发 OutOfMemory 错误。

Android 提供了两个方法判断 Bitmap 是否已回收,以及强制 Bitmap 回收自己:

  1. boolean isRecycled():返回该 Bitmap 对象是否已被回收;
  2. void recycle():强制一个 Bitmap 对象立即回收自己。

除此之外,如果 Android 应用需要访问其他存储路径(比如 SD 卡)里的图片,都需要借助于 BitmapFactory 来解析、创建 Bitmap 对象。

下面是一个查看 /assets/ 目录下图片的图片查看器的示例。

主布局文件的内容如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorGray"
    android:orientation="vertical"
    android:id="@+id/container"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next"
        android:layout_gravity="center"
        android:onClick="next"
        android:layout_marginTop="20dp"
        />

    <ImageView
        android:id="@+id/showImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:layout_marginTop="20dp"
        />

</LinearLayout>

主程序的代码如下所示:

package com.toby.personal.testlistview;

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.util.Log;
import android.view.View;
import android.widget.ImageView;

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

public class MainActivity extends AppCompatActivity {

    final private static String TAG = "Toby_Test";
    String[] images = null;
    AssetManager assetManager = null;
    ImageView imageView;
    int currentIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.showImage);

        try {
            assetManager = getAssets();
            images = assetManager.list("");
        } catch (IOException e) {
            e.printStackTrace();
        }

        next(null);
    }

    public void next(View view) {
        if (currentIndex >= images.length) {
            currentIndex = 0;
        }

        while (!images[currentIndex].endsWith(".png")
                && !images[currentIndex].endsWith(".jpg")
                && !images[currentIndex].endsWith(".jpeg")
                && !images[currentIndex].endsWith(".gif")) {
            currentIndex++;
            if (currentIndex >= images.length) {
                currentIndex = 0;
            }
        }

        InputStream assetFile = null;
        try {
            assetFile = assetManager.open(images[currentIndex++]);
        } catch (IOException e) {
            e.printStackTrace();
        }

        BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();

        if (bitmapDrawable != null && bitmapDrawable.getBitmap().isRecycled()) {
            bitmapDrawable.getBitmap().recycle();
        }

        imageView.setImageBitmap(BitmapFactory.decodeStream(assetFile));
    }
}

运行效果图:


显示效果

参考文献:《疯狂Android讲义(第2版)》

相关文章

网友评论

    本文标题:Bitmap 和 BitmapFactory 使用示例

    本文链接:https://www.haomeiwen.com/subject/xxxdattx.html