網(wǎng)站制作NEWS
如何優(yōu)化activity的加載速度
android 為了提高滾動(dòng)等各方面的繪制速度,可以為每一個(gè)view建立一個(gè)緩存,使用 View.buildDrawingCache為自己的view 建立相應(yīng)的緩存,
這 個(gè)所謂的緩存,實(shí)際上就是一個(gè)Bitmap對(duì)象。只是 這個(gè) bitmap 對(duì)象可以有多種格式而已,如
Bitmap.Config.ARGB_8888;
Bitmap.Config.ARGB_4444;
Bitmap.Config.ARGB_8888;
Bitmap.Config.ARGB_8888;
Bitmap.Config.RGB_565;
默認(rèn)的格式是Bitmap.Config.ARGB_8888.,但大多數(shù)嵌入式設(shè)備使用的顯示格式都是Bitmap.Config.RGB_565. 對(duì)于后者, 并沒(méi)有
alpha 值,所以繪制的時(shí)候不需要計(jì)算alpha合成,速遞當(dāng)讓快些。其次,RGB_565可以直接使用優(yōu)化了的memcopy函數(shù),效率相對(duì)高出許多。
所以, 在用buildDrawingCache建立緩存時(shí),可以使用RGB_565格式。但是如何制定這個(gè)格式呢 ?buildDrawingCache有兩個(gè)版本, buildDrawingCache(boolean)和buildDrawingCache()。并沒(méi)有任何參數(shù)可以設(shè)置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;}}
停止即為不過(guò),但是主板也有故障嫌疑的C3:內(nèi)存問(wèn)題D3、D4:此都為內(nèi)存問(wèn)題,但是主板內(nèi)存插槽也有可能
多重隨機(jī)標(biāo)簽