VerticalTextview.java
5.05 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright (c) 2017. wugian
* 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.xgimi.gimicinema.view;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import java.util.ArrayList;
/**
* User: xiehehe
* Date: 2016-07-19
* Time: 22:45
* FIXME
*/
public class VerticalTextview extends TextSwitcher implements ViewSwitcher.ViewFactory {
private static final int FLAG_START_AUTO_SCROLL = 0;
private static final int FLAG_STOP_AUTO_SCROLL = 1;
private float mTextSize = 16 ;
private int mPadding = 5;
private int textColor = Color.BLACK;
/**
* @param textSize 字号
* @param padding 内边距
* @param textColor 字体颜色
*/
public void setText(float textSize,int padding,int textColor) {
mTextSize = textSize;
mPadding = padding;
this.textColor = textColor;
}
private OnItemClickListener itemClickListener;
private Context mContext;
private int currentId = -1;
private ArrayList<String> textList;
private Handler handler;
public VerticalTextview(Context context) {
this(context, null);
mContext = context;
}
public VerticalTextview(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
textList = new ArrayList<String>();
}
public void setAnimTime(long animDuration) {
setFactory(this);
Animation in = new TranslateAnimation(0, 0, animDuration, 0);
in.setDuration(animDuration);
in.setInterpolator(new AccelerateInterpolator());
Animation out = new TranslateAnimation(0, 0, 0, -animDuration);
out.setDuration(animDuration);
out.setInterpolator(new AccelerateInterpolator());
setInAnimation(in);
setOutAnimation(out);
}
/**
* 间隔时间
* @param time
*/
public void setTextStillTime(final long time){
handler =new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case FLAG_START_AUTO_SCROLL:
if (textList.size() > 0) {
currentId++;
setText(textList.get(currentId % textList.size()));
}
handler.sendEmptyMessageDelayed(FLAG_START_AUTO_SCROLL,time);
break;
case FLAG_STOP_AUTO_SCROLL:
handler.removeMessages(FLAG_START_AUTO_SCROLL);
break;
}
}
};
}
/**
* 设置数据源
* @param titles
*/
public void setTextList(ArrayList<String> titles) {
textList.clear();
textList.addAll(titles);
currentId = -1;
}
/**
* 开始滚动
*/
public void startAutoScroll() {
handler.sendEmptyMessage(FLAG_START_AUTO_SCROLL);
}
/**
* 停止滚动
*/
public void stopAutoScroll() {
handler.sendEmptyMessage(FLAG_STOP_AUTO_SCROLL);
}
@Override
public View makeView() {
TextView t = new TextView(mContext);
t.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
t.setMaxLines(1);
t.setPadding(mPadding, mPadding, mPadding, mPadding);
t.setTextColor(textColor);
t.setTextSize(mTextSize);
t.setClickable(true);
t.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (itemClickListener != null && textList.size() > 0 && currentId != -1) {
itemClickListener.onItemClick(currentId % textList.size());
}
}
});
return t;
}
/**
* 设置点击事件监听
* @param itemClickListener
*/
public void setOnItemClickListener(OnItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
/**
* 轮播文本点击监听器
*/
public interface OnItemClickListener {
/**
* 点击回调
* @param position 当前点击ID
*/
void onItemClick(int position);
}
}