package com.example.compassdemo;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.WindowManager;
public class MainActivity extends Activity {
SurfaceView view;
SurfaceHolder s_hold;
//圆盘半径
int radius=200;
//屏幕的宽高
int screenWidth;
int screenHeight;
//圆盘原点
int cx;
int cy;
//画笔
Paint paint;
//文字
String TEXT="http://app.ewolian.com/pages/count.jsp";
//计数
int COUNT=60;
//计时器
Timer timer=new Timer();
TimerTask task=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WindowManager windowManager=getWindowManager();
Display display=windowManager.getDefaultDisplay();
DisplayMetrics metrics=new DisplayMetrics();
display.getMetrics(metrics);
//获取屏幕的宽高
screenHeight=metrics.heightPixels;//获取屏幕的高度
screenWidth=metrics.widthPixels;
//设置圆盘的圆心
cx=screenWidth/2;
cy=screenHeight/2-40;
view=(SurfaceView) findViewById(R.id.sView);
s_hold=view.getHolder();
s_hold.addCallback(new Callback() {
@Override
public void surfaceChanged(SurfaceHolder holder, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
//创建时绘制背景
drawBack(holder);
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
//销毁计时器
if(timer!=null){
timer.cancel();
}
}
});
task=new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
//获取系统当前的时间
Calendar c = Calendar.getInstance();
int minutes=c.getTime().getMinutes();
int seconds=c.getTime().getSeconds();
int hours=c.getTime().getHours();
Canvas canvas=s_hold.lockCanvas(new Rect(cx-radius+90,cy-radius+90,cx+radius-90,cy+radius-90));
canvas.drawColor(Color.BLUE);
drawMinutes(canvas,minutes);
drawSeconds(canvas,seconds);
drawHours(canvas,hours);
s_hold.unlockCanvasAndPost(canvas);
}
private void drawHours(Canvas canvas, int hours) {
// TODO Auto-generated method stub
canvas.save();
Paint p6=new Paint();
p6.setColor(Color.RED);
p6.setStyle(Paint.Style.FILL);
p6.setStrokeWidth(5);
canvas.rotate(360/12*hours,cx,cy);
canvas.drawLine(cx,cy,cx,cy-90, p6);
canvas.restore();
}
private void drawSeconds(Canvas canvas, int seconds) {
// TODO Auto-generated method stub
canvas.save();
Paint p4=new Paint();
p4.setColor(Color.GREEN);
p4.setStyle(Paint.Style.FILL);
p4.setStrokeWidth(2);
canvas.rotate(360/COUNT*seconds,cx,cy);
canvas.drawLine(cx,cy,cx,cy-130, p4);
canvas.restore();
}
private void drawMinutes(Canvas canvas, int minutes) {
// TODO Auto-generated method stub
Paint p5=new Paint();
p5.setColor(Color.YELLOW);
p5.setStyle(Paint.Style.FILL);
p5.setStrokeWidth(3);
canvas.save();
canvas.rotate(360/COUNT*minutes,cx,cy);
canvas.drawLine(cx, cy, cx,cy-110, p5);
canvas.restore();
}
};
timer.schedule(task,1000,1000);
}
//绘制背景
private void drawBack(SurfaceHolder holder){
//锁定SurfaceView画布
Canvas canvas=holder.lockCanvas();
//设置画布颜色
canvas.drawColor(Color.BLUE);
//画圆盘
drawCircle(canvas,radius);
//绘制文字
drawText(canvas,radius,TEXT);
//画刻度线
drawDividing(canvas,radius);
//解锁提交绘制
holder.unlockCanvasAndPost(canvas);
//holder.lockCanvas(new Rect(0,0,0,0));
//holder.unlockCanvasAndPost(canvas);
}
private void drawDividing(Canvas canvas, int radius) {
// TODO Auto-generated method stub
Paint p2=new Paint(paint);
p2.setTextSize(20);
for(int i=0;i<COUNT;i++){
if(i%5==0){
p2.setStrokeWidth(5);
canvas.drawLine(cx,cy-radius,cx,cy-radius-20, p2);
p2.setStrokeWidth(2);
if(i==0){
canvas.drawText(String.valueOf(12),cx-5,cy-radius-20-4, p2);
}else{
canvas.drawText(String.valueOf(i/5),cx-5,cy-radius-20-4, p2);
}
}else{
p2.setStrokeWidth(3);
canvas.drawLine(cx,cy-radius,cx,cy-radius-10, p2);
}
canvas.rotate(360/COUNT,cx,cy);
}
}
private void drawText(Canvas canvas, int radius, String text) {
// TODO Auto-generated method stub
canvas.save();
RectF rectf=new RectF(cx-radius+20,cy-radius+20,cx+radius-20,cy+radius-20);
Path path=new Path();
path.addArc(rectf,-180,359);
Paint p1=new Paint(paint);
p1.setStyle(Paint.Style.FILL);
p1.setTextSize(20);
p1.setStrokeWidth(1);
canvas.drawTextOnPath(text, path,0,0, p1);
canvas.restore();
}
private void drawCircle(Canvas canvas,int radius) {
// TODO Auto-generated method stub
paint=new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(8);
paint.setColor(Color.RED);
canvas.drawCircle(cx,cy,radius, paint);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(timer!=null){
timer.cancel();
}
}
}
</pre><pre name="code" class="java"><pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<SurfaceView
android:id="@+id/sView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>