复习自定义view 跑马灯实现

package com.snow.horseled;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.Calendar;

/**

  • Created by Schnee on 2017/2/3.
    */

public class RunHorseLed extends View {

Paint circlePaint;
Paint numPaint;
Paint secPaint, minPaint, hourPaint;

int roundTimes;

boolean isShiyingbiao = false;//石英机械表切换 哈哈哈

public RunHorseLed(Context context) {
    super(context);
}

public RunHorseLed(Context context, AttributeSet attrs) {
    super(context, attrs);
    roundTimes = isShiyingbiao ? 60 : 720;
    initPaint();

    setClockTime();
}

int times, min, hour;

private void setClockTime() {
    Calendar calendar = Calendar.getInstance();

    times = calendar.get(Calendar.SECOND) * roundTimes / 60;
    min = calendar.get(Calendar.MINUTE);
    hour = calendar.get(Calendar.HOUR);
    Log.e("kk", hour + "");
}

private void initPaint() {
    circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    circlePaint.setColor(Color.GREEN);
    circlePaint.setStrokeWidth(10);
    circlePaint.setStyle(Paint.Style.STROKE);

    numPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    numPaint.setColor(Color.BLACK);
    numPaint.setTextSize(30);

    secPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    secPaint.setStrokeWidth(5);

// secPaint

    minPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    minPaint.setStrokeWidth(8);

    hourPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    hourPaint.setStrokeWidth(12);

    handler.sendEmptyMessage(0);

}

int sRadius = 20, xRadius;
float gap;

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawCircle(width / 2, height / 2, sRadius, circlePaint);
    xRadius = width / 3;
    canvas.drawCircle(width / 2, height / 2, width / 3, circlePaint);

    canvas.save();
    for (int i = 1; i <= 12; i++) {
        canvas.rotate(30, width / 2, height / 2);
        gap = (float) (i < 10 ? 7 : 15);
        canvas.drawText(i + "", width / 2 - gap, height / 2 - width / 3 + 40, numPaint);
    }
    canvas.restore();

// double
canvas.drawLine(width / 2, height / 2,
width / 2 + (float) ((xRadius - 50) * Math.sin(times * 2 * Math.PI / roundTimes)),//x
height / 2 - (float) ((xRadius - 50) * Math.cos(times * 2 * Math.PI / roundTimes)), secPaint);
times++;

    canvas.drawLine(width / 2, height / 2,
            width / 2 + (float) ((xRadius - 80) * Math.sin(min * Math.PI / 30)),//x
            height / 2 - (float) ((xRadius - 80) * Math.cos(min * Math.PI / 30)),
            minPaint);

    canvas.drawLine(width / 2, height / 2,
            width / 2 + (float) ((xRadius - 120) * Math.sin(hour * Math.PI / 6)),//x
            height / 2 - (float) ((xRadius - 120) * Math.cos(hour * Math.PI / 6)),
            hourPaint);

    if (times == roundTimes) {
        times = 1;
        if (min == 60) {
            if (hour == 12) {
                hour = 1;
            } else
                hour++;
            min = 1;
        } else
            min++;
    }
}

Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        invalidate();
        sendEmptyMessageDelayed(0, 60 * 1000 / roundTimes);
    }
};

int width, height;

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
    height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);

// setMeasuredDimension(width, height);
}
}

你可能感兴趣的:(复习自定义view 跑马灯实现)