Android实战简易教程-第九枪(BitmapFactory.Options对资源图片进行缩放)
2016-01-04 20:34
633 查看
我们知道,我们编写的应用程序都是有一定内存限制的。程序占用了过高的内存就easy出现OOM(OutOfMemory)异常。因此在展示高分辨率图片的时候,最好先将图片进行压缩,压缩后的图片大小应该和用来展示它的控件大小相近。这样能够兼顾显示效果和内存占用。
BitmapFactory.Options这个类。有一个字段叫做 inJustDecodeBounds 。
SDK中对这个成员的说明是这种:
If
set to true, the decoder will return null (no bitmap), but the out…
也就是说。假设我们把它设为true,那么BitmapFactory.decodeFile(String path, Options opt)并不会真的返回一个Bitmap给你,它只会把它的宽,高取回来给你,这样就不会占用太多的内存,也就不会那么频繁的发生OOM了。
以下我们通过详细实例来展示怎么实现缩略图。
1.布局文件:
2.MainActivity.java代码例如以下:
3.执行实例:
缩略图效果体现出了了。
喜欢的朋友能够关注我!谢谢!
BitmapFactory.Options这个类。有一个字段叫做 inJustDecodeBounds 。
SDK中对这个成员的说明是这种:
If
set to true, the decoder will return null (no bitmap), but the out…
也就是说。假设我们把它设为true,那么BitmapFactory.decodeFile(String path, Options opt)并不会真的返回一个Bitmap给你,它只会把它的宽,高取回来给你,这样就不会占用太多的内存,也就不会那么频繁的发生OOM了。
以下我们通过详细实例来展示怎么实现缩略图。
1.布局文件:
<?xml version="1.0" encoding="utf-8"? > <RelativeLayout 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/imageView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/mei" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_below="@+id/imageView1" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:src="@drawable/mei" /> </RelativeLayout>
2.MainActivity.java代码例如以下:
package org.yayun.demo; import java.io.InputStream; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView imageView1; private ImageView imageView2; Bitmap mBitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); } private void initView(){ imageView1=(ImageView)findViewById(R.id.imageView1); imageView2=(ImageView)findViewById(R.id.imageView2); //读取资源图片 mBitmap=readBitMap(); //对资源图片进行缩放 imageView2.setImageBitmap(zoomBitmap(mBitmap, mBitmap.getWidth()/4, mBitmap.getHeight()/4)); } /** * 读取资源图片 * @return */ private Bitmap readBitMap(){ BitmapFactory.Options opt=new BitmapFactory.Options(); /* * 设置让解码器以最佳方式解码 */ opt.inPreferredConfig=Bitmap.Config.RGB_565; //以下两个字段须要组合使用 opt.inPurgeable=true; opt.inInputShareable=true; /* * 获取资源图片 */ InputStream is=this.getResources().openRawResource(R.drawable.mei); return BitmapFactory.decodeStream(is, null, opt); } /** * 缩放图片 * @param bitmap * @param w * @param h * @return */ public Bitmap zoomBitmap(Bitmap bitmap, int w, int h) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidht = ((float) w / width); float scaleHeight = ((float) h / height); /* * 通过Matrix类的postScale方法进行缩放 */ matrix.postScale(scaleWidht, scaleHeight); Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); return newbmp; } }
3.执行实例:
缩略图效果体现出了了。
喜欢的朋友能够关注我!谢谢!
相关文章推荐
- Android监听返回键、Menu键、Home键、
- Android开发艺术探索小记(第一章)
- Android 工具类计划
- Android Fragment学习资料
- Android四大组件-Activity篇
- Android布局控件之LinearLayout详解
- 学习Android从0开始之基础篇(1)-Android的四大基本组件
- Android----------WindowManager
- android仿ios的时间滚动控件WheelView
- Android 应用增量升级 思路
- Android——SpannableString
- Android Studio快捷键整理
- Android Studio系列教程六--Gradle多渠道打包
- Android Studio系列教程五--Gradle命令详解与导入第三方包
- Android Studio系列教程四--Gradle基础
- Android Studio系列教程三--快捷键
- Android Studio系列教程二--基本设置与运行
- 开源控件MPAndroidChart的使用
- Android-->Fragment生命周期详解(下)
- Android Handler,Loop,HandlerThread消息处理