[转载]TextView跑马灯的两种实现

 (2012-07-02 14:21:57)
标签: 

转载

 
普通的TextView可以实现跑马灯,但是只有当焦点在它上面时才有效。 如何做一个自动的跑马灯呢? 第一种:继承TextView,然后重写isFocused()方法就可以了,简单!
Java代码  
  1. import android.content.Context;  
  2. import android.util.AttributeSet;  
  3. import android.widget.TextView;  
  4.   
  5. public class ScrollForeverTextView extends TextView {  
  6.   
  7.     public ScrollForeverTextView(Context context) {  
  8.         super(context);  
  9.         // TODO Auto-generated constructor stub  
  10.     }  
  11.   
  12.     public ScrollForeverTextView(Context context, AttributeSet attrs) {  
  13.         super(context, attrs);  
  14.     }  
  15.   
  16.     public ScrollForeverTextView(Context context, AttributeSet attrs,  
  17.             int defStyle) {  
  18.         super(context, attrs, defStyle);  
  19.     }  
  20.   
  21.     @Override  
  22.     public boolean isFocused() {  
  23.         return true;  
  24.     }  
  25.   
  26. }  
使用时同TextView一样:
Xml代码  
  1. <com.ql.view.ScrollForeverTextView  
  2.         android:layout_width="fill_parent"   
  3.         android:layout_height="wrap_content"   
  4.         android:textSize="30px"  
  5.         android:singleLine="true"  
  6.         android:ellipsize="marquee"  
  7.         android:marqueeRepeatLimit="marquee_forever"  
  8.         android:textColor="@color/red"   
  9.         android:text="1234567890wwwwwwwwwwwwwwwwwwwwww1234567890"  
  10.         android:focusable="true"  
  11.     />  
