• (六十五)开发一个查看/assets/目录下面图片的图片查看器


    需求:开发一个查看/assets/目录下面图片的图片查看器,用户单击程序中的按钮会自动搜寻/assets/目录下的下一张图片。

    1、此项目的结构如下所示:

    二、MainActivity.java中代码如下所示:

    package com.example.bitmaptest;
    
    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 currentImg = 0;
        ImageView image;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            image = (ImageView) findViewById(R.id.iv_assets);
            try {
                assets = getAssets();
                images = assets.list("");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            final Button bt_assets_view = (Button) this.findViewById(R.id.bt_view_image);
            bt_assets_view.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    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) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    BitmapDrawable bitmapDrawable = (BitmapDrawable) image.getDrawable();
    //由于手机系统内存比较小,如果系统不停地去解析、创建Bitmap对象,可能由于前面创建的
    //Bitmap对象所占用的内存还没有回收,从而导致程序运行时引发内存溢出错误
    if(bitmapDrawable!= null &&!bitmapDrawable.getBitmap().isRecycled()){ bitmapDrawable.getBitmap().recycle(); } image.setImageBitmap(BitmapFactory.decodeStream(assetFile)); } }); } }

    二、activity_main.xml中代码如下所示:

    <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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.bitmaptest.MainActivity" >
    
        <ImageView
            android:id="@+id/iv_assets"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <Button
            android:id="@+id/bt_view_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="查看图片" />
    
    </LinearLayout>
  • 相关阅读:
    正则表达式
    移动开发知识点收集
    SQL Server
    百度数据图表插件Echarts
    Xamarin
    Func与Action
    MVC Core
    利用 async & await 的异步编程
    CSS3
    [leetcode]374. Guess Number Higher or Lower
  • 原文地址:https://www.cnblogs.com/fuyanan/p/4527362.html
Copyright © 2020-2023  润新知