forked from ShwoTimeNow/Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimatorView.java
More file actions
96 lines (76 loc) · 2.42 KB
/
Copy pathAnimatorView.java
File metadata and controls
96 lines (76 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* COPYRIGHT NOTICE
* Copyright (C) 2014, ticktick <lujun.hust@gmail.com>
* http://ticktick.blog.51cto.com/
*
* @license under the Apache License, Version 2.0
*
* @file AnimatorView.java
* @brief 实现了一个矩状图动画效果的自定义View
*
* @version 1.0
* @author ticktick
* @date 2014/08/27
*
*/
package com.ticktick.example.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
public class AnimatorView extends View {
private static final int RECT_WIDTH = 60; //每个矩形块的宽度
private static final int RECT_DISTANCE = 40; //矩形块之间的间距
private static final int TOTAL_PAINT_TIMES = 100; //控制绘制速度,分100次完成绘制
//待绘制的矩形块矩阵,left为高度,right为颜色
private static final int[][] RECT_ARRAY = {
{380,Color.GRAY},
{600,Color.YELLOW},
{200,Color.GREEN},
{450,Color.RED},
{300,Color.BLUE}
};
private Paint mPaint;
private int mPaintTimes = 0; //当前已经绘制的次数
private boolean mIsAnimaionRun = false;
public AnimatorView(Context context) {
super(context);
initialize();
}
public AnimatorView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public AnimatorView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
}
protected void initialize() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
if( !mIsAnimaionRun ) {
return;
}
mPaintTimes++;
for( int i=0; i<RECT_ARRAY.length; i++ ) {
mPaint.setColor(RECT_ARRAY[i][1]);
int paintXPos = i*(RECT_WIDTH+RECT_DISTANCE) + RECT_DISTANCE;
int paintYPos = RECT_ARRAY[i][0]/TOTAL_PAINT_TIMES*mPaintTimes;
canvas.drawRect(paintXPos, getHeight(), paintXPos+RECT_WIDTH,getHeight()-paintYPos, mPaint);
}
if( mPaintTimes < TOTAL_PAINT_TIMES ) {
invalidate();
}
}
public void startAnimation() {
mIsAnimaionRun = true;
invalidate();
}
}