Android的Bitmap和BitmapDrawable类解析-android学习之旅(六十)
2016-03-15 18:17
441 查看
使用简单图片
使用Drawable对象
bitmap和BitmapDrawable对象
package peng.liu.test; import android.app.Activity; import android.content.res.AssetFileDescriptor; import android.content.res.AssetManager; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.ClipDrawable; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; import java.io.InputStream; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends Activity{ String[] images; ImageView image; int currentImg; AssetManager asset; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = (ImageView) findViewById(R.id.imageBit); try{ asset = getAssets(); images = asset.list(""); }catch (Exception e){ e.printStackTrace(); } findViewById(R.id.btnBit).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { 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 = asset.open(images[currentImg++]); }catch (Exception e){ e.printStackTrace(); } BitmapDrawable drawable = (BitmapDrawable) image.getDrawable(); if (drawable != null && !drawable.getBitmap().isRecycled()){ drawable.getBitmap().recycle(); } image.setImageBitmap(BitmapFactory.decodeStream(assetFile)); } } }); } }
相关文章推荐
- Android中的数据库相关以及封装
- Canvas清理画布
- Android Studio中多项目共享Library
- android典型的选项卡界面利用viewPager+fragment实现
- Mac下Android Studio NDK 环境搭建并运行
- Android 实现模拟按键方法
- Android基础知识之控件系列(4)——CheckedTextView、Chronometer、DigitalClock类
- Android Studio 加载本地Assets中的html文件
- Android 5.0(Lollipop)中的SurfaceTexture,TextureView, SurfaceView和GLSurfaceView
- android各种效果开源项目
- android 跟随手指移动的 view
- Android 中 View 炸裂特效的实现分析
- Android View体系(六)从源码解析Activity的构成
- 关于BroadCastReceiver无法接收到广播的问题
- 仿ios9风格的Android 分享dialog
- 关于Android在线直播问题的讨论与研究
- Android高效加载大图、多图解决方案,有效避免程序OOM
- Android Studio中git的设置
- Android系统性能调优工具介绍
- 详解Androidstudio编写的APK过大的问题