机器人,创建多个位图时createScaledBitmap放缓 [英] Android , createScaledBitmap slow when creating multiple bitmaps

查看:175
本文介绍了机器人,创建多个位图时createScaledBitmap放缓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code创建一个调整位图时,code工作好,但它的速度很慢。问题是,当我尝试使用它进行多个图像电话冻结,直到创建的映像。

下面是我的code:

 位图位= NULL;
文件imgFile =新的文件(my_pic_file);
如果(imgFile.exists()){
  位= BitmapFactory.de codeFILE(imgFile.getAbsolutePath());
  位图resizedBitmap = Bitmap.createScaledBitmap(位图,300,200,FALSE);
  first_image.setImageBitmap(resizedBitmap);
}


解决方案

现在的问题是,你正在做在主线程的工作。这意味着,直到所有的处理完成将冻结的用户界面。您可以使用一个线程或异步执行的任务,无论是解决这个问题。

 私有类LoadImageTask扩展的AsyncTask<弦乐,太虚,Bitamp> { 私人ImageView的mImageView = NULL; 公共LoadImageTask(ImageView的ImageView的){    mImageView = ImageView的;
 } 保护位图doInBackground(字符串...文件){    位图位图= NULL;
    位图resizedBitmap = NULL;
    文件imgFile =新的文件(文件);
    如果(imgFile.exists()){
      位= BitmapFactory.de codeFILE(imgFile.getAbsolutePath());
      resizedBitmap = Bitmap.createScaledBitmap(位图,300,200,FALSE);
    }    返回resizedBitmap;
 } 保护无效onPostExecute(位图结果){
     如果(结果=空&安培;!&安培;!mImageView = NULL){        mImageView.setImageBitmap(结果);
     }
 }

然后在你的code就叫

 新LoadImageTask(first_image).execute(my_pic_file);

I'm using the following code to create a resized bitmap, the code works okay, but it's very slow. The problem is that when I try to use this for more than one image the phone freezes until the images are created.

Here is my code:

Bitmap bitmap = null;
File imgFile = new File(my_pic_file);
if(imgFile.exists()) {
  bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
  Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, 300, 200, false);
  first_image.setImageBitmap(resizedBitmap);
}

解决方案

The problem is that you are doing the work on the main thread. This means that it will freeze the UI until all the processing is finished. You can fix this by either using a thread or asynchronous task.

 private class LoadImageTask extends AsyncTask<String, Void, Bitamp> {

 private ImageView mImageView = null;

 public LoadImageTask(ImageView imageView) {

    mImageView = imageView;
 }

 protected Bitmap doInBackground(String... file) {

    Bitmap bitmap = null;
    Bitmap resizedBitmap = null;
    File imgFile = new File(file);
    if(imgFile.exists()) {
      bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
      resizedBitmap = Bitmap.createScaledBitmap(bitmap, 300, 200, false);
    }

    return resizedBitmap;
 }

 protected void onPostExecute(Bitmap result) {
     if (result != null && mImageView != null) {

        mImageView.setImageBitmap(result);
     }
 }

Then in your code just call

new LoadImageTask(first_image).execute(my_pic_file);

这篇关于机器人,创建多个位图时createScaledBitmap放缓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