HorizonScrollTextView介绍
主要功能
1、文字居中滚动。
2、可自定义滚动几遍结束
HorizonScrollTextView相关代码
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.WindowManager;
public class HorizonScrollTextView extends AppCompatTextView {
private float textLength = 0f;// 文本长度
private float step = 0f;// 文字的横坐标
// private float y = 0f;// 文字的纵坐标
private float temp_view_plus_text_length = 0.0f;// 用于计算的临时变量
private float temp_view_plus_two_text_length = 0.0f;// 用于计算的临时变量
public boolean isStarting = false;// 是否开始滚动
private Paint paint = null;// 绘图样式
private String text = "";// 文本内容
private onTextScrollListener mListener;
private int mTimes = 1;//次数
private int mTimesCount;
public HorizonScrollTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
private void initView() {
}
public void init(WindowManager windowManager) {
int color = getTextColors().getColorForState(getDrawableState(), 0);
paint = getPaint();
//设置滚动字体颜色
paint.setColor(color);
text = getText().toString();
textLength = paint.measureText(text);
float viewWidth = getWidth();
if (viewWidth == 0) {
if (windowManager != null) {
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
viewWidth = metrics.widthPixels;
// Display display = windowManager.getDefaultDisplay();
// viewWidth = display.getWidth();
}
}
step = textLength;
temp_view_plus_text_length = viewWidth + textLength;
temp_view_plus_two_text_length = viewWidth + textLength * 2;
// y = getTextSize() + getPaddingTop();
}
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.step = step;
ss.isStarting = isStarting;
return ss;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
step = ss.step;
isStarting = ss.isStarting;
}
private static class SavedState extends BaseSavedState {
boolean isStarting = false;
float step = 0.0f;
SavedState(Parcelable superState) {
super(superState);
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeBooleanArray(new boolean[]{isStarting});
out.writeFloat(step);
}
public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
public SavedState[] newArray(int size) {
return new SavedState[size];
}
@Override
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
};
private SavedState(Parcel in) {
super(in);
boolean[] b = null;
in.readBooleanArray(b);
if (b != null && b.length > 0)
isStarting = b[0];
step = in.readFloat();
}
}
public void startScroll() {
isStarting = true;
invalidate();
}
public void stopScroll() {
isStarting = false;
invalidate();
}
public boolean getHasStarting() {
return isStarting;
}
@Override
public void onDraw(Canvas canvas) {
Paint.FontMetrics fontMetrics = paint.getFontMetrics();
float fontHeight = fontMetrics.bottom - fontMetrics.top;
float textBaseY = getHeight() - (getHeight() - fontHeight) / 2 - fontMetrics.bottom;
canvas.drawText(text, temp_view_plus_text_length - step, textBaseY, paint);
// canvas.drawText(text, temp_view_plus_text_length - step, y, paint);
if (!isStarting) {
return;
}
step += 1.5f;// 文字滚动速度。
if (step > temp_view_plus_two_text_length) {
step = textLength;
if (mTimesCount <= mTimes) {
mListener.onFinish();
}
mTimes++;
} else {
mListener.onReset(temp_view_plus_text_length - step);
}
invalidate();
}
public interface onTextScrollListener {
void onReset(float x);
void onFinish();
}
/**
* 监听滚动次数并监听滚动结束
* @param times 滚动次数
* @param listener 文字滚动监听
*/
public void setTextScrollListener(int times, onTextScrollListener listener) {
mTimesCount = times;
mListener = listener;
}
}
在xml中引用和textview没有差别
<com.zdy.tv.ui.next.view.HorizonScrollTextView
android:id="@+id/tv_message_title"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_marginTop="103dp"
android:background="@mipmap/ic_main_video_notice"
android:gravity="center"
android:textColor="#000000"
android:textSize="20dp"
android:visibility="gone"
tools:text="消息推送" />
activity中使用的主要代码为
mTxtNotice.init(getWindowManager());
mTxtNotice.startScroll();//mTxtNotice为HorizonScrollTextView实例
监听滚动部分:
mTxtNotice.setTextScrollListener(1, new HorizonScrollTextView.onTextScrollListener() {
@Override
public void onReset(float x) {
//滚动中
}
@Override
public void onFinish() {
//滚动结束
mTxtNotice.stopScroll();
}
});
总结
至此HorizonScrollTextView的使用就结束了,自测了使用效果,暂时还没发现有什么问题存在。
其实主要代码是出自一个大神之手,我主要还是做了修改而已,比如字体居中,滚动监听等。大家也可以在这个基础上继续修改。
本人还是在学习阶段,这个部分对于我自己来说还是不错的。