package com.app.shui.datailnote.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* 轮询指定集合里面的内容
* <p/>
* Created by chenshuide on 2015/3/12.
*/
public class LooperTextView extends TextView {
private static final long TIMEDELAY = 200;
private List<String> list;
private Handler handler;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private int SPEED = 2;
private List<TextView> listText = new ArrayList<TextView>();
private boolean isInit;
private int TEXT_SPACE = 40;
private int TEXT_SIZE = 30;
private boolean isContinue;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
isContinue = false;
this.list = list;
}
public LooperTextView(Context context) {
super(context);
init(context);
}
public LooperTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(final Context context) {
paint.setTextAlign(Paint.Align.CENTER);
paint.setTextSize(TEXT_SIZE);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
invalidate();
}
};
}
@Override
protected void onDraw(Canvas canvas) {
if (!isInit)
initText();
if (!isContinue)
return;
/**
* 提前固定好在list中的位置 防止在绘制过程由于remove操作导致位置改变
* 引起闪屏
*/
for (int i = 0; i < listText.size(); i++) {
TextView t = listText.get(i);
if (t.height - SPEED < -10) {
listText.remove(t);
t.height = listText.get(listText.size() - 1).height + TEXT_SPACE;
listText.add(t);
}
}
for (int i = 0; i < listText.size(); i++) {
TextView t = listText.get(i);
canvas.drawText(t.text, t.width, t.height - SPEED, paint);
}
SPEED++;
handler.sendEmptyMessageDelayed(0, TIMEDELAY);
}
private void initText() {
if (list == null) {
isContinue = false;
return;
}
for (int i = 0; i < list.size(); i++) {
TextView t = new TextView();
t.text = list.get(i);
t.width = getWidth() / 2;
t.height = getHeight() / 2 + TEXT_SPACE * i;
listText.add(t);
}
isInit = true;
isContinue = true;
}
private static class TextView {
private int height;
private int width;
private String text;
}
}
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* 轮询指定集合里面的内容
* <p/>
* Created by chenshuide on 2015/3/12.
*/
public class LooperTextView extends TextView {
private static final long TIMEDELAY = 200;
private List<String> list;
private Handler handler;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private int SPEED = 2;
private List<TextView> listText = new ArrayList<TextView>();
private boolean isInit;
private int TEXT_SPACE = 40;
private int TEXT_SIZE = 30;
private boolean isContinue;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
isContinue = false;
this.list = list;
}
public LooperTextView(Context context) {
super(context);
init(context);
}
public LooperTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(final Context context) {
paint.setTextAlign(Paint.Align.CENTER);
paint.setTextSize(TEXT_SIZE);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
invalidate();
}
};
}
@Override
protected void onDraw(Canvas canvas) {
if (!isInit)
initText();
if (!isContinue)
return;
/**
* 提前固定好在list中的位置 防止在绘制过程由于remove操作导致位置改变
* 引起闪屏
*/
for (int i = 0; i < listText.size(); i++) {
TextView t = listText.get(i);
if (t.height - SPEED < -10) {
listText.remove(t);
t.height = listText.get(listText.size() - 1).height + TEXT_SPACE;
listText.add(t);
}
}
for (int i = 0; i < listText.size(); i++) {
TextView t = listText.get(i);
canvas.drawText(t.text, t.width, t.height - SPEED, paint);
}
SPEED++;
handler.sendEmptyMessageDelayed(0, TIMEDELAY);
}
private void initText() {
if (list == null) {
isContinue = false;
return;
}
for (int i = 0; i < list.size(); i++) {
TextView t = new TextView();
t.text = list.get(i);
t.width = getWidth() / 2;
t.height = getHeight() / 2 + TEXT_SPACE * i;
listText.add(t);
}
isInit = true;
isContinue = true;
}
private static class TextView {
private int height;
private int width;
private String text;
}
}