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(缩放值)


main.xml

<?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>

java源码

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);
}
 
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在 Android 上合成多个 Bitmap 图片,可以使用 Canvas 类和 Bitmap 类。下面是一个简单的示例: 1. 创建一个空的 Bitmap 对象,作为最终合成的图片: ```java Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(result); ``` 2. 将需要合成的 Bitmap 依次绘制到空的 Bitmap 上: ```java canvas.drawBitmap(bitmap1, x1, y1, null); canvas.drawBitmap(bitmap2, x2, y2, null); canvas.drawBitmap(bitmap3, x3, y3, null); ``` 3. 最后,可以保存合成后的 Bitmap 为图片文件或者显示在 ImageView 中: ```java imageView.setImageBitmap(result); ``` 完整的代码示例: ```java public Bitmap mergeBitmaps(Bitmap bitmap1, Bitmap bitmap2, Bitmap bitmap3) { int width = bitmap1.getWidth(); int height = bitmap1.getHeight() + bitmap2.getHeight() + bitmap3.getHeight(); Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(result); canvas.drawBitmap(bitmap1, 0, 0, null); canvas.drawBitmap(bitmap2, 0, bitmap1.getHeight(), null); canvas.drawBitmap(bitmap3, 0, bitmap1.getHeight() + bitmap2.getHeight(), null); return result; } ``` 以上代码将三个 Bitmap 垂直合成为一个 Bitmap,第一个 Bitmap 在最上面,第二个在中间,第三个在最下面。可以根据实际需求修改代码。 ### 回答2: Android中合成bitmap图片可以使用Canvas和Paint类来实现。步骤如下: 1. 创建一个新的Bitmap对象,用于存储合成后的图片。 2. 创建一个Canvas对象,并将新的Bitmap对象与Canvas关联起来。 3. 创建一个Paint对象,并设置相关的合成属性,如颜色、透明度、画笔风格等。 4. 使用Canvas的drawBitmap()方法将多个Bitmap对象绘制到新的Bitmap上,实现图片的合成效果。 5. 最后,可以将合成后的Bitmap保存到本地文件或者显示在界面上。 以下是一个简单的示例代码: ```java // 创建合成后的Bitmap对象 Bitmap resultBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // 创建Canvas对象,并与新的Bitmap关联 Canvas canvas = new Canvas(resultBitmap); // 创建Paint对象 Paint paint = new Paint(); // 设置合成属性 paint.setColor(Color.RED); paint.setAlpha(128); paint.setStyle(Paint.Style.FILL); // 绘制bitmap1 canvas.drawBitmap(bitmap1, matrix1, paint); // 绘制bitmap2 canvas.drawBitmap(bitmap2, matrix2, paint); // 绘制bitmap3 canvas.drawBitmap(bitmap3, matrix3, paint); // 可以依次绘制更多的bitmap ... // 保存合成后的Bitmap到本地文件 resultBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); ``` 以上代码中,width和height表示合成后的Bitmap的宽度和高度,bitmap1、bitmap2、bitmap3等表示待合成的原始Bitmap对象,matrix1、matrix2、matrix3等表示对相应的Bitmap进行变换的Matrix对象。 通过以上步骤,就可以实现Android中的Bitmap图片合成。 ### 回答3: Android中的Bitmap是一个表示图像的类,可以用来显示图片、进行图像处理等操作。要实现Bitmap的图片合成,可以通过以下步骤: 1. 创建一个新的Bitmap对象,用于存储合成后的图片。可以使用Bitmap的createBitmap()方法,指定宽度、高度和颜色格式创建一个空白的Bitmap对象。 2. 获取要合成的原始图片。可以使用BitmapFactory的decodeResource()方法,从资源文件中加载图片,并通过BitmapFactory.Options对象设置图片的缩放比例、色彩模式等参数。 3. 将原始图片绘制到新的Bitmap对象上。可以使用Canvas的drawBitmap()方法,在新的Bitmap上绘制原始图片。可以设置合成图片的位置、大小等属性。 4. 如果需要合成多张图片,重复步骤2和步骤3,将其他图片依次绘制到新的Bitmap对象上。 5. 最后,可以将合成后的Bitmap对象进行保存或显示。可以使用Bitmap的compress()方法将Bitmap对象保存到指定的输出流中,或使用ImageView等控件的setImageBitmap()方法显示合成后的图片。 需要注意的是,图片合成可能会消耗较大的内存和处理时间,特别是在合成大尺寸图片或大量图片时。为了避免内存溢出和性能问题,可以适当进行图片的压缩、缩放或分块处理,或使用异步处理方式进行合成。此外,可以通过Bitmap的回收和复用来优化内存使用。 总之,通过创建新的Bitmap对象,获取原始图片,绘制到新的Bitmap上,就可以实现Android中的图片合成功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值