(转)初学Android,图形图像之使用Bitmap和BitmapFactory(二十四)
2017-04-15 19:48
549 查看
原地址:http://blog.csdn.net/lee576/article/details/7860286
Bitmap代表一张位图,BitmapDrawable里封装的图片就是一个Bitmap对象.把Bitmap对象包装成BitmapDrawable对象,可以调用Bitmapdrawable的构造器
BitmapDrawable drawable = new BitmapDrawable(bitmap);
如果要获取BitmapDrawable所包装的bitmap对象,可以调用getBitmap()方法
Bitmap bitmap = drawable.getBitmap();
如果需要访问其它存储路径的图片,需要借助于BitmapFactory来解析,创建Bitmap对象
下面制作一个assets/文件夹下图片的图片查看器,在assets下随便放几张图像文件
点一下按钮
一直点下去,循环出现文件夹下的图片
另外在测试过程中,感觉图片加载的比较慢,不知道有什么优化的方法?
Bitmap代表一张位图,BitmapDrawable里封装的图片就是一个Bitmap对象.把Bitmap对象包装成BitmapDrawable对象,可以调用Bitmapdrawable的构造器
BitmapDrawable drawable = new BitmapDrawable(bitmap);
如果要获取BitmapDrawable所包装的bitmap对象,可以调用getBitmap()方法
Bitmap bitmap = drawable.getBitmap();
如果需要访问其它存储路径的图片,需要借助于BitmapFactory来解析,创建Bitmap对象
下面制作一个assets/文件夹下图片的图片查看器,在assets下随便放几张图像文件
package WangLi.Graphics.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 BitmapTest extends Activity { /** Called when the activity is first created. */ String[] images = null; AssetManager assets = null; int currentImg = 0; ImageView image; @Override public 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(){ public 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)); } }); } }每点一次按钮,ImageView的图片就会切换到另一张
点一下按钮
一直点下去,循环出现文件夹下的图片
另外在测试过程中,感觉图片加载的比较慢,不知道有什么优化的方法?
相关文章推荐
- 初学Android,图形图像之使用Bitmap和BitmapFactory(二十四)
- 初学Android,图形图像之使用Bitmap和BitmapFactory(二十四)
- 初学Android,图形图像之使用drawBitmapMesh扭曲图像(三十二)
- Android学习之——图形图像处理(Bitmap、BitmapFactory)(一)
- 初学Android,图形图像之使用Path类
- 初学Android,图形图像之使用双缓冲画图(二十七)
- 初学Android,图形图像之混合使用逐帧动画和补间动画(三十七)
- 初学Android,图形图像之使用逐帧动画(三十四)
- 初学Android,图形图像之使用SurfaceView(三十九)
- 初学Android,图形图像之使用Path类(二十六)
- 初学Android,图形图像之使用SurfaceView(四十)
- 初学Android,图形图像之使用Path类(二十六)
- 初学Android,图形图像之使用双缓冲画图(二十七)
- Android学习之——图形图像处理(Bitmap、BitmapFactory)(一)
- **ANDROID**# 第七章图形与图像处理(静态处理) > Bitmap是有像素点构成的点阵图。 ------ ## 使用简单的图片 ---- * 通过Drawable对象进行访问。
- 初学Android,图形图像之使用Canvas,Paint绘图(二十五)
- Android图形图像处理之Bitmap和BitmapFactory
- 初学Android,图形图像之使用Shader填充图像(三十三)
- Android学习之——图形图像处理(Bitmap、BitmapFactory)(一)
- android中使用BitmapFactory的decodeStream()方法解码图片失败问题