Android TV for full screen drawing vertical line

Source: Internet
Author: User

Custom View

With canvas double buffering, you can reduce the time of drawing and 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 Canavas 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 are printed as follows:
I/system.out: Time ===91



Method Two:

Staggeredview2.java

In the default way, it is a loop that draws a vertical line with a width. 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, efficiency is very low ...





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.