HorizonScrollTextView介紹
消息滾動.gif
主要功能
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的使用就結束了,自測了使用效果,暫時還沒發現有什么問題存在。
其實主要代碼是出自一個大神之手,我主要還是做了修改而已,比如字體居中,滾動監聽等。大家也可以在這個基礎上繼續修改。
本人還是在學習階段,這個部分對于我自己來說還是不錯的。