码迷,mamicode.com
首页 > 其他好文 > 详细

Bitmap图片查看器

时间:2015-05-20 23:40:51      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:

1.MainActivity.java

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 MainActivity extends Activity {

    String[] images = null;
    AssetManager assets = null;
    int currentImge = 0;
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        image = (ImageView) findViewById(R.id.imageBitmap);

        try {
            assets = getAssets();
            // 获取/assests/目录下的所有的文件
            images = assets.list("");

        } catch (IOException e) {
            e.printStackTrace();
        }

        final Button next = (Button) findViewById(R.id.btnBitmap);

        next.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (currentImge >= images.length) {
                    currentImge = 0;
                }

                // 找到下一个图片文件,这里使用jpg图片
                while (!images[currentImge].endsWith(".jpg")) {
                    currentImge++;
                    // 如果发生数组越界
                    if (currentImge >= images.length) {
                        currentImge = 0;
                    }
                }

                InputStream assetFile = null;

                try {
                    // 打开指定资源对应的输入流
                    assetFile = assets.open(images[currentImge++]);

                } catch (IOException e) {
                    e.printStackTrace();
                }
                BitmapDrawable bitmapDrawable = (BitmapDrawable) image
                        .getDrawable();
                // 如果图片还未回收,先强制回收该图片
                if (bitmapDrawable != null
                        && !bitmapDrawable.getBitmap().isRecycled()) {
                    bitmapDrawable.getBitmap().recycle();
                }
                // 该变现实的图片
                image.setImageBitmap(BitmapFactory.decodeStream(assetFile));

            }
        });
    }
}

2.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnBitmap"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/nextPic" />

    <ImageView
        android:id="@+id/imageBitmap"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

 

Bitmap图片查看器

标签:

原文地址:http://www.cnblogs.com/hehaiyang/p/4518397.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!