<pre name="code" class="html">package com.paad.compass;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class MyCompass extends View {
private Paint criclePaint;
private Paint markPaint;
private Paint textPaint;
private int degree = 0;
public MyCompass(Context context) {
super(context);
// TODO Auto-generated constructor stub
initView(context);
}
public MyCompass(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
initView(context);
}
public MyCompass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
initView(context);
}
private void initView(Context context) {
Resources r = context.getResources();
criclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
criclePaint.setColor(r.getColor(R.color.background_color));
criclePaint.setStrokeWidth(1);
criclePaint.setStyle(Paint.Style.FILL_AND_STROKE);
markPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
markPaint.setColor(r.getColor(R.color.marker_color));
textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(r.getColor(R.color.text_color));
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
int cx = getMeasuredWidth() / 2;
int cy = getMeasuredHeight() / 2;
int radius = cx;
canvas.drawCircle(cx, cy, radius, criclePaint);
canvas.save();//保存背景
for (int i = 0; i < 60; i++) {
canvas.save();//保存繪製前狀態
int startX = cx;
int startY = cy - radius;
int stopX = cx;
int stopY = startY + 10;
int h = (int) markPaint.measureText("Y");
String text = "";
if (i % 15 == 0) {
switch (i) {
case 0:
text = "12";
break;
case 15:
text = "3";
break;
case 30:
text = "6";
break;
case 45:
text = "9";
break;
}
int textH = (int) textPaint.measureText("W");
canvas.drawText(text, startX - textH / 2, startY + 10 + 2 * h,
textPaint);
canvas.drawLine(startX, startY, stopX, stopY+5, markPaint);
}else{
canvas.drawLine(startX, startY, stopX, stopY, markPaint);
}
canvas.translate(0, h);
canvas.restore();//恢復到繪製前狀態,清除上面的translate(0, h)動作。準備下一次繪繪製
canvas.rotate(6, cx, cy);
}
canvas.save();
canvas.rotate((int)degree, cx, cy);
int h = (int) markPaint.measureText("Y");
canvas.drawLine(cx-5, cy-radius + 25 + 3 * h, cx, cy-radius + 20 + 2 * h, markPaint);
canvas.drawLine(cx+5, cy-radius + 25 + 3 * h, cx, cy-radius + 20 + 2 * h, markPaint);
canvas.restore();//清除上面的canvas.rotate((int)degree, cx, cy);動作
canvas.restore();//恢復到背景
handler.sendEmptyMessageDelayed(0, 1000);
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Log.d("tag", "degree="+degree);
if(degree>=360){
degree = 0;
}
invalidate();
degree+=6;
}
};
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
// TODO Auto-generated method stub
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
int specW = measure(widthMeasureSpec);
int specH = measure(heightMeasureSpec);
int d = Math.min(specW, specH);
setMeasuredDimension(d, d);
}
private int measure(int size) {
int result = 0;
int specMode = MeasureSpec.getMode(size);
int specSize = MeasureSpec.getSize(size);
if (specMode == MeasureSpec.UNSPECIFIED) {
result = 200;
} else {
result = specSize;
}
return result;
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background_color">#F555</color>
<color name="marker_color">#AFFF</color>
<color name="text_color">#AFFF</color>
</resources>