启动页广告倒计时View 发表于 2016-10-27 | 分类于 自定义view | 上一篇我们了解了自定义view用户圆弧实现计步view,今天我们继续巩固,实现App的启动页广告倒计view1. 显示倒计时的圆弧(效果图中的红色圆弧)2. 按钮圆3. 倒计时数字4. 设置倒计时5. 倒计时圆弧动画 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160/** * Created by zhangfeng on 2016/10/27. * 广告倒计时 view */public class AVDCountZeroView extends View { private Paint paint; private float paintWidth = 5f; private float centerX; private float outRadius = dipToPx(20); private float insideRadius = dipToPx(19); private float textSize = dipToPx(15); private int countZeroNumber; private String describeStr = "跳过"; private float currentCountZeroArc = 360; public AVDCountZeroView(Context context) { super(context); init(); } public AVDCountZeroView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public AVDCountZeroView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { paint = new Paint(); paint.setAntiAlias(true); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); centerX = getWidth() / 2; drawCountZeroCircle(canvas); drawShowContent(canvas); drawCountZeroText(canvas); drawCountZeroNumber(canvas); } /** * 绘制倒计时圆弧 * * @param canvas */ private void drawCountZeroCircle(Canvas canvas) { paint.setStrokeWidth(paintWidth); paint.setColor(Color.RED); paint.setStyle(Paint.Style.STROKE); RectF rectF = new RectF(centerX - outRadius, centerX - outRadius, centerX + outRadius, centerX + outRadius); canvas.drawArc(rectF, 0, currentCountZeroArc, false, paint); } /** * 绘制显示内容圆 * * @param canvas */ private void drawShowContent(Canvas canvas) { paint.setStrokeWidth(paintWidth); paint.setColor(Color.LTGRAY); paint.setStyle(Paint.Style.FILL); canvas.drawCircle(centerX, centerX, insideRadius, paint); } /** * 绘制描述文字 * * @param canvas */ private void drawCountZeroText(Canvas canvas) { paint.setTextSize(textSize); paint.setColor(Color.RED); paint.setStyle(Paint.Style.FILL); Rect rect = new Rect(); paint.getTextBounds(describeStr + "", 0, String.valueOf(describeStr).length(), rect); canvas.drawText(describeStr + "", centerX - rect.width() / 2, centerX + rect.height() / 2, paint); } /** * 绘制倒计时数字 * * @param canvas */ private void drawCountZeroNumber(Canvas canvas) { paint.setTextSize(textSize); paint.setColor(Color.GRAY); paint.setStyle(Paint.Style.FILL); Rect rect = new Rect(); String tempStr = String.valueOf(countZeroNumber) + "秒"; paint.getTextBounds(tempStr, 0, tempStr.length(), rect); canvas.drawText(tempStr, centerX - rect.width() / 2, centerX + rect.height() / 2 + dipToPx(35), paint); } /** * dp 转换成px * * @param dip * @return */ private int dipToPx(float dip) { float density = getContext().getResources().getDisplayMetrics().density; return (int) (dip * density + 0.5f * (dip >= 0 ? 1 : -1)); } /** * 数字倒计时 * * @param number */ public void setCountZeroNumber(final int number) { new Thread(new Runnable() { @Override public void run() { for (int i = number; i >= 0; i--) { countZeroNumber = i; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); setAinmation(360, 0, number * 1000); } /** * 倒计时外圆动画 * * @param last * @param current * @param length */ private void setAinmation(float last, float current, int length) { ValueAnimator valueAnimator = ValueAnimator.ofFloat(last, current); valueAnimator.setDuration(length); valueAnimator.setTarget(currentCountZeroArc); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { currentCountZeroArc = (float) animation.getAnimatedValue(); postInvalidate(); } }); valueAnimator.start(); }}