Last active
August 29, 2015 13:57
-
-
Save frankkienl/9589223 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nl.frankkie.randomapp; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.res.Resources; | |
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.os.Bundle; | |
import android.view.View; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.GregorianCalendar; | |
import java.util.Locale; | |
import java.util.TimeZone; | |
/** | |
* Draw a XKCD Style clock | |
* | |
* @author FrankkieNL | |
*/ | |
public class NowClockActivity extends Activity { | |
MyClockView clockView; | |
int yellowRingSize = 6; //as in x% from size | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); //To change body of generated methods, choose Tools | Templates. | |
initUI(); | |
} | |
public void initUI() { | |
clockView = new MyClockView(this); | |
setContentView(clockView); | |
} | |
public class MyClockView extends View { | |
public MyClockView(Context context) { | |
super(context); | |
} | |
Paint paint = new Paint(); | |
RectF outerCircle = null; | |
RectF innerCircle = null; | |
Bitmap clockImage = null; | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
//Get maximum square drawing size, my using smallest dimension | |
//Truesmart watch should have a square screen. | |
int size = Math.min(canvas.getHeight(), canvas.getWidth()); | |
int center = size / 2; | |
canvas.drawColor(Color.WHITE); | |
//Lets paint a clock | |
paint.setAntiAlias(true); | |
//draw outer circle | |
paint.setColor(Color.YELLOW); | |
paint.setStyle(Paint.Style.FILL); | |
if (outerCircle == null) { | |
outerCircle = new RectF(0, 0, size, size); | |
} | |
canvas.drawOval(outerCircle, paint); | |
//Draw hour-lines | |
paint.setColor(Color.BLACK); | |
paint.setStyle(Paint.Style.STROKE); | |
for (int i = 0; i < 24; i++) { | |
float x = (float) Math.cos(Math.toRadians((360.0 / 24.0) * i)) * center + center; | |
float y = (float) Math.sin(Math.toRadians((360.0 / 24.0) * i)) * center + center; | |
canvas.drawLine(center, center, x, y, paint); | |
} | |
//draw inner circle | |
paint.setColor(Color.WHITE); | |
paint.setStyle(Paint.Style.FILL); | |
if (innerCircle == null) { | |
innerCircle = new RectF(size / 10, size / 10, size - size / 10, size - size / 10); | |
} | |
canvas.drawOval(innerCircle, paint); | |
//draw earth | |
if (clockImage == null) { | |
Resources res = getResources(); | |
clockImage = BitmapFactory.decodeResource(res, R.drawable.nowclock_earth); | |
} | |
float timeDegrees = 0; | |
Calendar c = new GregorianCalendar(TimeZone.getTimeZone("UTC")); | |
int hour = c.get(Calendar.HOUR_OF_DAY); //24 | |
int min = c.get(Calendar.MINUTE); //60 | |
timeDegrees += (360.0/24.0)*(hour+12); //do make sure we count from noon, not from midnight | |
timeDegrees += ((360.0/24.0)/60.0)*min; | |
canvas.rotate(timeDegrees, center, center); | |
canvas.drawBitmap(clockImage, null, new Rect(size / 10, size / 10, size - size / 10, size - size / 10), paint); | |
canvas.rotate(-timeDegrees, center, center); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment