优化的Andr​​oid code片段 - 更好的设计方法呢? [英] optimize android code snippet - better design approach?

查看:105
本文介绍了优化的Andr​​oid code片段 - 更好的设计方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code的这个片段,我想对它进行优化。我有一个是被OSMdroid库经常打电话来加载吨maptiles的方法。这一方法是直接调用文件流,并直接载入位图并返回一旦加载主UI线程上的位图。

虽然我已经成功使用的AsyncTask 平行执行人在后台运行。有时,在这个片段中code的运行速度较慢的 GC_FO_ALLOC的MapView覆盖(逐项)的大量数定期触发分配,并在我的日志消息我得到成长堆(frag的情况下)。我试过很多方法可以解决,但不够有效。出于某种原因,正在于主线程中执行这个任务我感觉,在我的日志消息我也得到跳过XX框架,应用程序可能会做很多任务的。任何想法,这可怎么做更好?问题是方法必须通过我怎么能允许这种方法要等到图形页面不平移或缩放,然后加载瓷砖?尽快恢复回来,因为它装载有

  @燮pressWarnings(德precation)
    @覆盖
    公众可绘制getDrawable(最终的InputStream aFileInputStream)抛出LowMemoryException {        尝试{
            DF =新DisplayFile();
            df.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,aFileInputStream);
            返回新BitmapDrawable(df.get());
        }赶上(最终的OutOfMemoryError E){
            System.gc()的;
        }赶上(InterruptedException的E){
            e.printStackTrace();
        }赶上(为ExecutionException E){
            e.printStackTrace();
        }
        返回null;
    }私有类DisplayFile扩展的AsyncTask< InputStream中,位图,位图> {        InputStream的路径;        @覆盖
        保护位图doInBackground(InputStream的......为arg0){
            PATH =将arg0 [0];
            BitmapFactory.Options mBitOpt =新BitmapFactory.Options();
            mBitOpt.inDither = FALSE;
            mBitOpt.inSampleSize = 1;
            mBitOpt.inPurgeable =真;
            mBitOpt.inInputShareable =真;
            mBitOpt.in preferredConfig = Bitmap.Config.ARGB_8888;
            最后的位图mBitmap = BitmapFactory.de codeStream(路径,空,mBitOpt);
            返回mBitmap;
        }
    }


解决方案

据我了解,你试图显示使用osmdroid离线地图。

Android是无法在内存中加载吨地图图块的,因为它只是不为每个应用程序提供足够的内存堆。

这就是为什么osmdroid拥有先进的机制(后台加载,缓存LRU,...)

你为什么不只是使用这些标准osmdroid机制?

看看这个帖子关于离线的更多详细信息映射在osmdroid:<一href=\"http://stackoverflow.com/questions/22862534/download-maps-for-osmdroid/22868462#22868462\">Download地图osmdroid

I've this snippet of code which I would like to optimize it. I've a method which is been called regularly by the OSMdroid library to load tons of maptiles. This method directly invokes the filestream and load the bitmap directly and will return the bitmap once loaded on the main UI thread.

Although I've managed to run in the background using AsyncTask with parallel executor. Sometimes with plenty number of overlays (itemized) in the mapview this snippet of code runs slower as GC_FO_ALLOC is triggered regularly for allocation, and in my log messages I get Grow Heap (frag case). I tried many ways to work around, but not effective enough. For some reason this task is being executed on main thread is my feeling as in my log messages I also get Skipped xx frames, the application may be doing lot of task. Any idea how can this be made better? The thing is method has to return back, as soon as it loads, there by how can I allow this method to wait until mapview is not panned or zoomed, then load the tiles?

@SuppressWarnings("deprecation")
    @Override
    public Drawable getDrawable(final InputStream aFileInputStream) throws LowMemoryException {

        try {
            df = new DisplayFile();
            df.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, aFileInputStream);
            return new BitmapDrawable(df.get());
        } catch (final OutOfMemoryError e) {
            System.gc();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }

private class DisplayFile extends AsyncTask<InputStream, Bitmap, Bitmap> {

        InputStream path;

        @Override
        protected Bitmap doInBackground(InputStream... arg0) {
            path = arg0[0];
            BitmapFactory.Options mBitOpt = new BitmapFactory.Options();
            mBitOpt.inDither = false;
            mBitOpt.inSampleSize = 1;
            mBitOpt.inPurgeable = true;
            mBitOpt.inInputShareable = true;
            mBitOpt.inPreferredConfig = Bitmap.Config.ARGB_8888;
            final Bitmap mBitmap = BitmapFactory.decodeStream(path,null,mBitOpt);
            return mBitmap;
        }
    }

解决方案

As far as I understand, you are trying to display an offline map using osmdroid.

Android is unable to load "tons of map tiles" in memory, because it just doesn't provide enough memory heap to each application.

This is why osmdroid has advanced mechanisms (background loading, LRU cache, ...)

Why are you not just using these standard osmdroid mechanisms?

Look at this post for more details about off-line maps on osmdroid: Download maps for osmdroid

这篇关于优化的Andr​​oid code片段 - 更好的设计方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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