0%

自动滚播TextView

要实现一个自动滚播的自定义View

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
155
156
public class AutoSwitchView extends AppCompatTextView {

private final int DEFAULT_IDLE_TIME = 3000;
private final int DEFAULT_SWITCH_TIME = 1000;

private int mIdleTime = DEFAULT_IDLE_TIME;
private int mSwitchTime = DEFAULT_SWITCH_TIME;
private Paint mPaint;
private String mCurStr;
private String mNextStr;
private List<String> mContentList;
private int mCurIndex = 0;
private float mCurValue;
private boolean mIsRunning = false;
private ValueAnimator mAnimator;
private Paint.FontMetrics mFontMetrics;
private Runnable mRunnable;

private int mWidth;
private int mPaddingLeft;
private int mPaddingRight;
private int mPaddingTop;
private int mPaddingBottom;
private int mHeight;
private float mTextBaseY;


public AutoSwitchView(Context context) {
super(context);
init(context);
}

public AutoSwitchView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}

public AutoSwitchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}

private void init(Context context) {
mPaint = new Paint();
mPaint.setTextAlign(Paint.Align.CENTER);

mAnimator = ValueAnimator.ofFloat(0, 1);
mAnimator.setStartDelay(mIdleTime);
mAnimator.setDuration(mSwitchTime);
mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mCurValue = (float) animation.getAnimatedValue();
// 必须要加这个判断, 不然会出现问题
if (mCurValue < 1.0) {
invalidate();
}
}
});
mAnimator.addListener(new AnimatorListenerAdapter() {

@Override
public void onAnimationEnd(Animator animation, boolean isReverse) {
if (!mIsRunning) {
return;
}
mCurIndex = (mCurIndex + 1) % mContentList.size();
mCurStr = mContentList.get(mCurIndex);
mNextStr = mContentList.get((mCurIndex + 1) % mContentList.size());
if (mRunnable == null) {
mRunnable = new Runnable() {
@Override
public void run() {
mAnimator.start();
}
};
}
postDelayed(mRunnable, mIdleTime);
}

@Override
public void onAnimationCancel(Animator animation) {
mIsRunning = false;
}
});
}

public void start() {
if (mContentList == null || mContentList.size() == 0)
return;
if (mIsRunning) {
return;
}
if (mContentList.size() > 1) {
mCurStr = mContentList.get(0);
mNextStr = mContentList.get(1);
} else {
mCurStr = mContentList.get(0);
mNextStr = mContentList.get(0);
}
mCurIndex = 0;
mIsRunning = true;
mAnimator.start();
mAnimator.setStartDelay(mIdleTime);
}

public void stop() {
if (mRunnable != null) {
removeCallbacks(mRunnable);
}
mAnimator.cancel();
mIsRunning = false;
mCurIndex = 0;
}

public void setContentList(List<String> contentList) {
if (contentList == null || contentList.size() == 0)
return;
mContentList = contentList;
start();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

mWidth = MeasureSpec.getSize(widthMeasureSpec);
mPaddingLeft = getPaddingLeft();
mPaddingRight = getPaddingRight();
mPaddingTop = getPaddingTop();
mPaddingBottom = getPaddingBottom();

mPaint.setTextSize(getTextSize());
mFontMetrics = mPaint.getFontMetrics();

mTextBaseY = -mFontMetrics.top + mPaddingTop;
mHeight = Math.round(mFontMetrics.bottom - mFontMetrics.top) + mPaddingTop + mPaddingBottom;

setMeasuredDimension(mWidth, mHeight);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

float curStartX = mPaddingLeft + mPaint.measureText(mCurStr) / 2;
float nextStartX = mPaddingLeft + mPaint.measureText(mNextStr) / 2;

float baseY = 2 * mTextBaseY * (0.5f - mCurValue);
if (baseY > 0) {
canvas.drawText(mCurStr, curStartX, baseY, mPaint);
} else {
canvas.drawText(mNextStr, nextStartX, 2 * mTextBaseY + baseY, mPaint);
}
}
}