Android Custom View Draw Alarm

Source: Internet
Author: User
Tags cos sin

Android Custom View Draw Alarm

This article simply implements an alarm clock, extending the View,canvas drawing
The effect is as follows:

The code is as follows:

 PackageCom.gaofeng.mobile.clock_demo;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;ImportAndroid.view.View;ImportJava.util.Calendar;/** * Created by Gaofeng on 15-8-29. * * Public  class clockview extends View {     Public Clockview(Context context) {Super(context);        Ringhourbitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.ring);        Ringminbitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.ring_min);        Synctime (); Setschedualtime (0); } Public Clockview(context context, AttributeSet attrs) {Super(context, attrs); } Public Clockview(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); }//Radius and Circle Center    Private Static Final floatRadius = $;Private Static Final floatCenterX = -;Private Static Final floatCenterY = -;Private floatSecondsdegree =0;Private floatMindegree =0;Private floatHourdegree =0;//Timing    Private floatSethourdegree;Private floatSetmindegree;Private Booleansetschedual;PrivateBitmap Ringhourbitmap;PrivateBitmap Ringminbitmap;Private intBitmapwidth = -, Bitmapheight = -;@Override    protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas);Try{//Stopwatch ProgressThread.Sleep ( +); }Catch(Interruptedexception e)        {E.printstacktrace ();        } setbackgroundcolor (Color.green);        Drawcircle (canvas);        Drawclockpoint (canvas);        Drawindicator (canvas);        Calc ();        Drawschedual (canvas);    Invalidate (); }Private void drawschedual(Canvas canvas) {if(setschedual) {Paint p =NewPaint ();//Find the coordinates of the hour            floatrad = (float) Math.toradians (Sethourdegree);floatx = (float) (Radius * Math.sin (RAD));floaty = (float) (Radius * Math.Cos (RAD));floatVX = CenterX + x;floatVY = centery-y;//More stupid method to solve the picture drawn in the circle. The drawing starts from the top left corner of the phone.            if(Sethourdegree >0&& Sethourdegree <= -) {VX = Vx-bitmapwidth; }if(Sethourdegree > -&& Sethourdegree < the) {VX = Vx-bitmapwidth;            VY = Vy-bitmapwidth; }if(Sethourdegree > the&& Sethourdegree < the) {vy = Vy-bitmapwidth; } LOG.D ("","Range x:"+ VX +"y:"+ VY); Canvas.drawbitmap (RINGHOURBITMAP,VX, VY, p);//Find coordinates for minutesrad = (float) Math.toradians (Setmindegree); x = (float) (Radius * Math.sin (RAD)); y = (float) (Radius * Math.Cos (RAD));             VX = CenterX + x; VY = centery-y;if(Setmindegree >0&& Setmindegree <= -) {VX = Vx-bitmapwidth; }if(Setmindegree > -&& Setmindegree < the) {VX = Vx-bitmapwidth;            VY = Vy-bitmapwidth; }if(Setmindegree > the&& Setmindegree < the) {vy = Vy-bitmapwidth;            } canvas.drawbitmap (Ringminbitmap,vx,vy, p); p =NULL; }    }Private void Calc() {Secondsdegree = Secondsdegree +6;if(Secondsdegree >= the) {//lap for one minuteSecondsdegree =0; Mindegree = Mindegree +6; }if(Mindegree >= the) {//lap for one hoursMindegree =0; Hourdegree = Hourdegree +6; }if(Hourdegree >= the) {//lap or starting at 0 degreesHourdegree =0; }    }//Draw a circle    Private void drawcircle(Canvas canvas) {Paint paint =NewPaint ();        Paint.setcolor (color.red); Paint.setstrokewidth (2);        Canvas.drawcircle (Centerx,centery,radius,paint); Paint Paint2 =NewPaint ();        Paint2.setcolor (Color.yellow); Paint2.setstrokewidth ( thef);    Canvas.drawpoint (Centerx,centery,paint2); }Private void Drawclockpoint(Canvas canvas) {Paint paint =NewPaint ();        Paint.setcolor (Color.yellow); Paint.setstrokewidth (Tenf);//Draw The tick value above the alarm clock, a total of 12 points, 360 degrees         for(floatdegree =0; Degree <= the;d Egree = degree + -) {//mathematical formula find the dot coordinate above the circle            floatrad = (float) Math.toradians (degree);//Convert to degrees            floatx = (float) (Radius * Math.sin (RAD));floaty = (float) (Radius * Math.Cos (RAD));        Canvas.drawpoint (x + CenterX, centery-y,paint); }    }//Draw the pointer, turn the slow pointer thicker, the default will start from 0    Private void Drawindicator(Canvas canvas) {Paint paint =NewPaint ();        Paint.setcolor (Color.Black); Paint.setstrokewidth (3f);//Second hand_drawline (Canvas,secondsdegree,paint);//minute handPaint.setcolor (Color.dkgray); Paint.setstrokewidth (6f); _drawline (Canvas,mindegree,paint);//hourPaint.setcolor (Color.White); Paint.setstrokewidth ( Af);    _drawline (Canvas,hourdegree,paint); }Private void _drawline(Canvas canvas,floatDegree,paint Paint) {floatrad = (float) Math.toradians (degree);//Convert to degrees        floatx = (float) (Radius * Math.sin (RAD));floaty = (float) (Radius * Math.Cos (RAD));    Canvas.drawline (CenterX, centery, x + CenterX, centery-y, paint); } Public void Setschedualtime(LongTime) {Calendar cal = Calendar.getinstance (); Cal.settimeinmillis (time);//Demo        inthour = -;intMin = *;if(Hour > A) {//is a 24-hour typehour = hour- A; }//calculation is the same as beforeSethourdegree = Hour * -; Setmindegree = min *6; Setschedual =true; }//And phone time synchronization     Public void Synctime() {Calendar cal = Calendar.getinstance ();inthour = Cal.get (Calendar.hour_of_day);intmin = Cal.get (calendar.minute);intSecond = Cal.get (Calendar.second); LOG.D ("","Time now Hour:"+ Hour +"Min:"+ min +"Second:"+ second);if(Hour > A) {//is a 24-hour typehour = hour- A; }//Calculate radians each step is 60/360 = 6 degrees, Hour 12/360 degreesHourdegree = Hour * -; Mindegree = min *6; Secondsdegree = Second *6; }}

Summarize
-no numbers are drawn for clocks, this is just a simple drawing of text
-Determine if the timing picture is in the circle, do it relatively simple did not spend too much time to find a way
-If you want to implement the need to extend the pointer viewgroup

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Custom View draw alarm

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.