package org.crazyit.image;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;/** * Description: * 
site: crazyit.org *
Copyright (C), 2001-2012, Yeeku.H.Lee *
This program is protected by copyright laws. *
Program Name: *
Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */public class BitmapTest extends Activity{String[] images = null;AssetManager assets = null;int currentImg = 0;ImageView image;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);image = (ImageView)findViewById(R.id.image);try{assets = getAssets();//获取/assets/目录下所有文件images = assets.list("");}catch (IOException e){e.printStackTrace();}//获取bn按钮final Button next = (Button)findViewById(R.id.next);//为bn按钮绑定事件监听器,该监听器将会查看下一张图片next.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View sources){//如果发生数组越界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){e.printStackTrace();}BitmapDrawable bitmapDrawable = (BitmapDrawable) image.getDrawable();//如果图片还未回收,先强制回收该图片if (bitmapDrawable != null&& !bitmapDrawable.getBitmap().isRecycled()) //①{bitmapDrawable.getBitmap().recycle();}//改变ImageView显示的图片image.setImageBitmap(BitmapFactory.decodeStream(assetFile)); //②}});}}

更多相关文章

  1. Android保存图片到图库,Android扫描文件到媒体库,Android保存图片
  2. Android 框架学习4:一次读懂热门图片框架 Picasso 源码及流程
  3. Android笔记Android基于事件监听器处理机制
  4. 关于android中网络图片下载中oom解决开源框架Afinal的探究
  5. Blog-07-《一周快速上手Kotlin For Android》-之数组
  6. Android App 性能优化之图片优化
  7. 常用组件:android的图片组件ImageView
  8. Android移动图片

随机推荐

  1. 【DB笔试面试697】在Oracle中,V$SESSION视
  2. 【DB笔试面试678】在Oracle中,什么是热块?
  3. Redis,就是这么朴实无华
  4. 【DB笔试面试628】Oracle的统计信息包括
  5. 【DB笔试面试821】在Oracle中,如何定时生
  6. 7个设计模式在Spring中的应用
  7. 【DB笔试面试445】Oracle中的异常可以分
  8. 【秒杀】一、系统设计要点,从卖病鹅说起
  9. 【DB笔试面试108】在Oracle中,sqldlr true
  10. 【DB笔试面试822】在Oracle中,AWR报告中主