Custom components-draw a clock

Source: Internet
Author: User

Custom components-draw a clock

1,

Vc/y0sa2r3ijrM/yyc/Pwre9z/LSxraveS5jYW52YXO1xMSsyM/release + release/ytcS3xbTzsbbK/aOsec6qyvrWsbe9z/release + release/ Release/release/deqoaM8YnI + release/nOvcfQseSjrMbkyrW + zcrHsNHNvM/xtcS2pbK/u/K117K/zca1vdK7sd + release/release + LXEu + release MVzdG9yZdKqxeS21Mq508OjqHJlc3RvcmW/drawing + HS/drawing/ghost "brush: java;"> package com. example. canvastest. view; import java. util. calendar; import android. content. context; import android. graphics. canvas; import android. graphics. color; import androi D. graphics. paint; import android. graphics. paint. align; import android. graphics. paint. style; import android. graphics. path; import android. graphics. rectF; import android. OS. handler; import android. util. attributeSet; import android. util. log; import android. view. view; import com. example. canvastest. densityUtil; import com. example. canvastest. r;/*** draw the clock ** @ author libin **/public class ClockView extends View {// Radius private float radius = 200; private Paint mDownPaint; // width private float mCirclePaintWidth = 10; // Paint brush private Paint mOutSidePaint in the circle above; // draw the text private Paint mTextPaint; // text size: private int textFontSize = 16; private int longLine; private int outer line; private int widthLine; // the radius of the small center of the private int circle; private int hourDegrees; private int minDegrees; private int secDegrees; public ClockView (Context cont Ext) {super (context); init ();} public ClockView (Context context, AttributeSet attrs) {super (context, attrs); init ();} public ClockView (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); init ();} private void init () {mDownPaint = new (); mDownPaint. setColor (Color. BLACK); mDownPaint. setAntiAlias (true); mDownPaint. setStrokeWidth (mCirclePaintWidth); textFontSize = (Int) getResources (). getDimension (R. dimen. textSize); vertex line = (int) getResources (). getDimension (R. dimen. required line); longLine = (int) getResources (). getDimension (R. dimen. longLine); widthLine = (int) getResources (). getDimension (R. dimen. widthLine); radius = (int) getResources (). getDimension (R. dimen. circleRadius); circle = (int) getResources (). getDimension (R. dimen. circle); // the paint brush above mOutSidePaint = new Pai Nt (); mOutSidePaint. setAntiAlias (true); mOutSidePaint. setStrokeWidth (mCirclePaintWidth); mOutSidePaint. setStyle (Style. STROKE); mOutSidePaint. setColor (Color. BLACK); // the Paint brush above mTextPaint = new Paint (); mTextPaint. setAntiAlias (true); mTextPaint. setColor (Color. BLACK); mTextPaint. setStrokeWidth (widthLine); mTextPaint. setTextAlign (Align. CENTER); mTextPaint. setTextSize (textFontSize); mDownPaint. setTextSize (textFontSi Ze); mHandler. sendEmptyMessage (10) ;}@ Overrideprotected void onDraw (Canvas canvas) {// pan, move the coordinate origin of the Canvas to the left and right Directions x, and move y up and down. the default position of the canvas is (0, 0 ). // example: if the origin of the canvas falls on (), then the translate () is to move 10 on the X axis and Y axis respectively on the Origin, the origin is changed ). Canvas. translate (canvas. getWidth ()/2, canvas. getHeight ()/2); // move the position to the coordinate point of the painted paper: 150,150 // draw a circle canvas. drawCircle (0, 0, radius, mOutSidePaint); canvas. save (); // draw Path path Path = new path (); Path. addArc (new RectF (-radius,-radius, radius, radius),-180,180); mDownPaint. setTextAlign (Align. CENTER); int pathy = DensityUtil. dip2px (getContext (),-5); // display the text in the middle canvas. drawTextOnPath ("canvas Clock", path, 0, pathy, mDownPaint); canvas. restore (); // draw the scale canvas. save (); canvas. rotate (210); float y = radius; int count = 60; // The total number of scales for (int I = 0; I <count; I ++) {if (I % 5 = 0) {canvas. drawLine (0f, y-longLine + 2, 0, y + mCirclePaintWidth/2, mTextPaint); canvas. drawText (String. valueOf (I/5 + 1), 0, y-mCirclePaintWidth/2-longLine, mTextPaint);} else {canvas. drawLine (0f, y-plain line, 0f, y-mCirclePaintWidth/2, mTextPaint);} canvas. rotate (360/count, 0f, 0f); // rotating paper} canvas. restore (); // draw a canvas with seconds. save (); canvas. rotate (secDegrees); Paint hourPaint = new Paint (mDownPaint); hourPaint. setColor (Color. BLACK); hourPaint. setStrokeWidth (circle); canvas. drawLine (0,-longLine, 0, radius * 2/3, hourPaint); canvas. restore (); // draw the canvas of the sub-needle. save (); canvas. rotate (minDegrees); canvas. drawLine (0,-longLine, 0, radius * 1/2, hourPaint); canvas. restore (); // draw the canvas for the hour. save (); canvas. rotate (hourDegrees); canvas. drawLine (0,-longLine, 0, radius * 1/3, hourPaint); canvas. restore (); // Paint center black spot Paint tmpPaint = new Paint (mDownPaint); tmpPaint. setColor (Color. BLACK); canvas. drawCircle (0, 0, circle * 2, tmpPaint); // draw center reddot tmpPaint. setColor (Color. RED); canvas. drawCircle (0, 0, circle * 4/3, tmpPaint); super. onDraw (canvas);}/*** obtain angle */private void calculate () {Calendar ar calendar AR = Calendar ar. getInstance (); int hour = calendar. get (Calendar. HOUR); if (hour> 12) {hour = hour-12;} hourDegrees = hour * 30-180; int min = calendar. get (Calendar. MINUTE); minDegrees = min * 6-180; int sec = calendar. get (Calendar. SECOND); secDegrees = sec * 6-180; Log. I ("tag", "" + hour + "" + min + "" + sec);}/*** timer */private Handler mHandler = new Handler () {public void handleMessage (android. OS. message msg) {switch (msg. what) {case 10: calculate (); invalidate (); this. sendEmptyMessageDelayed (10,100 0); break; default: break ;}};};}4. Constant data

    
 
  14sp
     
 
  100dip
     
 
  9dip
     
 
  15dip
     
 
  5dip
     
 
  3dip
 



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.