Android自绘跑马灯控件

Android上实现一个简单的跑马灯控件,通过点击start or stop



 

  1. import android.content.Context;
     
  2. import android.graphics.Canvas;
     
  3. import android.graphics.Paint;
     
  4. import android.os.Parcel;
     
  5. import android.os.Parcelable;
     
  6. import android.util.AttributeSet;
     
  7. import android.view.Display;
     
  8. import android.view.View;
     
  9. import android.view.WindowManager;
     
  10. import android.view.View.OnClickListener;
     
  11. import android.widget.TextView;
     

  12.  

  13.  
  14. public class MarqueeView extends TextView implements OnClickListener {
     
  15.         public final static String TAG = MarqueeView.class.getSimpleName();
     
  16.         private float textLength = 0f;        // 文本长度
     
  17.         private float viewWidth = 0f;        //控件显示文字区域宽度
     
  18.         private float step = 0f;                // 文字的横坐标
     
  19.         private float y = 0f;                        // 文字的纵坐标
     
  20.         public boolean isStarting = true;// 是否开始滚动
     
  21.         private Paint paint = null;// 绘图样式
     
  22.         private String text = "";// 文本内容
     
  23.         
     
  24.         public MarqueeView(Context context) {
     
  25.         super(context);
     
  26.         initView();
     
  27.         }
     
  28.         
     
  29.         public MarqueeView(Context context, AttributeSet attrs) {
     
  30.         super(context, attrs);
     
  31.         initView();
     
  32.         }
     
  33.         
     
  34.         public MarqueeView(Context context, AttributeSet attrs, int defStyle) {
     
  35.         super(context, attrs, defStyle);
     
  36.         initView();
     
  37.         }
     
  38.         
     
  39.        
     
  40.         private void initView() {
     
  41.         setOnClickListener(this);
     
  42.         }
     
  43.         
     
  44.        
     
  45.         public void init(WindowManager windowManager) {
     
  46.         paint = getPaint();
     
  47.         text = getText().toString();
     
  48.         textLength = paint.measureText(text);
     
  49.         viewWidth = getWidth();
     
  50.         if (viewWidth == 0) {
     
  51.             if (windowManager != null) {
     
  52.                 Display display = windowManager.getDefaultDisplay();
     
  53.                 viewWidth = display.getWidth();
     
  54.             }
     
  55.         }
     
  56.         step = 0;
     
  57.         y = getTextSize() + getPaddingTop();
     
  58.         }
     
  59.         
     
  60.         @Override
     
  61.         public Parcelable onSaveInstanceState() {
     
  62.         Parcelable superState = super.onSaveInstanceState();
     
  63.         SavedState ss = new SavedState(superState);
     
  64.         ss.step = step;
     
  65.         ss.isStarting = isStarting;
     
  66.         return ss;
     
  67.         }
     
  68.         
     
  69.         @Override
     
  70.         public void onRestoreInstanceState(Parcelable state) {
     
  71.         if (!(state instanceof SavedState)) {
     
  72.             super.onRestoreInstanceState(state);
     
  73.             return;
     
  74.         }
     
  75.         SavedState ss = (SavedState) state;
     
  76.         super.onRestoreInstanceState(ss.getSuperState());
     
  77.         step = ss.step;
     
  78.         isStarting = ss.isStarting;
     
  79.         }
     
  80.         
     
  81.         public static class SavedState extends BaseSavedState {
     
  82.         public boolean isStarting = false;
     
  83.         public float step = 0.0f;
     

  84.  
  85.         SavedState(Parcelable superState) {
     
  86.             super(superState);
     
  87.         }
     

  88.  
  89.         public void writeToParcel(Parcel out, int flags) {
     
  90.             super.writeToParcel(out, flags);
     
  91.             out.writeBooleanArray(new boolean[] { isStarting });
     
  92.             out.writeFloat(step);
     
  93.         }
     

  94.  
  95.         public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
     

  96.  
  97.             public SavedState[] newArray(int size) {
     
  98.                 return new SavedState[size];
     
  99.             }
     

  100.  
  101.             @Override
     
  102.             public SavedState createFromParcel(Parcel in) {
     
  103.                 return new SavedState(in);
     
  104.             }
     
  105.         };
     

  106.  
  107.         private SavedState(Parcel in) {
     
  108.             super(in);
     
  109.             boolean[] b = null;
     
  110.             in.readBooleanArray(b);
     
  111.             if (b != null && b.length > 0)
     
  112.                     isStarting = b[0];
     
  113.             step = in.readFloat();
     
  114.         }
     
  115.         }
     
  116.         
     
  117.        
     
  118.         public void startScroll() {
     
  119.         isStarting = true;
     
  120.         invalidate();
     
  121.         }
     
  122.         
     
  123.        
     
  124.         public void stopScroll() {
     
  125.         isStarting = false;
     
  126.         invalidate();
     
  127.         }
     
  128.         
     
  129.         @Override
     
  130.         public void onDraw(Canvas canvas) {
     
  131.         canvas.drawText(text, step, y, paint);
     
  132.         if (!isStarting) {
     
  133.                 return;
     
  134.         }
     
  135.         step -= 0.8;
     
  136.         if (step <= -textLength)
     
  137.                 step = viewWidth;
     
  138.         invalidate();
     
  139.         }
     
  140.         
     
  141.         @Override
     
  142.         public void onClick(View v) {
     
  143.         if (isStarting)
     
  144.             stopScroll();
     
  145.         else
     
  146.             startScroll();
     
  147.         }
     
  148. }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值