【Android基础】简单的本地图片浏览器
来源:互联网 发布:北大青鸟网络授课 编辑:程序博客网 时间:2024/05/21 20:26
主要思路:
1.将指定目录下的图片文件添加到一个ArrayList中
2.通过按钮来控制ArrayList的指针值
3.通过将File对象转化为Bitmap对象,然后使用ImageView的setImageBitmap()方法来显示图片。
首先是布局:
<?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:orientation="vertical"> <ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/pre" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上一张"/> <View android:layout_width="0dp" android:layout_height="1px" android:layout_weight="1"/> <Button android:id="@+id/next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一张"/> </LinearLayout></LinearLayout>
界面是介个样子的:
然后是Java代码:
package com.numeron.myimagebrowser;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.ArrayList;import static android.os.Environment.getExternalStorageDirectory;public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button pre,next; private ImageView imageView; private ArrayList<File> images; private int i = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.images = getPath(); //获取本地图片集合 init(); } /** * 对布局文件进行初始化 * */ private void init(){ pre = (Button) findViewById(R.id.pre); pre.setOnClickListener(this); next = (Button) findViewById(R.id.next); next.setOnClickListener(this); imageView = (ImageView) findViewById(R.id.imageview); showImage(0); } /** * 为按键添加监听事件 * 实际上就是控制ArrayList集合中指针的数据来显示图片 * 速度较慢,每次都需要重新读取。 * */ @Override public void onClick(View v) { switch(v.getId()){ case R.id.pre: i --; if(i < 0) { i = 0; Toast.makeText(this, "已经是第一张了", Toast.LENGTH_SHORT).show(); break; } showImage(i); break; case R.id.next: i ++; if(i >= images.size()) { i = images.size() - 1; Toast.makeText(this, "已经是最后一张了", Toast.LENGTH_SHORT).show(); break; } showImage(i); break; } } /** * 通过文件获取流,将流转化为Bitmap对象 * */ private Bitmap getBMP(File file){ BufferedInputStream in = null; Bitmap BMP = null; try{ in = new BufferedInputStream(new FileInputStream(file)); BMP = BitmapFactory.decodeStream(in); } catch (FileNotFoundException e) { Toast.makeText(this, "程序异常!", Toast.LENGTH_SHORT).show(); } finally { if(in != null) try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return BMP; } /** * 将指定目录下的指定格式的文件存到入集合中。 * */ private ArrayList<File> getPath(){ ArrayList<File> al = new ArrayList<>(); File myphotos = new File(getExternalStorageDirectory(),"DCIM"); File[] files = myphotos.listFiles(); for (File file : files) { if(file.exists() && file.isFile() && isImage(file)){ al.add(file); } } return al; } /** * 设置文件过滤器,只需要指定格式的文件 * */ private boolean isImage(File file){ String[] strs = {".jpg",".png,"}; for (String str : strs) { return file.getName().endsWith(str); } return false; } /** * 设置文件的 * */ private void showImage(int i){ imageView.setImageBitmap(getBMP(this.images.get(i))); }}
运行结果:
阅读全文
0 0
- 【Android基础】简单的本地图片浏览器
- 【Android】简单图片浏览器
- Android 简单图片浏览器
- Android开发-简单的图片浏览器
- Android开发—简单的图片浏览器
- 简单的图片浏览器
- 简单的图片浏览器
- Android 简单图片浏览器 ImageView
- android开发简单图片浏览器
- Android简单的图片浏览器开发遇到的各种坑
- Android界面编程之简单的图片浏览器
- Qt 实现的简易本地图片浏览器
- android 基础-自定义listView的实现 简单的文件浏览器
- C#简单的图片浏览器
- 一个简单的图片浏览器
- 制作简单的图片浏览器
- 简单图片浏览器的实现
- android ViewPager 简单多线程加载本地图片
- struts处理.do后缀请求报Struts2中关于"There is no Action mapped for namespace / and action name"的总结
- 如何做自动化测试
- serialVersionUID的作用
- ipa 包中的 PNG 文件的正常化
- 创建数据库以及增删改查
- 【Android基础】简单的本地图片浏览器
- java中equals() 和 hasCode()方法比较
- 见识superDiamond的强悍
- 【cogs2711】jump,二分答案+倍增套ST表
- 几个 Context 上下文的区别
- Promise 异步函数顺序执行
- Object的wait、notify来实现线程同步原理
- 从零构建 vue2 + vue-router + vuex 开发环境到入门,实现基本的登录退出功能
- 基于layer上传查看图片