通过使用AsyncTask的优化装载位图 [英] optimizing bitmap loading by using aSyncTask

查看:198
本文介绍了通过使用AsyncTask的优化装载位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力优化它加载了一堆瓦片构成一个大的位图我的单线程应用程序。该应用程序变得非常缓慢时,它会载入新砖到系统内存。林现在正在尝试使用异步任务用于此目的。该应用程序检测到这是平铺在留在一个被称为的onDraw方法上,创建一个包含在资产文件夹中的位图的路径的字符串,然后检查是否位图绘制之前空。如果为null,将其加载到内存中。我的想法是来处理DoBackground位图,并在postExecute触发视图无效,显示加载异步位图。几个问题:

1。)我可以逐条位异步我的任务是什么? (这句话:?新myAsyncTaskManager()执行(bitmapPath);如果没有,什么是去了解它,因为唯一异步会做的位图只是加载到内存的最好方法

2)是否有可能设置优先级的AsyncTask如果位图加载太慢?

3)是否有更好的方式去这件事吗? IM一定是位图加载,而不是在画布上绘制减慢的应用程序。

我的临时异步code:

 私有类myAsyncTaskManager扩展的AsyncTask<弦乐,太虚,字符串> {@覆盖
保护字符串doInBackground(字符串... bitmapPath){
      Log.e(SYS,我跑使用aTask);
        尝试{            bitmapArray [rectBeingDrawn] = BitmapFactory.de codeStream(assetManager.open(imagePathToLoad));
        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }返回null;
}
@覆盖
保护无效onPostExecute(字符串结果){
    //长耗时的操作结果的执行
    mCampusMap.invalidate();
}
}


解决方案

添加一个新的答案,你完全新的问题:)


  1. 取决于位图的数量。你有多少?你不希望创建几十个线程。毕竟,你只需要您的硬件一个核心,所以有多个线程不会买任何东西 - 上下文切换只会淹没它。如果你有吨的位图,你可能希望有位图的队列,并通过它的工作。为此,一个线程和处理程序实际上是更好的。


  2. 有。一般来说,我的工作线程的首要任务级比主线程低。


I have been trying to optimize my single thread app which loads a bunch of tiles that makeup a large bitmap. The app was becoming very sluggish when it would load the new tiles into system memory. Im now looking trying to use Async Tasks for this purpose. The app detects which tile is in the top left in a method called by onDraw, creates a string that contains the path of the bitmap in the Assets folder, and then checks to see if the bitmap is null before drawing. If it is null, it will load it into memory. My idea was to process the bitmap in DoBackground, and in postExecute trigger a view invalidate to display the async loaded bitmap. Few questions:

1.) can i execute my aSync task for each bitmap? (this statement: new myAsyncTaskManager().execute(bitmapPath); if not, what is the best way to go about it since the only thing aSync will do is just load bitmaps into memory?

2.) Is it possible to set the priority aSyncTask if the bitmaps load too slow?

3.) Is there a better way to go about this? im certain it is the bitmap loading, and not the canvas drawing that slows down the app.

My temporary aSync code:

private class myAsyncTaskManager extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... bitmapPath) {
      Log.e("sys","i ran using aTask");
        try {

            bitmapArray[rectBeingDrawn] = BitmapFactory.decodeStream(assetManager.open(imagePathToLoad));


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       return null;
}


@Override
protected void onPostExecute(String result) {
    // execution of result of Long time consuming operation
    mCampusMap.invalidate();
}
}

解决方案

Adding a new answer to your completely new question :)

  1. Depends on the amount of bitmaps. How many do you have? You don't want to create dozens of threads. After all, you only have one core on your hardware, so having multiple threads won't buy you anything - the context switching would just drown it out. If you have tons of bitmaps, you may want to have a queue of bitmaps and work through it. For that, a Thread and Handler would actually be better.

  2. It is. Generally, I set worker threads one priority level lower than the main thread.

这篇关于通过使用AsyncTask的优化装载位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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