Android跑马灯实现头尾呼应,复习自定义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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值