`

Android bitmap优化

阅读更多

直接使用ImageView显示bitmap会占用较多资源,特别是图片较大的时候,可能导致崩溃。

使用BitmapFactory.Options设置inSampleSize, 这样做可以减少对系统资源的要求。

 

属性值inSampleSize表示缩略图大小为原始图片大小的几分之一,即如果这个值为2,则取出的缩略图的宽和高都是原始图片的1/2,图片大小就为原始大小的1/4。

 

Options中有个属性inJustDecodeBounds,SDK中是这么说的

If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.

 

通过设置inJustDecodeBounds为true,获取到outHeight(图片原始高度)和outWidth(图片的原始宽度),然后计算一个inSampleSize(缩放值)

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView
android:id="@+id/imageview"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
/>
</LinearLayout>

 

 

 

 

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;
 
public class AndroidImage extends Activity {
 
private String imageFile = "/sdcard/AndroidSharedPreferencesEditor.png";
/** Called when the activity is first created. */
 
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
   
ImageView myImageView = (ImageView)findViewById(R.id.imageview);
//Bitmap bitmap = BitmapFactory.decodeFile(imageFile);
//myImageView.setImageBitmap(bitmap);
 
Bitmap bitmap;
float imagew = 300;
float imageh = 300;
 
BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options();
bitmapFactoryOptions.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeFile(imageFile, bitmapFactoryOptions);
 
int yRatio = (int)Math.ceil(bitmapFactoryOptions.outHeight/imageh);
int xRatio = (int)Math.ceil(bitmapFactoryOptions.outWidth/imagew);
 
if (yRatio > 1 || xRatio > 1){
 if (yRatio > xRatio) {
  bitmapFactoryOptions.inSampleSize = yRatio;
  Toast.makeText(this,
    "yRatio = " + String.valueOf(yRatio),
    Toast.LENGTH_LONG).show();
 }
 else {
  bitmapFactoryOptions.inSampleSize = xRatio;
  Toast.makeText(this,
    "xRatio = " + String.valueOf(xRatio),
    Toast.LENGTH_LONG).show();
 }
}
else{
 Toast.makeText(this,
   "inSampleSize = 1",
   Toast.LENGTH_LONG).show();
}
 
bitmapFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(imageFile, bitmapFactoryOptions);
myImageView.setImageBitmap(bitmap);
}
 
}
分享到:
评论
1 楼 sunny78117 2012-07-27  
谢谢,写得好

相关推荐

    Android Bitmap详解及Bitmap的内存优化

    Android Bitmap详解及Bitmap的内存优化 一、Bitmap: Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。 常用方法: ...

    Android Bitmap的加载优化与Cache相关介绍

    高效加载 Bitmap BitMapFactory 提供了四类方法: decodeFile,decodeResource,decodeStream 和 decodeByteArray 分别用于从文件系统,资源,输入流以及字节数组中加载出一个 Bitmap 对象。 高效加载 Bitmap 很简单,即...

    解析Android开发优化之:对Bitmap的内存优化详解

    这里就有疑问了,Android系统有自己的垃圾回收机制,可以不定期的回收掉不使用的内存空间,当然也包括Bitmap的空间。那为什么还需要这个方法呢? Bitmap类的构造方法都是私有的,所以开发者不能直接new出一个Bitmap...

    Android性能优化之Bitmap图片优化详解

    在Android开发过程中,Bitmap往往会给开发者带来一些困扰,因为对Bitmap操作不慎,就容易造成OOM(Java.lang.OutofMemoryError – 内存溢出),本篇博客,我们将一起探讨Bitmap的性能优化。 为什么Bitmap会导致OOM? 1...

    Android内存OOM优化详解.pdf

    Android 内存 OOM 优化和治理,Android 内存泄漏原因详解与应对措施,Android 中的 Bitmap 使用详解,Android 中的 Bitmap 内存治理和优化详解,Android 内存分析命令详解,Java内存模型。Android 内存 OOM 优化和...

    处理android bitmap oom

    安卓在加载大图的时候经常会出现oom的错误,给大家分享我的一些处理经验。 这个是优化后的版本。

    Android性能优化(七)Bitmap内存压缩示例源码BitMapCache.zip

    Android性能优化(七)Bitmap内存压缩示例源码

    Yv12 nv21 yuv420 转换为Bitmap

    Camera onPreview中byte[] 转换为Bitmap 在录像时设置了YV12要保存图片时 YV12或者NV12时要转换时所用的工具的优化

    Google 官方 Android DisplayBimaps的优化事例

    Google 官方 Android DisplayBimaps的优化事例,博客链接http://blog.csdn.net/chivalrousman/article/details/51553114

    android-tao-bitmap:Android位图库-旋转,翻转,调整大小,缩放,裁剪位图。 细心的内存管理

    android-tao-bitmap Android位图库。 变形金刚(制作为Builder)位图-旋转,翻转,调整大小,缩放,裁剪位图。 存在内存优化(默认情况下使用缓存文件)。 从视图转换位图。 使用遮罩创建形状的位图。

    Bitmap压缩技术

    Bitmap压缩技术 主流android图片优化技术

    Android内存优化杂谈

    Android内存优化是我们性能优化工作中比较重要的一环,这里其实主要包括两方面的工作: 1、优化RAM,即降低运行时内存。这里的目的是防止程序发生OOM异常,以及降低程序由于内存过大被LMK机制杀死的概率。另一方面,...

    Android--从系统Camera和Gallery获取图片优化

    Android--从系统Camera和Gallery获取图片优化http://www.cnblogs.com/plokmju/p/android_BitmapRecycle.html

    android内存优化之图片优化

    因此,改用先通过BitmapFactory.decodeStream方法,创建出一个bitmap,再将其设为ImageView的source,decodeStream最大的秘密在于其直接调用JNI&gt;&gt;nativeDecodeAsset()来完成decode,无需再使用java层的createBitmap...

    Android开发艺术探索.任玉刚(带详细书签).pdf

    本书是一本Android进阶类书籍,采用理论、源码和实践相结合的... 15.1.5 ListView和Bitmap优化 501 15.1.6 线程优化 501 15.1.7 一些性能优化建议 501 15.2 内存泄露分析之MAT工具 502 15.3 提高程序的可维护性 506

Global site tag (gtag.js) - Google Analytics