第2种:还是继承TextView,重写onDraw(),在onDraw中不停的重绘。
Java代码  
  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 AutoScrollTextView extends TextView implements OnClickListener {  
  15.     public final static String TAG = AutoScrollTextView.class.getSimpleName();  
  16.   
  17.     private float textLength = 0f;// 文本长度  
  18.     private float viewWidth = 0f;  
  19.     private float step = 0f;// 文字的横坐标  
  20.     private float y = 0f;// 文字的纵坐标  
  21.     private float temp_view_plus_text_length = 0.0f;// 用于计算的临时变量  
  22.     private float temp_view_plus_two_text_length = 0.0f;// 用于计算的临时变量  
  23.     public boolean isStarting = false;// 是否开始滚动  
  24.     private Paint paint = null;// 绘图样式  
  25.     private CharSequence text = "";// 文本内容  
  26.     private float speed = 0.5f;  
  27.     private int textColor=0xFF000000;  
  28.       
  29.     public int getTextColor() {  
  30.         return textColor;  
  31.     }  
  32.   
  33.     public void setTextColor(int color) {  
  34.         this.textColor = color;  
  35.     }  
  36.   
  37.     public float getSpeed() {  
  38.         return speed;  
  39.     }  
  40.   
  41.     public void setSpeed(float speed) {  
  42.         this.speed = speed;  
  43.     }  
  44.   
  45.     public AutoScrollTextView(Context context) {  
  46.         super(context);  
  47.         initView();  
  48.     }  
  49.   
  50.     public AutoScrollTextView(Context context, AttributeSet attrs) {  
  51.         super(context, attrs);  
  52.         initView();  
  53.     }  
  54.   
  55.     public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {  
  56.         super(context, attrs, defStyle);  
  57.         initView();  
  58.     }  
  59.   
  60.       
  61.       
  62.     private void initView() {  
  63.         setOnClickListener(this);  
  64.     }  
  65.   
  66.       
  67.       
  68.     public void init(float width) {  
  69.         text=super.getText();  
  70.         paint = super.getPaint();  
  71. //      Paint paint=new Paint();  
  72.         text = getText().toString();  
  73.         textLength = paint.measureText(text.toString());  
  74. //      viewWidth = getWidth();  
  75. //      if (viewWidth == 0) {  
  76. //          if (windowManager != null) {  
  77. //              Display display = windowManager.getDefaultDisplay();  
  78. //              viewWidth = display.getWidth();  
  79. //          }  
  80. //      }  
  81.         viewWidth=width;  
  82.         step = textLength;  
  83.         temp_view_plus_text_length = viewWidth + textLength;  
  84.         temp_view_plus_two_text_length = viewWidth + textLength * 2;  
  85.         y = getTextSize() + getPaddingTop();  
  86.         paint.setColor(textColor);  
  87.     }  
  88.   
  89.     @Override  
  90.     public Parcelable onSaveInstanceState() {  
  91.         Parcelable superState = super.onSaveInstanceState();  
  92.         SavedState ss = new SavedState(superState);  
  93.   
  94.         ss.step = step;  
  95.         ss.isStarting = isStarting;  
  96.   
  97.         return ss;  
  98.   
  99.     }  
  100.   
  101.     @Override  
  102.     public void onRestoreInstanceState(Parcelable state) {  
  103.         if (!(state instanceof SavedState)) {  
  104.             super.onRestoreInstanceState(state);  
  105.             return;  
  106.         }  
  107.         SavedState ss = (SavedState) state;  
  108.         super.onRestoreInstanceState(ss.getSuperState());  
  109.   
  110.         step = ss.step;  
  111.         isStarting = ss.isStarting;  
  112.   
  113.     }  
  114.   
  115.     public static class SavedState extends BaseSavedState {  
  116.         public boolean isStarting = false;  
  117.         public float step = 0.0f;  
  118.   
  119.         SavedState(Parcelable superState) {  
  120.             super(superState);  
  121.         }  
  122.   
  123.         @Override  
  124.         public void writeToParcel(Parcel out, int flags) {  
  125.             super.writeToParcel(out, flags);  
  126.             out.writeBooleanArray(new boolean[] { isStarting });  
  127.             out.writeFloat(step);  
  128.         }  
  129.   
  130.         public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {  
  131.   
  132.             public SavedState[] newArray(int size) {  
  133.                 return new SavedState[size];  
  134.             }  
  135.   
  136.             @Override  
  137.             public SavedState createFromParcel(Parcel in) {  
  138.                 return new SavedState(in);  
  139.             }  
  140.         };  
  141.   
  142.         private SavedState(Parcel in) {  
  143.             super(in);  
  144.             boolean[] b = null;  
  145.             in.readBooleanArray(b);  
  146.             if (b != null && b.length > 0)  
  147.                 isStarting = b[0];  
  148.             step = in.readFloat();  
  149.         }  
  150.     }  
  151.   
  152.       
  153.       
  154.     public void startScroll() {  
  155.         isStarting = true;  
  156.         invalidate();  
  157.     }  
  158.   
  159.       
  160.       
  161.     public void stopScroll() {  
  162.         isStarting = false;  
  163.         invalidate();  
  164.     }  
  165.   
  166.     @Override  
  167.     public void onDraw(Canvas canvas) {  
  168. //      super.onDraw(canvas);  
  169.           
  170.         canvas.drawText(text,0,text.length(), temp_view_plus_text_length - step, y, paint);  
  171.         if (!isStarting) {  
  172.             return;  
  173.         }  
  174.         step += speed;  
  175.         if (step > temp_view_plus_two_text_length)  
  176.             step = textLength;  
  177.         invalidate();  
  178.   
  179.     }  
  180.   
  181.     @Override  
  182.     public void onClick(View v) {  
  183.         if (isStarting)  
  184.             stopScroll();  
  185.         else  
  186.             startScroll();  
  187.   
  188.     }  
  189.   
  190. }  
使用:
Java代码  
  1. marquee = (AutoScrollTextView) findViewById(R.id.marquee);  
  2. //      marquee.setText(String.format(getResources().getString(R.string.marquee0),Consts.termno,"2010-12-28"));  
  3.         marquee.setText("上证指数3000.15 6.81(0.37%)深圳成指3000.15 6.81(0.37%)");  
  4. //      marquee.setTextColor(0xffff0000);//注意:颜色必须在这里设置,xml中设置无效!默认黑色。  
  5.         //如果想改变跑马灯的文字内容或者文字效果,则在调用完setText方法之后,需要再调用一下init(width)方法,重新进行初始化和相关参数的计算。  
  6.         marquee.setSpeed(1.5f);  
  7.         marquee.init(width);//width通常就是屏幕宽!  
  8.         marquee.startScroll();  
这2种跑马灯稍微有点区别,需要掂量着使用。 第1种跑马灯不能设置速度,并且要超过一行才会滚动。 第2种跑马灯只能设置单一颜色,且需要在代码中设置,不能同时设置多种颜色。速度设的不要过大(<= 2.0f),否则停顿现象比较明显。
posted on 2012-12-25 11:44  zhengbeibei  阅读(424)  评论(0编辑  收藏  举报