Preface
Not long ago, I saw the simulated clock implemented by android on the Internet. It was very interesting. Here is the address:
Http://www.eoeandroid.com/forum.php? MoD = viewthread & tid = 58324 unfortunately this method does not exist
Stopwatch. I suddenly became interested and wanted to implement the following analog clock. After a while, I finally got it.
Now we will share the implementation methods and share them with you.
Not to mention, go first:
Preparations
First, we should prepare the relevant materials: the clock disk, the hour hand, the minute hand, the second hand picture.
Clock Disk:
Hour hand:
Split needle:
Seconds:
Source code
Configuration file, relatively simple:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context=".MainActivity" android:background="@color/bg"> <com.kiritor.mymodelclock.MyQAnalogClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /></LinearLayout>
Myqanalogclock code: used to construct the clock view and refresh the display:
Package COM. kiritor. mymodelclock; import android. util. attributeset; import COM. kiritor. mymodelclock. r; import android. content. context; import android. graphics. *; import android. graphics. drawable. bitmapdrawable; import android. OS. handler; import android. view. view; import Java. util. calendar; import Java. util. timezone;/*** created by kiritor on 13-5-30. */public class extends view {// clock disk, minute, second, And hour object bitmap mbmpdial; bitmap mbmphour; bitmap mbmpminute; bitmap mbmpsecond; describmdhour; describmdminute; describmdsecond; bitmapdrawable bmddial; paint mpaint; handler tickhandler; int mwidth; int mheigh; int mtempwidth = bmdsecond. getintrinsicwidth (); int mtempheigh; int centerx; int centery; int availablewidth = 100; int availableheight = 100; private string stimezonestring; Public myqanalogclock (context, attributeset ATTR) {This (context, "GMT + 8:00");} public myqanalogclock (context, string stime_zone) {super (context); stimezonestring = stime_zone; mbmphour = bitmapfactory. decoderesource (getresources (), R. drawable. shizhen); bmdhour = new bitmapdrawable (mbmphour); mbmpminute = bitmapfactory. decoderesource (getresources (), R. drawable. fenzhen); bmdminute = new bitmapdrawable (mbmpminute); mbmpsecond = bitmapfactory. decoderesource (getresources (), R. drawable. miaozhen); bmdsecond = new bitmapdrawable (mbmpsecond); mbmpdial = bitmapfactory. decoderesource (getresources (), R. drawable. android_clock_dial); bmddial = new bitmapdrawable (mbmpdial); mwidth = mbmpdial. getwidth (); mheigh = mbmpdial. getheight (); centerx = availablewidth/2; centery = availableheight/2; mpaint = new paint (); mpaint. setcolor (color. blue); run ();} public void run () {tickhandler = new handler (); tickhandler. post (tickrunnable);} private runnable tickrunnable = new runnable () {public void run () {postinvalidate (); tickhandler. postdelayed (tickrunnable, 1000) ;}}; protected void ondraw (canvas) {super. ondraw (canvas); Calendar Cal = calendar. getinstance (timezone. gettimezone (stimezonestring); int hour = Cal. get (calendar. hour); int minute = Cal. get (calendar. minute); int second = Cal. get (calendar. second); float hourrotate = hour * 30366f + minute/601_f * 30366f; float minuterotate = minute * 6.0f; float secondrotate = Second * 6.0f; Boolean scaled = false; if (availablewidth <mwidth | availableheight <mheigh) {scaled = true; float scale = math. min (float) availablewidth/(float) mwidth, (float) availableheight/(float) mheigh); canvas. save (); canvas. scale (scale, scale, centerx, centery);} bmddial. setbounds (centerx-(mwidth/2), centery-(mheigh/2), centerx + (mwidth/2), centery + (mheigh/2); bmddial. draw (canvas); mtempwidth = bmdhour. getintrinsicwidth (); mtempheigh = bmdhour. getintrinsicheight (); canvas. save (); canvas. rotate (hourrotate, centerx, centery); bmdhour. setbounds (centerx-(mtempwidth/2), centery-(mtempheigh/2), centerx + (mtempwidth/2), centery + (mtempheigh/2); bmdhour. draw (canvas); canvas. restore (); mtempwidth = bmdminute. getintrinsicwidth (); mtempheigh = bmdminute. getintrinsicheight (); canvas. save (); canvas. rotate (minuterotate, centerx, centery); bmdminute. setbounds (centerx-(mtempwidth/2), centery-(mtempheigh/2), centerx + (mtempwidth/2), centery + (mtempheigh/2); bmdminute. draw (canvas); canvas. restore (); mtempwidth = bmdsecond. getintrinsicwidth (); mtempheigh = bmdsecond. getintrinsicheight (); canvas. rotate (secondrotate, centerx, centery); bmdsecond. setbounds (centerx-(mtempwidth/2), centery-(mtempheigh/2), centerx + (mtempwidth/2), centery + (mtempheigh/2); bmdsecond. draw (canvas); If (scaled) {canvas. restore ();}}}
Main activity:
package com.kiritor.mymodelclock;import android.app.Activity;import android.os.Bundle;/** * Created by Kiritor on 13-5-30. */public class MainActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}
A simple analog clock is complete. However, we can see from the observation of the running effect that the author
The Design of hour hands, minute hands, and second-hand pictures is not very beautiful, but they are not very clear and experience is slightly poor. But this is not the focus
You can design the four pictures on your own to achieve different effects!
This example uses Android studio. The complete project source code is not uploaded! Over!