Their definition of view
Adopt canvas double buffering, which can reduce rendering time. Improve performance.
Staggeredview.java
The source code is as follows:
Package Com.xxx.demo;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.Canvas; Import Android.graphics.color;import Android.graphics.paint;import Android.view.view;import Android.view.windowmanager;public class Staggeredview extends view {int width; int height; Paint p; Bitmap Bitmap = null; Canvas x = null; Public Staggeredview (Context context) {super (context); WindowManager wm = (WindowManager) getcontext (). Getsystemservice (Context.window_service); width = Wm.getdefaultdisplay (). GetWidth (); Height = Wm.getdefaultdisplay (). GetHeight (); p = new Paint (); private void Initmycanvas () {p.setcolor (color.white);//Set Green bitmap = Bitmap.createbitmap (bitm Ap. config.argb_8888); Create Canvas. x = new Canvas (); X.setbitmap (bitmap); P.setstyle (Paint.Style.STROKE); P.setantialias (TRUE); for (int i = 0; I <=20; i++) { ChangeColor (I, p); X.drawline (i, 0, I, p),//Draw line}} @Override protected void OnDraw (canvas canvas) {Super.ondraw ( Canvas); Canvas.drawcolor (Color.White); Create a brush long starttime=system.currenttimemillis (); Initmycanvas (); int bitwidth = Bitmap.getwidth (); int bitheight = Bitmap.getheight (); for (int i = 0; i < height; i + = Bitheight) {for (int j = 0; J < width; j + = Bitwidth) {// ChangeColor (i,j,p);//Canvas.drawpoint (i, J, p); Canvas.drawbitmap (Bitmap, J, I, p); }} System.out.println ("time = = =" + (System.currenttimemillis ()-starttime)); }/** * Change the color of the brush * * @param i * @param p */private void ChangeColor (int i, Paint p) {SWI TCH (i% 2) {case 0:p.setcolor (color.white); Break Case 1:p.setcolor (Color.blacK); Break } }}
First, draw a small rectangular area, create a canvas to draw on the bitmap, then use the system's Canavas to reuse the front bitmap, can improve the efficiency of processing.
Mainactivity.java
public class Mainactivity extends Activity { @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Requestwindowfeature (window.feature_no_title); Setcontentview (New Staggeredview (This));} }
The results of the printing are as follows:
I/system.out: Time ===91
Method Two:
Staggeredview2.java
In the default way. is a circle, draw width a vertical line. The efficiency is still very low.
。
public class StaggeredView2 extends View {int width; int height; Paint p; Bitmap Bitmap = null; Canvas x = null; Public StaggeredView2 (Context context) {super (context); WindowManager wm = (WindowManager) getcontext (). Getsystemservice (Context.window_service); width = Wm.getdefaultdisplay (). GetWidth (); Height = Wm.getdefaultdisplay (). GetHeight (); p = new Paint (); Initmycanvas (); private void Initmycanvas () {p.setcolor (color.white);//Set Green bitmap = Bitmap.createbitmap (bitm Ap. config.argb_8888); Create Canvas. x = new Canvas (); X.setbitmap (bitmap); P.setstyle (Paint.Style.STROKE); P.setantialias (TRUE); for (int i = 0; I <=; i++) {ChangeColor (I, p); X.drawline (i, 0, I, +, p);//Draw line}} @Override protected void OnDraw (canvas canvas) {Super.ondraw ( Canvas); Canvas.drawcolor (Color.White); Create a brush long starttime=system.currenttimemillis (); for (int i = 0; i < width; i++) {ChangeColor (I, p); Canvas.drawline (i,0, I, height-1, p); } System.out.println ("time = = =" + (System.currenttimemillis ()-starttime)); }/** * Change the color of the brush * * @param i * @param p */private void ChangeColor (int i, Paint p) {SWI TCH (i% 2) {case 0:p.setcolor (color.white); Break Case 1:p.setcolor (Color.Black); Break } }}
The result is:
I/system.out: Time ===277
Relative to the previous approach. The efficiency is very low.
。
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Android TV Full Screen vertical drawing