In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this article Xiaobian for you to introduce in detail "Android how to customize the analog clock control", the content is detailed, the steps are clear, the details are handled properly, I hope this "Android how to customize the analog clock control" article can help you solve your doubts, following the editor's ideas slowly in-depth, let's learn new knowledge.
Custom view- transparent analog clock display
Idea: rewrite view,1. The radius of the analog clock is obtained according to the width and height of the control. two。 Override the onDraw method to rotate the canvas at different angles to draw the dial center scale pointer
The code will be added directly here.
Custom TimeClockView:
Package com.eq.viewdemo;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.os.Handler;import android.os.Message;import android.text.TextPaint;import android.util.AttributeSet;import android.view.View;import java.util.Calendar;/** * Created by pc on 2017-3-29. * / public class TimeClockView extends View {private int width; private int height; private Paint mPaintLine; private Paint mPaintCircle; private Paint mPaintHour; private Paint mPaintMinute; private Paint mPaintSec; private TextPaint mPaintText; private Calendar mCalendar; public static final int START_ONDRAW = 0X23 / / call the redrawing method private Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {switch (msg.what) {case START_ONDRAW: mCalendar = Calendar.getInstance (); invalidate () in handler every second / / tell UI main thread to redraw handler.sendEmptyMessageDelayed (START_ONDRAW, 1000); break; default: break;}; public TimeClockView (Context context) {super (context);} public TimeClockView (Context context, AttributeSet attrs) {super (context, attrs) MCalendar = Calendar.getInstance (); mPaintLine = new Paint (); mPaintLine.setColor (Color.GREEN); mPaintLine.setStrokeWidth (2); mPaintLine.setAntiAlias (true); / / set anti-aliasing mPaintLine.setStyle (Paint.Style.STROKE); / / set painting style mPaintCircle = new Paint (); mPaintCircle.setColor (Color.RED); / / set color mPaintCircle.setStrokeWidth (2) / / set lineweight mPaintCircle.setAntiAlias (true); / / set whether anti-aliasing mPaintCircle.setStyle (Paint.Style.FILL); / / set drawing style mPaintText = new TextPaint (); mPaintText.setColor (Color.BLUE); mPaintText.setStrokeWidth (5); mPaintText.setTextAlign (Paint.Align.CENTER); mPaintText.setTextSize (30); mPaintHour = new Paint (); mPaintHour.setStrokeWidth (6) MPaintHour.setColor (Color.BLUE); mPaintHour.setAntiAlias (true); mPaintMinute = new Paint (); mPaintMinute.setStrokeWidth (4); mPaintMinute.setColor (Color.BLUE); mPaintMinute.setAntiAlias (true); mPaintSec = new Paint (); mPaintSec.setStrokeWidth (2); mPaintSec.setColor (Color.BLUE); mPaintSec.setAntiAlias (true); handler.sendEmptyMessage (START_ONDRAW) / send a message to handler to open redrawing} @ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure (widthMeasureSpec, heightMeasureSpec); width = getDefaultSize (getSuggestedMinimumWidth (), widthMeasureSpec); height = getDefaultSize (getSuggestedMinimumHeight (), heightMeasureSpec); setMeasuredDimension (width, height);} @ Override protected void onDraw (Canvas canvas) {super.onDraw (canvas); int circleRadius / / if (width > height) {circleRadius = height / 2-10;} else {circleRadius = width / 2-10;} / / draw the center of the circle canvas.drawCircle (width / 2, height / 2, 5, mPaintCircle); / / rotate the canvas in turn and draw each scale and the corresponding number for (int I = 1) I 9) {/ / minutes greater than 9 time = "0" + hour + ":" + minute + ": 0" + sec; / / 02:12:02} else if (sec > 9 & & hour
< 10 && minute < 10) {//秒大于9 time = "0" + hour + ":0" + minute + ":" + sec; //02:02:12 } else if (sec < 10 && hour >9 & & minute
< 10) {//时大于9 time = hour + ":0" + minute + ":0" + sec; //12:02:02 } else if (sec < 10 && hour >9 & & minute > 9) {/ / at 9 time = hour + ":" + minute + ": 0" + sec; / / 12:12:02} else if (sec > 9 & & hour > 9 & & minute
< 10) {//时秒大于9 time = hour + ":0" + minute + ":" + sec; //12:02:12 } else if (sec >9 & & hour
< 10 && minute >9) {/ / more than 9 time = "0" + hour + ":" + minute + ":" + sec; / / 02:12:12} else {time = hour + ":" + minute + ":" + sec; / / 12:12:12} / / the time below the drawing center displays mPaintText.setTextSize (circleRadius / 10 * 2); canvas.save () Canvas.drawText (time, width / 2, height / 2 + circleRadius / 10 * 4, mPaintText); / / the pointer corresponding to minutes and seconds float minuteDegree = minute / 60f * 360 canvas.rotate (); canvas.rotate (minuteDegree, width / 2, height / 2) Canvas.drawLine (width / 2, height / 2-circleRadius + circleRadius / 3, width / 2, height / 2 + circleRadius / 6, mPaintMinute); canvas.restore (); float hourDegree = (hour * 60 + minute) / 12f / 60 * 360 beat / get the angle of clock rotation canvas.save (); canvas.rotate (hourDegree, width / 2, height / 2) Canvas.drawLine (width / 2, height / 2-circleRadius + circleRadius / 2, width / 2, height / 2 + circleRadius / 9, mPaintHour); canvas.restore (); float secDegree = sec / 60f * 360 canvas.restore / get the second hand rotation angle canvas.save (); canvas.rotate (secDegree, width / 2, height / 2) Canvas.drawLine (width / 2, height / 2-circleRadius + 2, width / 2, height / 2 + circleRadius / 4, mPaintSec); canvas.restore ();}}
Call in the layout
Activity_main.xml:
After reading this, the article "how to customize the analog clock control by Android" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about the article, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.