package cn.com.chenzheng_java;
import android.content.Context;
import android.graphics.Color;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.Scroller;
import android.widget.TextView;
/***
* 可滑动的文本
* @author 陈正
* @version 2.0
* @since 2011/12/16
*/
public class MyTextViewGroup extends ViewGroup implements View.OnClickListener {
private int totalCount = 0;// the total count of the data array
private int width_showArea = 0;// the width of visible area
private int width_perText = 0;// the width of per text,if text's length is too long,it will scroll
private int height_perText = 30;// the default height of per text
private int visible_number = 0;// the number of text within scan
private String sources[];//the array to store infos
private Context context;
int middle = 0;// the number of visible_number/2-1,it's useful for our compute
private int default_normal_textColor = Color.BLACK;// the default text color of unchecked
private int default_selected_textColor = Color.WHITE;//the default text color of checked
private int max_scroll_width = 0;//the max scroll width
private StringBuilder stringBuilder = new StringBuilder();
private MyTypeClickListener myTypeClickListener = null;// custom listener of click event
private Scroller scroller;
private int fontSize = 20;
private boolean isSelectChanged = true;
private int max_index = 0;//the max number of index
public MyTextViewGroup(Context context) {
super(context);
this.context = context;
this.setBackgroundColor(Color.RED);
}
/***
* constrcutor
* @param context
* @param attributeSet
*/
public MyTextViewGroup(Context context, AttributeSet attributeSet) {
super(context);
this.context = context;
}
/**
* 设置属性,并显示组件,任何对于组件的设置操作,应该都在setData之前被调用
* @param sources
* 文本数组
* @param width_showArea
* 展示区域的宽度
* @param visible_number
* 展示区域展示的个数
* @param height_perText
* 每一个文本的高度
*/
public void setData(String[] sources, int width_showArea,
int visible_number, int height_perText) {
this.sources = sources;
this.width_showArea = width_showArea;
this.visible_number = visible_number;
this.width_perText = width_showArea / visible_number;
this.height_perText = height_perText;
this.totalCount = this.sources.length;
max_index = totalCount-1;
init();
}
/***
* init something
*/
private void init() {
removeAllViewsInLayout();
scroller = new Scroller(context, new AccelerateDecelerateInterpolator());
for (int index = 0; index < totalCount; index++) {
makeAndAddView(index, null);
}
max_scroll_width = (totalCount - visible_number) * width_perText;
middle = visible_number/2;
}
private int temp__center_index = 0;//中心位置的那个元素的索引
private int temp_scroll_width = 0;//相对于中心位置来说,其滚动的距离,往左为负数
/***
* 将索引位置移动到视野的中心位置
* @param index
*/
public void showAtIndex(int index){
index = validateIndex(index);
switch (getScrollModel(index)) {
case ScrollMode.MODE_FIRST:
gotoScroll(index);
scrollFirst(index);
break;
case ScrollMode.MODE_END:
gotoScroll(index);
scrollEnd(index);
break;
default:
gotoScroll(index);
scrollNormal(index);
break;
}
changeColor(index);
invalidate();
}
/***
* next
*/
public void next(){
isSelectChanged = false;
showAtIndex(temp__center_index+1);
}
/**
* previous
*/
public void previous(){
isSelectChanged = false;
showAtIndex(temp__center_index-1);
}
/**
* 处理移动到首页时的操作
* @param index (index<middle)
*/
private void scrollFirst(int index){
temp__center_index = middle;
}
/**
* 处理移动到末页时的操作
* @param index (index>(max_index-middle))
*/
private void scrollEnd(int index){
temp__center_index = max_index-middle;
}
/**
*
* @param index
*/
private void scrollNormal(int index){
temp__center_index = index;
}
/***
* 根据index和当前默认位置,前后滑动
* @param index
*/
private void gotoScroll(int index){
// forward
if(index>temp__center_index){
next_step(index-temp__center_index);
}
else{
previous_step(temp__center_index-index);
}
}
// 处理越界的情况
private int validateIndex(int index){
if (index >= (totalCount - 1))
index = totalCount - 1;
if (index < 0)
index = 0;
return index;
}
/**
* forward with the step
* @param step
*/
private void next_step(int step){
if(!scroller.isFinished()){
scroller.abortAnimation();
}
if(max_scroll_width<=(getScrollX()+step*width_perText)){
//scrollTo(max_scroll_width, 0);
scroller.startScroll(getScrollX(), 0, max_scroll_width-getScrollX(), 0);
}else{
scroller.startScroll(getScrollX(), 0, width_perText*step, 0);
//scrollBy(width_perText*step, 0);
}
}
/***
* scroll back
* @param step
*/
private void previous_step(int step) {
if(!scroller.isFinished()){
scroller.abortAnimation();
}
if (getScrollX()-step*width_perText <= 0) {
scrollTo(0, 0);
} else {
scroller.startScroll(getScrollX(), 0, -step*width_perText, 0);
// scrollBy(-step*width_perText, 0);
}
}
@Override
public void computeScroll() {
if (scroller.computeScrollOffset()) {
//Log.e("computeScroll", "-滚动完毕-");
scrollTo(scroller.getCurrX(), 0);
postInvalidate();
}
// Log.e("computeScroll", "-滚动中-");
}
/***
* 改变指定索引的内容为高亮显示
*
* @param index
* start from 0
*/
private void changeColor(int index){
if(!isSelectChanged)
return ;
if (index < 0 || index > (totalCount - 1))
return;
TextView textView;
MyScrollText myScrollText;
for (int i = 0; i < getChildCount(); i++) {
if(i==index){
if (stringBuilder.toString().indexOf("-" + index + "-") == -1) {
textView = (TextView) getChildAt(index);
textView.setTextColor(default_selected_textColor);
} else {
myScrollText = (MyScrollText) getChildAt(index);
myScrollText.getPaint().setColor(default_selected_textColor);
}
}else{
if (stringBuilder.toString().indexOf("-" + i + "-") == -1) {
textView = (TextView) getChildAt(i);
textView.setTextColor(default_normal_textColor);
} else {
myScrollText = (MyScrollText) getChildAt(i);
myScrollText.getPaint().setColor(default_normal_textColor);
}
}
}
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
//Log.e("onScrollChanged", "--------");
super.onScrollChanged(l, t, oldl, oldt);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
int childLeft = 0;
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
final int childWidth = width_perText;
child.layout(childLeft, 0, childLeft + childWidth,
height_perText);
childLeft += childWidth;
}
}
}
/***
* 创建并且添加一个布局到viewFlow中
*
* @param position
* dapter中的索引
* @param addToEnd
* 是否添加到父容器的最后
* @param convertView
* 布局View
* @return
*/
private View makeAndAddView(int position, View convertView) {
View view = createMyScrollText(position);
vi
- 1
- 2
- 3
- 4
- 5
- 6
前往页