这个自定义view主要实现的是垂直文本自动滚动,当文本高度超出垂直高度时自动滚动。也可以修改成其他条件触发滚动。参考了网上一篇文章,找不到出处了。
package com.serviatech.mediaplayer.view;
import com.serviatech.mediaplayer.utils.Logcat;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ScrollView;
import android.widget.TextView;
/**
* 里面有一个textview,当textview文字高度小于1080时,设置textview为1080高度,如果大于就开始滚动
*/
public class AutoScrollView extends ScrollView {
private final Handler handler = new Handler();
private long duration = 50;
private boolean isScrolled = false;
private int currentIndex = 0;
private long period = 1000;
private int currentY = -1;
private float mTextSize;
private int mTextColor;
private Typeface mTypeface;
private String string;
private Context mContext;
public AutoScrollView(Context context) {
this(context, null);
init(context);
}
public AutoScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
init(context);
}
public AutoScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public void init(Context context) {
this.mContext = context;
mTextSize = 50;
mTextColor = Color.RED;
mTypeface = Typeface.DEFAULT;
}
// /
public void setTextSize(float textSize) {
this.mTextSize = textSize;
caculate();
}
public void setTextColor(int textColor) {
this.mTextColor = textColor;
}
public void setTypeface(Typeface typeface) {
this.mTypeface = typeface;
caculate();
}
public void setText(String string) {
this.string = string;
caculate();
}
public void setBackgroundColor(int color) {
setBackgroundColor(color);
}
// /
/**
* 计算是否需要滚动
*
* @data 2016-5-27 上午9:21:51
*/
public void caculate() {
stopScroll();
final TextView textView = new TextView(mContext);
textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
textView.setTextSize(mTextSize);
textView.setTextColor(mTextColor);
textView.setTypeface(mTypeface);
textView.setText(string);
textView.setGravity(Gravity.CENTER);
removeAllViews();
addView(textView);
new Thread() {
@Override
public void run() {
super.run();
try {
sleep(200);// 必须要休眠一段时间
Logcat.d("@@@",
"textView.getHeight(): " + textView.getHeight()
+ ",getHeight(): " + getHeight());
if (textView.getHeight() > getHeight()) {
autoScroll();
} else {
stopScroll();
}
} catch (Exception e) {
}
}
}.start();
}
private void stopScroll() {
isScrolled = false;
}
private void autoScroll() {
isScrolled = true;
handler.postDelayed(new Runnable() {
@Override
public void run() {
boolean flag = isScrolled;
if (flag) {
// Log.d("test", "currentY = " + currentY
// + " getScrollY() = " + getScrollY());
if (currentY == getScrollY()) {
try {
Thread.sleep(period);
} catch (InterruptedException e) {
e.printStackTrace();
}
currentIndex = 0;
scrollTo(0, 0);
handler.postDelayed(this, period);
} else {
currentY = getScrollY();
handler.postDelayed(this, duration);
currentIndex++;
scrollTo(0, currentIndex * 1);
}
} else {
// currentIndex = 0;
// scrollTo(0, 0);
}
}
}, duration);
}
//
}
使用
<com.serviatech.mediaplayer.view.AutoScrollView
android:id="@+id/tv_emergen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="#fff"
android:fillViewport="true"
android:gravity="center"
android:text="遇到紧急情况!!!"
android:textColor="#f00"
android:textSize="50sp"
android:visibility="gone" />