android上實現一個簡單的跑馬燈控件,通過點擊start or stop
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.os.Parcel;
- import android.os.Parcelable;
- import android.util.AttributeSet;
- import android.vIEw.Display;
- import android.view.VIEw;
- import android.vIEw.WindowManager;
- import android.view.VIEw.OnClickListener;
- import android.widget.TextVIEw;
- /**
- * 單行文本跑馬燈控件
- */
- public class MarqueeView extends TextVIEw implements OnClickListener {
- public final static String TAG = MarqueeVIEw.class.getSimpleName();
- private float textLength = 0f; // 文本長度
- private float vIEwWidth = 0f; //控件顯示文字區域寬度
- private float step = 0f; // 文字的橫坐標
- private float y = 0f; // 文字的縱坐標
- public boolean isStarting = true;// 是否開始滾動
- private Paint paint = null;// 繪圖樣式
- private String text = "";// 文本內容
-
- public MarqueeVIEw(Context context) {
- super(context);
- initVIEw();
- }
-
- public MarqueeVIEw(Context context, AttributeSet attrs) {
- super(context, attrs);
- initVIEw();
- }
-
- public MarqueeVIEw(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- initVIEw();
- }
-
- /**
- * 初始化控件
- */
- private void initVIEw() {
- setOnClickListener(this);
- }
-
- /**
- * 文本初始化,每次更改文本內容或者文本效果等之後都需要重新初始化一下
- */
- public void init(WindowManager windowManager) {
- paint = getPaint();
- text = getText().toString();
- textLength = paint.measureText(text);
- vIEwWidth = getWidth();
- if (vIEwWidth == 0) {
- if (windowManager != null) {
- Display display = windowManager.getDefaultDisplay();
- vIEwWidth = display.getWidth();
- }
- }
- step = 0;
- 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);
- }
- 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) {
- canvas.drawText(text, step, y, paint);
- if (!isStarting) {
- return;
- }
- step -= 0.8;
- if (step <= -textLength)
- step = vIEwWidth;
- invalidate();
- }
-
- @Override
- public void onClick(VIEw v) {
- if (isStarting)
- stopScroll();
- else
- startScroll();
- }
- }