Android Open Source - lunatick Tick






From Project

Back to project page lunatick.

License

The source code is released under:

Copyright (c) 2014, a. bellenir All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1....

If you think the Android project lunatick listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.bellenir.lunatick;
/*from  w w w .j  ava2  s  .c  o  m*/
//Bitmap, Canvas, Color, Paint, RectF
import android.graphics.*;
import android.content.Context;
import java.util.Date;
import java.util.Calendar;

public class Tick {
  private static final int TIP = 0;
  private static final int BAK = 1;
  private static final int WT = 2;
  private static final double[] LU_HAND = {1.1, 1.06, 0.03};
  private static final double[] HR_HAND = {0.6, -0.1, 0.04};
  private static final double[] MN_HAND = {0.9, -0.1, 0.02};
  private static final double[] SC_HAND = {0.9, -0.1, 0.01};
  private static final double[] TK_MARK = {1, 0.9, 0.02};
  private static final double FACE_WT = 0.02;
  //todo: dynamic or spcify in dip
  public static final int RADIUS = 150;
  
  public static Bitmap getNao(boolean drawSeconds){
    int d = RADIUS * 2;
    Bitmap bmp = Bitmap.createBitmap(d, d, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bmp);
    drawNao(c, RADIUS, drawSeconds);
    return bmp;
  }
  
  public static void drawNao(Canvas c, int r, boolean drawSeconds){
    double ph = Luna.getCurrentPhase();
    drawHand(c, ph, LU_HAND, r);
    drawFace(c, r);
    Calendar now = Calendar.getInstance();
    //include minute precision to get hour hand positions between tic marks
    double hour = (now.get(Calendar.HOUR)*60+now.get(Calendar.MINUTE))/(60*12.0);
    //ditto for seconds
    double minute = (now.get(Calendar.MINUTE)*60+now.get(Calendar.SECOND))/(3600.0);
    double second = (now.get(Calendar.SECOND)/60.0);
    drawHandShadow(c, hour, HR_HAND, r);
    drawHandShadow(c, minute, MN_HAND, r);
    if(drawSeconds) drawHandShadow(c, second, SC_HAND, r);
    drawHand(c, hour, HR_HAND, r);
    drawHand(c, minute, MN_HAND, r);
    if(drawSeconds) drawHand(c, second, SC_HAND, r);
  }
  
  private static void drawFace(Canvas c, int r){
    Paint p = new Paint();
    p.setStrokeWidth((int)(r*FACE_WT));
    p.setColor(Color.WHITE);
    p.setStyle(Paint.Style.STROKE);
    int hcw = c.getWidth() / 2;
    int hch = c.getHeight() / 2;
    c.drawCircle(hcw, hch, r, p);
    double inc = 1/12.0; //amount to increment angle for each hour
    //int inr = r-((int)(r*TK_LEN_PCNT)); //inner radius
    for(double h=0; h<12; h+= inc){
      drawHand(c, h, TK_MARK, r);
      //drawRadialLine(c, h, inr, r, p);
    }
  }
  
  private static void drawRadialLine(Canvas c, double angle, int inner, int outer, Paint p){
    //to make clock calculations easier, angle 0 = 12:00, 0.25 = 3:00, 0.5 = 6:00, ...
    double rads = (angle * -2 * Math.PI) + (Math.PI/2);
    double cos = Math.cos(rads);
    double sin = Math.sin(rads);
    int hcw = c.getWidth()/2;
    int hch = c.getHeight()/2;
    float bx = (float)(hcw + cos * inner);
    float by = (float)(hch - sin * inner);
    float ex = (float)(hcw + cos * outer);
    float ey = (float)(hch - sin * outer);
    c.drawLine(bx, by, ex, ey, p);
  }
  private static void drawHand(Canvas c, double angle, double[] hand, int r){
    Paint p = new Paint();
    p.setColor(Color.WHITE);
    p.setStrokeWidth((int)(r*hand[WT]));
    drawRadialLine(c, angle, ((int)(r*hand[BAK])), ((int)(r*hand[TIP])), p);
  }
  private static void drawHandShadow(Canvas c, double angle, double[] hand, int r){
    Paint p = new Paint();
    p.setColor(Color.BLACK);
    p.setStrokeWidth((int)(3*r*hand[WT]));
    drawRadialLine(c, angle, ((int)(r*(hand[BAK]-hand[WT]))), ((int)(r*(hand[TIP]+hand[WT]))), p);
  }
}




Java Source Code List

com.bellenir.lunatick.Info.java
com.bellenir.lunatick.LunaTick.java
com.bellenir.lunatick.Luna.java
com.bellenir.lunatick.Tick.java
com.bellenir.lunatick.Widget.java