a
554325746@qq.com
2019-07-15 e6c8834e2aa2f3c1f46e588d94ae8ec6aa6c8dec
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright 2017 linjiang.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.basic.security.utils;
 
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.graphics.PointF;
 
public class Unit implements Comparable<Unit>, Cloneable {
 
    public static long DURATION = 800;
    private ValueAnimator VALUEANIMATOR = ValueAnimator.ofFloat(0, 1);
 
    /**
     * 当前点的值
     */
    private float value;
    // 当前点的额外信息(可选,x轴)
    private String extX;
    /**
     * 当前点的坐标信息,都是相对的canvas而不是linesArea
     */
    private PointF xy;
 
    /**
     * 当前点的动画进度,
     * 默认为1表示无动画
     */
    private float percent = 1f;
 
    public Unit(float value) {
        this.value = value;
    }
 
    public boolean draw = false;
 
    public Unit(float value, String extX) {
        this.value = value;
        this.extX = extX;
        this.draw = true;
    }
 
    public Unit(float value, String extX, boolean draw) {
        this.value = value;
        this.extX = extX;
        this.draw = draw;
    }
 
    public float getValue() {
        return value;
    }
    public void setXY(PointF xy) {
        this.xy = xy;
    }
    public PointF getXY() {
        return xy;
    }
    public void setPercent(float percent) {
        this.percent = percent;
    }
 
    public float getPercent() {
        return percent;
    }
 
    public void setExtX(String extX) {
        this.extX = extX;
    }
 
    public String getExtX() {
        return extX;
    }
 
 
    public void cancelToEndAnim() {
        if (VALUEANIMATOR.isRunning()) {
            VALUEANIMATOR.cancel();
        }
        percent = 1f;
    }
 
    public void startAnim(TimeInterpolator value) {
        if (percent > 0 || VALUEANIMATOR.isRunning()) {
            return;
        }
        // 如果value小于一定阈值就不开启动画
        if (Math.abs((int)this.value) < 0.1) {
            percent = 1;
            return;
        }
        VALUEANIMATOR.setFloatValues(0, 1);
        VALUEANIMATOR.setDuration(DURATION);
        VALUEANIMATOR.setInterpolator(value);
        VALUEANIMATOR.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                percent = (float) animation.getAnimatedValue();
            }
        });
        VALUEANIMATOR.start();
    }
 
 
 
    @Override
    public int compareTo(Unit o) {
        if (value == o.value) {
            return 0;
        } else if (value > o.value) {
            return 1;
        } else {
            return -1;
        }
    }
 
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Unit)) return false;
        Unit unit = (Unit) obj;
        return value == unit.value
                && (extX == unit.extX) || (extX != null && extX.equals(unit.extX));
    }
 
    @Override
    public String toString() {
        return "Unit{" +
                "xy=" + xy +
                '}';
    }
 
    @Override
    public Unit clone() {// 转化为深度拷贝,防止在集合中排序时的引用问题
        try {
            return (Unit) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }
}