利用自定义View 绘画时钟,实现时针,分针,秒针
package com.example.liyang.customview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Shader;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import java.util.Calendar;
/**
* paint a clock
* Created by liyang on 2016/3/28.
*/
public class ClockView extends View implements Handler.Callback{
private Paint paint;
private Handler handler = new Handler(this);
private Bitmap bitmap;
private BitmapShader bitmapShader;
public ClockView(Context context) {
this(context,null);
}
public ClockView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint = new Paint();
handler.sendEmptyMessage(0);
bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.img1);
bitmapShader = new BitmapShader(bitmap,BitmapShader.TileMode.CLAMP,BitmapShader.TileMode.CLAMP);
}
public ClockView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//draw a clock
// between 200px
// we can translate canvas to clock'center
canvas.save();
// make clock in center
canvas.translate(getWidth()/2,getHeight()/2);
float min = Math.min(getWidth()/200.0f,getHeight()/200.f);
canvas.scale(min,min);// let clock bigger
canvas.drawColor(Color.WHITE);
// draw backGround-----------------------------------
canvas.save();
Paint temp = new Paint();
temp.setShader(bitmapShader);
canvas.translate(-100, -100);
canvas.drawCircle(100, 100, 100,temp);
canvas.restore();
//----------------------------------------------------------------
// canvas.translate(100,100);
//canvas.drawColor(Color.WHITE);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
canvas.drawCircle(0,0,100,paint);
// draw 1 2 ..
canvas.save();
for (int i =0; i<12;i++){
if (i%3==0){
paint.setStrokeWidth(7);
canvas.drawLine(0,-100,0,-85,paint);
}else
paint.setStrokeWidth(5);
canvas.drawLine(0,-100,0,-90,paint);
canvas.rotate(30,0,0);//默认的锚点就是 00 写不写都行
}
// only after translate
canvas.restore();
Calendar calendar = Calendar.getInstance();
//draw miao
canvas.save();
paint.setStrokeWidth(3);
paint.setColor(Color.RED);
//draw
// calculate rotate one circle is 360 so * 360
canvas.rotate(calendar.get(Calendar.SECOND)*6,0,0);
// draw miao
canvas.drawLine(0,10,0,-90,paint);
canvas.restore();
// ------------------minutes
canvas.save();
// move to init
canvas.rotate(calendar.get(Calendar.MINUTE)*6+calendar.get(Calendar.SECOND)*6f/60,0,0);
//draw line
canvas.drawLine(0,10,0,-80,paint);
canvas.restore();
// ------------------hour
canvas.save();
canvas.rotate(calendar.get(Calendar.HOUR)*30+calendar.get(Calendar.SECOND)*6f/3600,0,0);
canvas.drawLine(0,10,0,-50,paint);
canvas.restore();
// as same start
canvas.restore();
}
@Override
public boolean handleMessage(Message msg) {
switch (msg.what){
case 0:
// 通知画布去 刷新
//同步刷新 通知 快 缺点是 只能在主线程 中有用
invalidate();
handler.sendEmptyMessage(0);
//异步通知 优点是 : 可以在子线程中 使用 缺点是慢
// postInvalidate();
break;
}
return true;
}
}