Android's own definition of the view dashboard

Source: Internet
Author: User

Create a new project, create a new DashBoardView inherited self View OnGlobalLayoutListener -implementing interface, and override the OnDraw method.
The use of OnGlobalLayoutListener interfaces requires overriding onGlobalLayout methods. In this method we will get the width of the view.

Create a new variable such as the following:

private Context mContext;    private Paint mCirclePaint,mDegreePaint,mHourPaint,mMinPaint;    privateint mViewWidth,mViewHeight;

Initialize these variables:

public void init (context context) {Mcontext = context;Mcirclepaint = new Paint (Paint. ANTI_alias_flag);Mdegreepaint = new Paint (Paint. ANTI_alias_flag);Mhourpaint = new Paint (Paint. ANTI_alias_flag);Mminpaint = new Paint (Paint. ANTI_alias_flag);Mcirclepaint. Setantialias(true);Mcirclepaint. Setstrokewidth(5);Mcirclepaint. SetStyle(Paint. Style. STROKE);Mhourpaint. Setstrokewidth( -);Mminpaint. Setstrokewidth(Ten);Getviewtreeobserver (). Addongloballayoutlistener(this);}

onGlobalLayoutIn the initialization of the DashBoardView wide height:

@Override    publicvoidonGlobalLayout() {        mViewHeight = getHeight();        mViewWidth = getWidth();    }

Add, for example, the following drawing code in the OnDraw method:

/ * * Draw dial Round * */Canvas. Drawcircle(mviewwidth/2, mviewheight/2, mviewwidth/2, Mcirclepaint);        / * * Draw scale * */for (int i=0; i<24;i++) {if (i==0|| i==6|| i== A|| i== -){/ * * Draw the hourly tick * */Mdegreepaint. Setstrokewidth(5);Mdegreepaint. Settextsize( -);Canvas. DrawLine(mviewwidth/2, mviewheight/2-mviewwidth/2, mviewwidth/2, mviewheight/2-mviewwidth/2+ -, Mdegreepaint);String degree = string. ValueOf(i);Canvas. DrawText(degree,mviewwidth/2-mdegreepaint. Measuretext(degree)/2, mviewheight/2-mviewwidth/2+ -, Mdegreepaint);}else{Mdegreepaint. Setstrokewidth(3);Mdegreepaint. Settextsize( the);Canvas. DrawLine(mviewwidth/2, mviewheight/2-mviewwidth/2, mviewwidth/2, mviewheight/2-mviewwidth/2+ -, Mdegreepaint);String degree = string. ValueOf(i);Canvas. DrawText(degree,mviewwidth/2-mdegreepaint. Measuretext(degree)/2, mviewheight/2-mviewwidth/2+ -, Mdegreepaint);}/ * * To draw all the scales by rotating the canvas * */Canvas. Rotate( the, mviewwidth/2, mviewheight/2);}/ * * Draw the pointer * */Mhourpaint. Setstrokewidth( -);Mminpaint. Setstrokewidth(Ten);Canvas. Save();Canvas. Translate(mviewwidth/2, mviewheight/2);Canvas. DrawLine(0,0, -, -, Mhourpaint);Canvas. DrawLine(0,0, -, $, Mminpaint);Canvas. Restore();

So we're finished. A dashboard is drawn. For example, the following:

Full code Download: Full Project

Android's own definition of the view dashboard

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.