普通的TextView可以实现跑马灯,但是只有当焦点在它上面时才有效。 如何做一个自动的跑马灯呢?

  第一种:继承TextView,然后重写isFocused()方法就可以了,简单!

Java代码:

  1. import android.content.Context;
  2. import android.util.AttributeSet;
  3. import android.widget.TextView;

  4. /**
  5. * 单行文本跑马灯控件
  6. * @author admin
  7. *
  8. */

  9. public class ScrollForeverTextView extends TextView {

  10. public ScrollForeverTextView(Context context) {
  11. super(context);
  12. // TODO Auto-generated constructor stub
  13. }

  14. public ScrollForeverTextView(Context context, AttributeSet attrs) {
  15. super(context, attrs);
  16. }

  17. public ScrollForeverTextView(Context context, AttributeSet attrs,int defStyle) {
  18. super(context, attrs, defStyle);
  19. }

  20. @Override
  21. public boolean isFocused() {
  22. return true;
  23. }

  24. }
复制代码


使用时同TextView一样:

Java代码:

  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. * @author admin
  15. */

  16. public class AutoScrollTextView extends TextView implements OnClickListener {
  17. public final static String TAG = AutoScrollTextView.class.getSimpleName();

  18. private float textLength = 0f;// 文本长度
  19. private float viewWidth = 0f;
  20. private float step = 0f;// 文字的横坐标
  21. private float y = 0f;// 文字的纵坐标
  22. private float temp_view_plus_text_length = 0.0f;// 用于计算的临时变量
  23. private float temp_view_plus_two_text_length = 0.0f;// 用于计算的临时变量
  24. public boolean isStarting = false;// 是否开始滚动
  25. private Paint paint = null;// 绘图样式
  26. private CharSequence text = "";// 文本内容
  27. private float speed = 0.5f;
  28. private int textColor=0xFF000000;

  29. public int getTextColor() {
  30. return textColor;
  31. }

  32. public void setTextColor(int color) {
  33. this.textColor = color;
  34. }

  35. public float getSpeed() {
  36. return speed;
  37. }

  38. public void setSpeed(float speed) {
  39. this.speed = speed;
  40. }

  41. public AutoScrollTextView(Context context) {
  42. super(context);
  43. initView();
  44. }

  45. public AutoScrollTextView(Context context, AttributeSet attrs) {
  46. super(context, attrs);
  47. initView();
  48. }

  49. public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
  50. super(context, attrs, defStyle);
  51. initView();
  52. }

  53. /**
  54. * 初始化控件
  55. */

  56. private void initView() {
  57. setOnClickListener(this);
  58. }

  59. /**
  60. * 文本初始化,每次更改文本内容或者文本效果等之后都需要重新初始化一下!
  61. */

  62. public void init(float width/*WindowManager windowManager*/) {
  63. text=super.getText();
  64. paint = super.getPaint();
  65. // Paint paint=new Paint();
  66. text = getText().toString();
  67. textLength = paint.measureText(text.toString());
  68. // viewWidth = getWidth();
  69. // if (viewWidth == 0) {
  70. // if (windowManager != null) {
  71. // Display display = windowManager.getDefaultDisplay();
  72. // viewWidth = display.getWidth();
  73. // }
  74. // }

  75. viewWidth=width;
  76. step = textLength;
  77. temp_view_plus_text_length = viewWidth + textLength;
  78. temp_view_plus_two_text_length = viewWidth + textLength * 2;
  79. y = getTextSize() + getPaddingTop();
  80. paint.setColor(textColor);
  81. }

  82. @Override
  83. public Parcelable onSaveInstanceState() {
  84. Parcelable superState = super.onSaveInstanceState();
  85. SavedState ss = new SavedState(superState);
  86. ss.step = step;
  87. ss.isStarting = isStarting;
  88. return ss;
  89. }


  90. @Override
  91. public void onRestoreInstanceState(Parcelable state) {
  92. if (!(state instanceof SavedState)) {
  93. super.onRestoreInstanceState(state);
  94. return;
  95. }
  96. SavedState ss = (SavedState) state;
  97. super.onRestoreInstanceState(ss.getSuperState());
  98. step = ss.step;
  99. isStarting = ss.isStarting;
  100. }

  101. public static class SavedState extends BaseSavedState {
  102. public boolean isStarting = false;
  103. public float step = 0.0f;
  104. SavedState(Parcelable superState) {
  105. super(superState);
  106. }

  107. @Override
  108. public void writeToParcel(Parcel out, int flags) {
  109. super.writeToParcel(out, flags);
  110. out.writeBooleanArray(new boolean[] { isStarting });
  111. out.writeFloat(step);
  112. }

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

  114. public SavedState[] newArray(int size) {
  115. return new SavedState[size];
  116. }

  117. @Override
  118. public SavedState createFromParcel(Parcel in) {
  119. return new SavedState(in);
  120. }
  121. };

  122. private SavedState(Parcel in) {
  123. super(in);
  124. boolean[] b = null;
  125. in.readBooleanArray(b);
  126. if (b != null && b.length > 0)
  127. isStarting = b[0];
  128. step = in.readFloat();
  129. }
  130. }

  131. /**
  132. * 开始滚动
  133. */

  134. public void startScroll() {
  135. isStarting = true;
  136. invalidate();
  137. }

  138. /**
  139. * 停止滚动
  140. */
  141. public void stopScroll() {
  142. isStarting = false;
  143. invalidate();
  144. }

  145. @Override
  146. public void onDraw(Canvas canvas) {
  147. // super.onDraw(canvas);

  148. canvas.drawText(text,0,text.length(), temp_view_plus_text_length - step, y, paint);
  149. if (!isStarting) {
  150. return;
  151. }
  152. step += speed;
  153. if (step > temp_view_plus_two_text_length)
  154. step = textLength;
  155. invalidate();
  156. }

  157. @Override
  158. public void onClick(View v) {
  159. if (isStarting)
  160. stopScroll();
  161. else
  162. startScroll();

  163. }

  164. }
复制代码


使用:

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();
复制代码
第三种方法:设置其对应的layout中的: addStatesFromChildren = “true”.使用其获得焦点时有跑马灯的效果。
src:http://www.eoeandroid.com/thread-78365-1-1.html

更多相关文章

  1. 为什么要学习 Markdown?究竟有什么用?
  2. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  3. android studio introduction No.3 hotkey
  4. 关于android的pan_display
  5. Android程序如何升级
  6. [Android]使用Dagger 2依赖注入 - 图表创建的性能
  7. android 4.1 UI 工具测试的新利器, uiautomator
  8. Android的蓝牙开发技术(一)
  9. Android源代码编译一次成功

随机推荐

  1. android学习网站
  2. android ksoap2 访问https javax.net.ssl
  3. 【Android】播放提示音
  4. 自定义RatingBar
  5. Android--Intent常用(拨号,浏览器,联系人,Wi-
  6. android 设置textview边框以及点击效果
  7. android:Spinner 设置收起以及展开的字体
  8. Android(安卓)对话框
  9. android软键盘隐藏总结
  10. android4.2上获取应用程序大小的变更点