Android笔记(二十八) Android中图片之简单图片使用

         用户界面很大程度上决定了APP是否被用户接收,为了提供友好的界面,就需要在应用中使用图片了,Android提供了丰富的图片处理功能。

简单使用图片

      使用Drawable对象

         为Android应用增加了Drawable资源之后,系统会自动在R.java文件中创建一个索引项:R.drawable.fileName,然后在Java中可以通过R.drawable.fileName来获取到该资源的索引(一个int类型的常量),如果要获取实际的Drawable对象,则可以调用Resources的getDrawable(int id)方法来获取。

ImageView image = (ImageView) findViewById(R.id.image);
image.setImageResource(R.drawable.pic1);

      Bitmap和BitmapFactory

         Bitmap代表一张位图,BitmapDrawable里封装的图片是一个Bitmap对象。开发者为了把一个Bitmap对象包装成一个BitmapDrawable对象,可以调用BitmapDrawable的构造器:

BitmapDrawable bd = new BitmapDrawable(bitmap);

         如果需要获取BitmapDrawable所包装的Bitmap对象,则可调用BitmapDrawable的getBitmap()方法

Bitmap bitmap = drawable.getBitmap();

         BitmapFactory是一个工具类,用于从不同的数据源来解析、创建Bitmap对象

         BitmapFactory提供了一系列方法来帮助我们创建一个Bitmap对象,然后我们可以通过

imageView.setImageBitmap(Bitmap bm)

         来更改一个ImageView显示的图像。

         由于系统内容比较小,如果系统不停的去解析、创建Bitmap对象,可能会有内存溢出错误,所以Android为Bitmap提供了两个方法来判断它是否已经回收,如果没有,则强制Bitmap回收自己

    boolean isRecycled();    判断该Bitmap对象是否已被回收
    void recycle()    强制Bitmap对象回收自己

          一个例子:

package cn.lixyz.bitmaptest;

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.widget.Button;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class MainActivity extends Activity implements View.OnClickListener {

    private ImageView imageView;
    private ArrayList<String> images;
    private Button btnNext;
    private Button btnLast;
    private int index = 0;
    private AssetManager am;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获得组件
        imageView = (ImageView) findViewById(R.id.image);
        btnNext = (Button) findViewById(R.id.next);
        btnLast = (Button) findViewById(R.id.last);

        //调用getImages方法,获取assets下的图片集合
        getImages();

        //点击按钮
        btnNext.setOnClickListener(this);
        btnLast.setOnClickListener(this);
    }

    /**
     * 因为assets下不光有图片,还会有其他的目录或文件,需要将图片甄别出来存到一个list中当作数据源
     */
    public void getImages() {
        String[] tmpImgs = null;
        images = new ArrayList<String>();
        //getAssets()方法可以获得AssetManager对象
        am = getAssets();
        try {
            //获取asset下内容list
            tmpImgs = am.list("");
            //挑出.jpg文件,存入list中
            for (int i = 0; i < tmpImgs.length; i++) {
                if (tmpImgs[i].endsWith(".jpg")) {
                    images.add(tmpImgs[i]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 点击按钮事件
     *
     * @param v view对象,用于判断点击的是什么按钮
     */
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.next:
                try {
                    index++;    //下标+1,用于显示下一张图片
                    if (index >= images.size()) {   //防止越界
                        index = 0;
                    }

                    //判断Bitmap是否已经回收,如果没有回收,则先回收
                    BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable();
                    if (bd != null && !bd.getBitmap().isRecycled()) {
                        bd.getBitmap().recycle();
                    }

                    //AssetManager类的open方法,可以返回一个输入流
                    InputStream is = am.open(images.get(index));
                    //通过BitmapFactory的decodeStream()方法,改变显示图像
                    imageView.setImageBitmap(BitmapFactory.decodeStream(is));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.last:
                try {
                    index--;    //下标+1,用于显示下一张图片
                    if (index < 0) {    //防止越界
                        index = images.size() - 1;
                    }
                    //判断Bitmap是否已经回收,如果没有回收,则先回收
                    BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable();
                    if (bd != null && !bd.getBitmap().isRecycled()) {
                        bd.getBitmap().recycle();
                    }
                    //AssetManager类的open方法,可以返回一个输入流
                    InputStream is = am.open(images.get(index));
                    //通过BitmapFactory的decodeStream()方法,改变显示图像
                    imageView.setImageBitmap(BitmapFactory.decodeStream(is));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
MainActivity.java
<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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:orientation="horizontal">

        <Button
            android:id="@+id/last"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="上一张" />

        <Button
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="下一张" />
    </LinearLayout>
</RelativeLayout>
activity_main.xml

          运行结果:

原文地址:https://www.cnblogs.com/xs104/p/4809023.html