Created
July 9, 2014 15:28
-
-
Save ganadist/d2021b718ae0dba62af8 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 ganachoco.customclockface; | |
import android.app.Activity; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.Canvas; | |
import android.graphics.Matrix; | |
import android.graphics.Paint; | |
import android.graphics.drawable.Drawable; | |
import android.os.Bundle; | |
import android.os.SystemClock; | |
import android.support.wearable.view.WatchViewStub; | |
import android.view.SurfaceHolder; | |
import android.view.SurfaceView; | |
import android.widget.TextView; | |
public class MyActivity extends Activity { | |
private SurfaceView mSurfaceView; | |
private Bitmap mHourHand; | |
private Bitmap mMinHand; | |
private Bitmap mSecHand; | |
private android.text.format.Time mTimeFormat = new android.text.format.Time(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_my); | |
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); | |
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { | |
@Override | |
public void onLayoutInflated(WatchViewStub stub) { | |
mSurfaceView = (SurfaceView) stub.findViewById(R.id.canvas); | |
updateTime(); | |
} | |
}); | |
mHourHand = BitmapFactory.decodeResource(getResources(), R.drawable.clock_hour_hand); | |
mMinHand = BitmapFactory.decodeResource(getResources(), R.drawable.clock_minute_hand); | |
mSecHand = BitmapFactory.decodeResource(getResources(), R.drawable.clock_second_hand); | |
} | |
private void updateTime() { | |
long time = SystemClock.currentThreadTimeMillis(); | |
float secAngle = (((time / 1000.0f) % 60) * (float)Math.PI * 2 / 60); | |
float minAngle = (((time / 1000) % 3600) * (float)Math.PI * 2 / 3600); | |
float hourAngle = (((time / 1000) % 43200) * (float)Math.PI * 2 / 43200); | |
SurfaceHolder holder = mSurfaceView.getHolder(); | |
Canvas canvas = holder.lockCanvas(); | |
Matrix mat = new Matrix(); | |
Paint paint = new Paint(); | |
canvas.drawBitmap(mHourHand, mat, paint); | |
canvas.drawBitmap(mMinHand, mat, paint); | |
canvas.drawBitmap(mSecHand, mat, paint); | |
holder.unlockCanvasAndPost(canvas); | |
} | |
private BroadcastReceiver mTickReceiver = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
updateTime(); | |
} | |
}; | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
registerReceiver(mTickReceiver, new IntentFilter(Intent.ACTION_TIME_TICK)); | |
} | |
@Override | |
protected void onPause() | |
{ | |
unregisterReceiver(mTickReceiver); | |
super.onPause(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment