如何优化activity的启动速度

 我来答
匿名用户
2014-12-26
展开全部
android 为了提高滚动等各方面的绘制速度,可以为每一个view建立一个缓存,使用 View.buildDrawingCache为自己的view 建立相应的缓存,
这 个所谓的缓存,实际上就是一个Bitmap对象。只是 这个 bitmap 对象可以有多种格式而已,如
Bitmap.Config.ARGB_8888;
Bitmap.Config.ARGB_4444;
Bitmap.Config.ARGB_8888;
Bitmap.Config.ARGB_8888;
Bitmap.Config.RGB_565;
默认的格式是Bitmap.Config.ARGB_8888.,但大多数嵌入式设备使用的显示格式都是Bitmap.Config.RGB_565. 对于后者, 并没有
alpha 值,所以绘制的时候不需要计算alpha合成,速递当让快些。其次,RGB_565可以直接使用优化了的memcopy函数,效率相对高出许多。
所以, 在用buildDrawingCache建立缓存时,可以使用RGB_565格式。但是如何制定这个格式呢 ?buildDrawingCache有两个版本, buildDrawingCache(boolean)和buildDrawingCache()。并没有任何参数可以设置rgb格式,看看源码先:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 publicvoid buildDrawingCache(boolean autoScale){if((mPrivateFlags & DRAWING_CACHE_VALID)==0||(autoScale ?(mDrawingCache
==null|| mDrawingCache.get()==null):(mUnscaledDrawingCache ==null|| mUnscaledDrawingCache.get()==null))){if(ViewDebug.TRACE_HIERARCHY){ ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);}if(Config.DEBUG&& ViewDebug.profileDrawing){ EventLog.writeEvent(60002, hashCode());}int width = mRight - mLeft;int height = mBottom - mTop;final AttachInfo attachInfo = mAttachInfo;finalboolean scalingRequired = attachInfo !=null&& attachInfo.mScalingRequired;if(autoScale && scalingRequired){ width =(int)((width * attachInfo.mApplicationScale)+ 0.5f); height =(int)((height * attachInfo.mApplicationScale)+ 0.5f);}finalint drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;finalboolean opaque = drawingCacheBackgroundColor !=0||(mBGDrawable !=null&& mBGDrawable.getOpacity()== PixelFormat.OPAQUE);if(width // Projected bitmap size in bytes ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())){ destroyDrawingCache();return;}boolean clear =true; Bitmap bitmap = autoScale ?(mDrawingCache ==null?null: mDrawingCache.get()):(mUnscaledDrawingCache ==null?null: mUnscaledDrawingCache.get());if(bitmap
==null|| bitmap.getWidth()!= width || bitmap.getHeight()!= height){ Bitmap.Config quality;if(!opaque){switch(mViewFlags & DRAWING_CACHE_QUALITY_MASK){case DRAWING_CACHE_QUALITY_AUTO: quality = Bitmap.Config.ARGB_8888;break;case DRAWING_CACHE_QUALITY_LOW: quality = Bitmap.Config.ARGB_4444;break;case DRAWING_CACHE_QUALITY_HIGH: quality = Bitmap.Config.ARGB_8888;break;default: quality = Bitmap.Config.ARGB_8888;break;}}else{ quality = Bitmap.Config.RGB_565;}// Try to cleanup memoryif(bitmap !=null) bitmap.recycle();try{ bitmap = Bitmap.createBitmap(width, height, quality); bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);if(autoScale){ mDrawingCache =new SoftReference(bitmap);}else{ mUnscaledDrawingCache =new SoftReference(bitmap);}}catch(OutOfMemoryError e){// If there is not enough memory to create the bitmap cache, just// ignore the issue as bitmap caches are not required to draw the// view hierarchyif(autoScale){ mDrawingCache =null;}else{ mUnscaledDrawingCache =null;}return;} clear = drawingCacheBackgroundColor !=0;}Canvas canvas;if(attachInfo !=null){ canvas = attachInfo.mCanvas;if(canvas
==null){ canvas =newCanvas();} canvas.setBitmap(bitmap);// Temporarily clobber the cached Canvas in case one of our children// is also using a drawing cache. Without this, the children would// steal the canvas by attaching their own bitmap to it and bad, bad// thing would happen (invisible views, corrupted drawings, etc.) attachInfo.mCanvas=null;}else{// This case should hopefully never or seldom happen canvas =newCanvas(bitmap);}if(clear){ bitmap.eraseColor(drawingCacheBackgroundColor);} computeScroll();finalint restoreCount = canvas.save();if(autoScale && scalingRequired){finalfloat scale = attachInfo.mApplicationScale; canvas.scale(scale, scale);} canvas.translate(-mScrollX, -mScrollY); mPrivateFlags |= DRAWN;// Fast path for layouts with no backgroundsif((mPrivateFlags & SKIP_DRAW)== SKIP_DRAW){if(ViewDebug.TRACE_HIERARCHY){ ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);} mPrivateFlags &= ~DIRTY_MASK; dispatchDraw(canvas);}else{ draw(canvas);} canvas.restoreToCount(restoreCount);if(attachInfo !=null){// Restore the cached Canvas for our siblings attachInfo.mCanvas= canvas;} mPrivateFlags |= DRAWING_CACHE_VALID;}}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式