android 跑馬燈 文字一行能顯示全也能跑馬燈
阿新 • • 發佈:2019-01-09
android 裡面跑馬燈效果一般都是當行顯示不全的情況下才會有跑馬燈效果,當一行能顯示全的情況下,跑馬燈是沒有效果的。前幾天ui要一效果,希望一行能顯示全的情況下也能出現跑馬燈效果,這樣更醒目。
public class RunTextView extends TextView implements OnClickListener { public final static String TAG = CustomTextView.class.getSimpleName(); private float textLength = 0f;// 文字長度 private float viewWidth = 0f; private float step = 0f;// 文字的橫座標 private final float SPEED = 1.5f; // 跑馬燈速度 值越大,越快 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 Context context; public RunTextView (Context context) { super(context); this.context = context; initView(); } public RunTextView (Context context, AttributeSet attrs) { super(context, attrs); this.context = context; initView(); } public RunTextView (Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; initView(); } /** * 初始化控制元件 */ private void initView() { setOnClickListener(this); } /** * 文字初始化,每次更改文字內容或者文字效果等之後都需要重新初始化一下 */ public void init(WindowManager windowManager) { paint = getPaint(); paint.setColor(context.getResources().getColor(R.color.notice_text_color)); //設定文字顏色 text = getText().toString(); textLength = paint.measureText(text); viewWidth = getWidth(); if (viewWidth == 0) { if (windowManager != null) { 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; } public static class SavedState extends BaseSavedState { public boolean isStarting = false; public 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 Parcelable.Creator<SavedState> CREATOR = new Parcelable.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(); } @Override public void onDraw(Canvas canvas) { if (text != null) { canvas.drawText(text, temp_view_plus_text_length - step, y, paint); } if (!isStarting) { return; } step += SPEED; if (step > temp_view_plus_two_text_length){ step = textLength; invalidate(); } @Override public void onClick(View v) { if (isStarting){ startScroll(); } } } } stopScroll(); }else{ } 呼叫方式: RunTextView noticeText; ..... // findViewById noticeText.setText(result.getNotice()); //每次重新設定文字都需要重新呼叫一下下面的這些程式碼 noticeText.init(getActivity().getWindowManager()); noticeText.startScroll(); noticeText.setEnabled(false);