First, look at the effect of the alarm clock page
Features to implement:
1: Turning pictures According to weather conditions change
2: Turning time can be set, the end of rotation, alarm sound on the end
3: Aperture Color Gradient effect
Directly on the code:
Package Com.yuekong.sirius.extension.customview;
Import Android.animation.Animator;
Import Android.animation.ValueAnimator;
Import Android.content.Context;
Import Android.content.res.TypedArray;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.Rect;
Import Android.graphics.RectF;
Import android.graphics.SweepGradient;
Import Android.media.Ringtone;
Import Android.media.RingtoneManager;
Import Android.net.Uri;
Import Android.util.AttributeSet;
Import Android.util.TypedValue;
Import Android.view.View;
Import Android.view.animation.LinearInterpolator;
Import COM.YUEKONG.SIRIUS.EXTENSION.R;
Import Com.yuekong.sirius.extension.util.ExtendUtil;
Import Java.text.SimpleDateFormat;
/** * Created by Zhongqi.shao on 2016/12/5.
* * public class Clockwakeview extends View {//The outer layer of the round color private int moutcirclecolor; The outer radius of a circle private int Moutcircleradis;
Inner circle color private int minnercirclecolor;
The radius of the inner circle is private int minnercircleradis;
private int mwidth;
private int mheight;
Default width private final int default_width = dp2px (240);
Default height private final int default_height = dp2px (240);
Default radius of the most outer circle private int default_out_radis = DP2PX (120);
The default radius of the inner circle private int default_inner_radis = DP2PX (105);
The default distance from the top of the header is private int default_title_padding_top = DP2PX (40);
The outermost rounded Paint private Paint moutcirclepaint;
Inner circle of Paint private Paint minnercirclepaint;
Title Paint private Paint Mtitlepaint;
Gradient arc private Paint mgradientarcpaint;
Private Sweepgradient msweepgradient;
Date Time Paint private Paint mtimepaint;
Private Paint Mdatepaint;
Time-delimited picture private Bitmap mdividerpic;
Weather picture private Bitmap mweatherpic;
Title Private String Mtitle;
Current time private long mcurrentdate = 1481010829605L;
Private SimpleDateFormat Mformat;
Private ringtone mringtone; The picture goes through a cycle in a few minutes. Calculates the private long mcountdowntim according to the music timeE
Countdown to turn over the angle private float mcurrentangle;
Countdown End Callback Private Countdownfinishlistener Mcountdownlistener;
Private context Mcontext;
Public Clockwakeview {This (context, NULL);
Mcontext = context;
Public Clockwakeview (context, AttributeSet attrs) {This (context, attrs, 0);
Mcontext = context;
Public Clockwakeview (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);
Mcontext = context;
TypedArray ta = context.obtainstyledattributes (attrs, R.styleable.siriusclockwakeview); Moutcirclecolor = Ta.getcolor (R.styleable.siriusclockwakeview_outcolor, Context.getresources (). GetColor (
R.color.out_circle_color));
Moutcircleradis = (int) ta.getdimension (R.styleable.siriusclockwakeview_outcircleradis, DEFAULT_OUT_RADIS);
Minnercirclecolor = Context.getresources (). GetColor (R.color.inner_circle_color);
Minnercircleradis = Default_inner_radis;
Mtitle = Context.getresources (). getString (R.string.clock); Ta.recycle ();
Initpaint ();
} private void Initpaint () {Mformat = new SimpleDateFormat ("MM month DD day _hh_mm_eeee");
Get delimited picture mdividerpic = Bitmapfactory.decoderesource (Getresources (), r.drawable.time_divider);
Mweatherpic = Bitmapfactory.decoderesource (Getresources (), r.drawable.icon_weather);
Outer circle of Paint moutcirclepaint = new Paint ();
Moutcirclepaint.setantialias (TRUE);
Moutcirclepaint.setstyle (Paint.Style.STROKE);
Moutcirclepaint.setstrokewidth (dp2px (5));
Moutcirclepaint.setcolor (Moutcirclecolor);
Inner circle of Paint minnercirclepaint = new Paint ();
Minnercirclepaint.setantialias (TRUE);
Minnercirclepaint.setstyle (Paint.Style.STROKE);
Minnercirclepaint.setstrokewidth (dp2px (5));
Minnercirclepaint.setcolor (Minnercirclecolor);
Title of Paint Mtitlepaint = new Paint ();
Mtitlepaint.setantialias (TRUE);
Mtitlepaint.setstyle (Paint.Style.FILL);
Mtitlepaint.setcolor (Mcontext.getresources (). GetColor (r.color.nav_highlighted));
Mtitlepaint.settextsize (sp2px (14));
Paint of Time Mtimepaint = new Paint (); MTImepaint.setantialias (TRUE);
Mtimepaint.setcolor (Color.White);
Mtimepaint.setstyle (Paint.Style.FILL);
Mtimepaint.settextsize (sp2px (70));
Date of Paint Mdatepaint = new Paint ();
Mdatepaint.setantialias (TRUE);
Mdatepaint.setcolor (Color.White);
Mdatepaint.setstyle (Paint.Style.FILL);
Mdatepaint.settextsize (sp2px (14));
Gradient arc Paint mgradientarcpaint = new Paint ();
Mgradientarcpaint.setantialias (TRUE);
Mgradientarcpaint.setstyle (Paint.Style.STROKE);
Mgradientarcpaint.setstrokewidth (dp2px (5));
Mgradientarcpaint.setstrokecap (Paint.Cap.BUTT);
Mgradientarcpaint.setstrokejoin (Paint.Join.MITER);
Sets the color of the gradient int[] colors = {0x00aea1ff, 0x40aea1ff, 0xffaea1ff, 0xffaea1ff};
Gradient msweepgradient = new Sweepgradient (0, 0, colors, null);
Mgradientarcpaint.setshader (msweepgradient); @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec, H
EIGHTMEASURESPEC);
Mwidth = Startmeasure (Widthmeasurespec); Mheight = Startmeasure (HeIGHTMEASURESPEC);
Setmeasureddimension (Mwidth, mheight);
} @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
Move the canvas to the Middle Canvas.translate (MWIDTH/2, MHEIGHT/2);
Draw the outermost circular drawoutcircle (canvas);
Draw Inner circle drawinnercircle (canvas);
Draw the middle separating picture drawdividerpic (canvas);
Draw the title drawtitle (canvas);
Drawing time Drawdatetime (canvas);
Draw a rotating picture drawlightarc (canvas);
private void Drawoutcircle (Canvas Canvas) {canvas.save ();
Canvas.drawcircle (0, 0, Moutcircleradis, moutcirclepaint);
Canvas.restore ();
private void Drawinnercircle (Canvas Canvas) {canvas.save ();
Canvas.drawcircle (0, 0, Minnercircleradis, minnercirclepaint);
Canvas.restore ();
private void Drawdividerpic (Canvas Canvas) {canvas.save ();
Canvas.drawbitmap (Mdividerpic, NULL, New Rect (-DP2PX (3),-dp2px (), dp2px (3), dp2px ()), NULL);
Canvas.restore ();
private void Drawtitle (Canvas Canvas) {canvas.save ();
if (Mtitle = null | | mtitle.length () <= 0) {return; } float TEXTWIDTH = Mtitlepaint.measuretext (Mtitle);
float baseLine = default_inner_radis-default_title_padding_top;
Canvas.drawtext (Mtitle,-TEXTWIDTH/2,-baseline, Mtitlepaint);
Canvas.restore ();
private void Drawlightarc (Canvas Canvas) {canvas.save ();
Canvas.rotate (Mcurrentangle);
RECTF rect = new RECTF (-minnercircleradis,-minnercircleradis, Minnercircleradis, Minnercircleradis);
Canvas.drawarc (Rect, -270, 180, false, Mgradientarcpaint); Canvas.drawbitmap (Mweatherpic, NULL, New Rect (-dp2px (),-minnercircleradis-dp2px (), dp2px (), dp2px (12)-
Minnercircleradis), NULL);
Canvas.restore ();
private void Drawdatetime (Canvas Canvas) {canvas.save ();
String timestr = Extendutil.getallformatstr (Mformat, mcurrentdate);
string[] Array = Timestr.split ("_");
String date = array[0];
String hour = array[1];
String minute = array[2];
String wake = array[3];
Float width = Mtimepaint.measuretext (hour); RECTF targetrect = new RECTF (-(width + dp2px),-sp2px (m)/2,-dp2px (), sp2px (70)/2);
Paint.fontmetricsint FontMetrics = Mtimepaint.getfontmetricsint (); int baseline = (int) (targetrect.top + targetrect.bottom-targetrect.top-fontmetrics.bottom + fontmetrics.top)/2-f
Ontmetrics.top);
Canvas.drawtext (hour,-width-dp2px), Baseline, mtimepaint);
float minutewidth = Mtimepaint.measuretext (minute);
RECTF minuterect = new RECTF (dp2px),-sp2px (m)/2, dp2px (+) + minutewidth, sp2px (70)/2);
Paint.fontmetricsint minutemetrics = Mtimepaint.getfontmetricsint (); int minuteline = (int) (Minuterect.top + (Minuterect.bottom-minuterect.top-minutemetrics.bottom + minuteMetrics.top)/
2-minutemetrics.top);
Canvas.drawtext (minute, dp2px), Minuteline, mtimepaint);
String datestr = date + "" + wake;
float datewidth = Mdatepaint.measuretext (DATESTR);
RECTF daterect = new RECTF (-DATEWIDTH/2, sp2px (), DATEWIDTH/2, sp2px () + sp2px (14));
Paint.fontmetricsint datemetrics = Mtimepaint.getfontmetricsint (); int dateline = (int) (dAterect.top + (Daterect.bottom-daterect.top-datemetrics.bottom + datemetrics.top)/2-datemetrics.top);
Canvas.drawtext (Datestr,-DATEWIDTH/2, Dateline, Mdatepaint);
Canvas.restore ();
private int startmeasure (int whspec) {int result = 0;
int size = Measurespec.getsize (WHSPEC);
int mode = Measurespec.getmode (WHSPEC);
if (mode = = measurespec.exactly) {result = size;
else {result = Default_width;
return result;
//Start Countdown calculation alarm end public void Startcountdown () {Valueanimator animator = valueanimator.offloat (0, 1.0f);
Animator.setduration (Mcountdowntime);
Constant Animator.setinterpolator (new Linearinterpolator ());
Non-cyclic animator.setrepeatcount (0); Animator.addupdatelistener (New Valueanimator.animatorupdatelistener () {@Override public void when listening to animations is worth changing in real time
Onanimationupdate (Valueanimator valueanimator) {mcurrentangle = (float) valueanimator.getanimatedvalue () * 360;
Invalidate ();
}
}); Animator.addlistener (New Animator.animatorlistener () {@Override public void Onanimationstart (animator animator) {} @Override public void Onanimationend (animator animator) {if (M
Ringtone!= null && mringtone.isplaying ()) {mringtone.stop ();
} if (Mcountdownlistener!= null) {mcountdownlistener.countdownfinished (); @Override public void Onanimationcancel (animator animator) {} @Override public void Onanimationrepeat (animator
Animator) {}});
Animator.start ();
Play the alarm bell ring URI notification = Ringtonemanager.getdefaulturi (ringtonemanager.type_ringtone);
Mringtone = Ringtonemanager.getringtone (mcontext, notification);
if (mringtone!= null &&!mringtone.isplaying ()) {mringtone.play ();
}//Provides a way to set the countdown to public void Setcountdowntime (long, Countdownfinishlistener listener) {mcountdowntime = time;
Mcountdownlistener = listener;
//Set the current date time public void Setcurrenttime (long currenttime) {mcurrentdate = CurrentTime;
Invalidate (); private int dp2px (int dp) {return (int) Typedvalue.applydimensIon (Typedvalue.complex_unit_dip, DP, Getresources (). Getdisplaymetrics ()); private int sp2px (int sp) {return (int) typedvalue.applydimension (typedvalue.complex_unit_sp, SP, getresources (). getd
Isplaymetrics ());
Public interface Countdownfinishlistener {//Countdown end operation void Countdownfinished (); }
}
The above is a small set to introduce the Android custom view to realize the alarm clock ringing function, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!