<Bitmap>

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 包含了如下方法;

  1. decodeByteArray(byte[] data, int offset, int length) ; 从指定字节数组的 offset位置开始,将长度为length的字节数据解析成 Bitmap对象.
  2. decodeFile(String pathName) ; 从 pathName指定文件中解析,创建 Bitmap对象.
  3. decodeFileDescriptor(FileDescriptor fd) : 用于从FileDescriptor 对应的文件中解析,创建 itmap对象.
  4. decodeResource(Resources res, int id): 用于根据给定的资源Id从指定资源中解析,创建 itmap对象.
  5. decodeStream(InputStream is): 用于从指定输入流中 解析,创建 itmap对象.

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

  1. boolean isRecycled(); 返回该 Bitmap对象是否已经回收.
  2. 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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值