自定义View
public class MyView extends SurfaceView {
private float degree;
public float getDegree() {
return degree;
}
public void setDegree(float degree) {
this.degree = degree;
}
private SurfaceHolder.Callback callback;
private int width;
private int height;
private Paint mPaint;
private Paint mPaintText;
private String[] dicression={"南","东","北","西"};
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint=new Paint();
mPaint.setStrokeWidth(5);
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaintText=new Paint();
mPaintText.setTextSize(20);
mPaintText.setTextAlign(Paint.Align.CENTER);
mPaintText.setColor(Color.BLUE);
final SurfaceHolder holder=getHolder();
callback= new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
new Thread(new Runnable() {
@Override
public void run() {
while (true){
Canvas canvas=holder.lockCanvas();
canvas.drawColor(Color.WHITE);
canvas.drawCircle(width/2,height/2,200,mPaint);
canvas.drawCircle(width / 2, height / 2, 8, mPaint);
canvas.rotate(degree,width/2,height/2);
canvas.save();
for (int i=0;i<4;i++){
canvas.rotate(360/4f*i,width/2,height/2);
canvas.drawLine(width/2,height/2-200,width/2,height/2-170,mPaint);
canvas.drawText(dicression[i],width/2,height/2+220,mPaintText);
}
canvas.drawLine(width/2,height/2,width/2,height/2-150,mPaint);
canvas.restore();
holder.unlockCanvasAndPost(canvas);
}
}
}).start();
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
};
holder.addCallback(callback);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width=getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
height=getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec);
setMeasuredDimension(width,height);
mBitmap=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
}
}
MainActivity
public class MyView extends SurfaceView {
private float degree;
public float getDegree() {
return degree;
}
public void setDegree(float degree) {
this.degree = degree;
}
private SurfaceHolder.Callback callback;
private int width;
private int height;
private Paint mPaint;
private Paint mPaintText;
private String[] dicression={"南","东","北","西"};
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint=new Paint();
mPaint.setStrokeWidth(5);
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaintText=new Paint();
mPaintText.setTextSize(20);
mPaintText.setTextAlign(Paint.Align.CENTER);
mPaintText.setColor(Color.BLUE);
final SurfaceHolder holder=getHolder();
callback= new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
new Thread(new Runnable() {
@Override
public void run() {
while (true){
Canvas canvas=holder.lockCanvas();
canvas.drawColor(Color.WHITE);
canvas.drawCircle(width/2,height/2,200,mPaint);
canvas.drawCircle(width / 2, height / 2, 8, mPaint);
canvas.rotate(degree,width/2,height/2);
canvas.save();
for (int i=0;i<4;i++){
canvas.rotate(360/4f*i,width/2,height/2);
canvas.drawLine(width/2,height/2-200,width/2,height/2-170,mPaint);
canvas.drawText(dicression[i],width/2,height/2+220,mPaintText);
}
canvas.drawLine(width/2,height/2,width/2,height/2-150,mPaint);
canvas.restore();
holder.unlockCanvasAndPost(canvas);
}
}
}).start();
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
};
holder.addCallback(callback);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width=getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
height=getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec);
setMeasuredDimension(width,height);
mBitmap=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
}
}