这个案例是通过view来制作一个Android时钟
//创建一个类继承view
public class ClockView extends View {
private static final String TAG = "ClockView";
private final Paint mPaint2;
private final Paint mPaint3;
private final Paint mPaint4;
private final Paint mPaint5;
private int mCx;
private int mCy;
private int mRadius;
private final Paint mPaint;
private final Paint mPaint1;
private int CIRCLESIZE=3; //环到外边框的距离
private int DIGITSLONG=20;//长刻度距离
private int DIGISTSHART=10;//断刻度距离
private int mTextsize=20;//文本的大小
private Bitmap mbitmap;
private int mHourDegree;
private int mMinuteDegree;
private int mSecondDegree;
private Paint mBackgroundPaint;
public ClockView(Context context, AttributeSet attrs) {
super(context, attrs);
//圆环的笔
mPaint = new Paint();
mPaint.setStrokeWidth(5);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setAntiAlias(true);
mPaint.setColor(Color.GRAY);
//刻度的笔
mPaint1 = new Paint();
mPaint1.setColor(Color.GRAY);
mPaint1.setAntiAlias(true);
mPaint1.setStrokeWidth(3);
//loge的笔
mbitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.heima);
mPaint2 = new Paint();
//指针的笔
mPaint3 = new Paint();
mPaint3.setColor(Color.WHITE);
mPaint3.setStrokeWidth(5);
mPaint3.setAntiAlias(true);
mPaint4 = new Paint();
mPaint4.setColor(Color.WHITE);
mPaint4.setStrokeWidth(4);
mPaint4.setAntiAlias(true);
mPaint5 = new Paint();
mPaint5.setColor(Color.WHITE);
mPaint5.setStrokeWidth(3);
mPaint5.setAntiAlias(true);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
//原点
mCx = w/2;
mCy = h/2;
//环半径
mRadius = w/2-CIRCLESIZE;
initBackgroundPaint();
}
private void initBackgroundPaint() {
mBackgroundPaint = new Paint();
mBackgroundPaint.setColor(Color.LTGRAY);
int[] colors = {Color.BLACK, Color.LTGRAY, Color.WHITE, Color.LTGRAY};
// float[] positions = {0.0f, 0.5f, 0.75f, 1};
Shader shader = new RadialGradient(mCx, mCy, mRadius, colors, null, Shader.TileMode.CLAMP);
mBackgroundPaint.setShader(shader);
}
@Override
//开始绘制
protected void onDraw(Canvas canvas) {
// 6. 画背景
canvas.drawCircle(mCx, mCy, mRadius, mBackgroundPaint);
//1.画环
canvas.drawCircle(mCx,mCy,mRadius,mPaint);
//2.画刻度
/* int startX=mCx;
int startY=5;
int stopX=mCx;
int stopY=20;
canvas.rotate(30,mCx,mCy);
canvas.drawLine(startX,startY,stopX,stopY,mPaint1);*/
drawDigits(canvas);
//3画数字
drawText(canvas);
//画loge
drawLoge(canvas);
//画指针
drawArrows(canvas);
//指针动起来 调用onAttachedToWindow 当view绑定到window的时候调用
}
private void drawArrows(Canvas canvas) {
/* mpath=new Path();
mpath.moveTo(mCx,mCy+DIGITSLONG);
mpath.lineTo(mCx,mCy-DIGITSLONG);*/
//canvas.drawPath(mpath,mPaint3);
//进行指针动画
canvas.rotate(mHourDegree,mCx,mCy);
canvas.drawLine(mCx,mCy+DIGISTSHART,mCx,CIRCLESIZE*3+DIGITSLONG+mTextsize,mPaint3);
canvas.rotate(-mHourDegree,mCx,mCy);
canvas.rotate(mMinuteDegree,mCx,mCy);
canvas.drawLine(mCx,mCy+DIGISTSHART,mCx,CIRCLESIZE+DIGITSLONG+mTextsize,mPaint4);
canvas.rotate(-mMinuteDegree,mCx,mCy);
canvas.rotate(mSecondDegree,mCx,mCy);
canvas.drawLine(mCx,mCy+DIGISTSHART,mCx,DIGISTSHART+mTextsize,mPaint5);
canvas.rotate(-mSecondDegree,mCx,mCy);
}
private void drawLoge(Canvas canvas) {
canvas.drawBitmap(mbitmap,mCx-mTextsize/2,CIRCLESIZE+DIGITSLONG+mTextsize,mPaint2);
}
private void drawText(Canvas canvas) {
for (int i=1;i<=12;i++){
canvas.rotate(30,mCx,mCy);
//调整位置
canvas.rotate(-30*i,mCx,CIRCLESIZE+DIGITSLONG+mTextsize/2);
canvas.drawText(i+"",mCx-CIRCLESIZE,CIRCLESIZE*2+DIGITSLONG+mTextsize/2,mPaint1);
canvas.rotate(30*i,mCx,CIRCLESIZE+DIGITSLONG+mTextsize/2);
}
}
private void drawDigits(Canvas canvas) {
for (int i=0 ;i<60;i++) {
int startX = mCx;
int startY = CIRCLESIZE;
int stopX = mCx;
int stopY;
if(i%5==0){
stopY = DIGITSLONG;
}else{
stopY=DIGISTSHART;
}
canvas.drawLine(startX, startY, stopX, stopY, mPaint1);
canvas.rotate(6, mCx, mCy);
}
}
@Override
//当view绑定到window的时候调用
protected void onAttachedToWindow() {
super.onAttachedToWindow();
//开启指针动画
startTick();
}
private void startTick() {
//1秒执行一次
postDelayed(runnable,1000);
}
private Runnable runnable=new Runnable() {
@Override
public void run() {
//计算指针便宜角度
calculateDegree();
//重新绘制
invalidate();
//递归
startTick();
}
};
private void calculateDegree() {
//获取当前时间
Calendar instance = Calendar.getInstance();
instance.setTimeInMillis(System.currentTimeMillis());
int hour = instance.get(Calendar.HOUR);
int minute = instance.get(Calendar.MINUTE);
int second = instance.get(Calendar.SECOND);
//计算角度
mHourDegree = hour * 30;
mMinuteDegree = minute * 6;
mSecondDegree = second * 6;
}
}
//布局中的代码
android:layout_centerInParent="true"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#ffffff"/>