1. 程式人生 > >TextView跑馬燈的兩種實現

TextView跑馬燈的兩種實現

普通的TextView可以實現跑馬燈,但是隻有當焦點在它上面時才有效。

如何做一個自動的跑馬燈呢?
第一種:繼承TextView,然後重寫isFocused()方法就可以了,簡單!
Java程式碼  
  1. import android.content.Context;  
  2. import android.util.AttributeSet;  
  3. import android.widget.TextView;  
  4. publicclass ScrollForeverTextView extends TextView {  
  5.     public ScrollForeverTextView(Context context) {  
  6.         super(context);  
  7.         // TODO Auto-generated constructor stub
  8.     }  
  9.     public ScrollForeverTextView(Context context, AttributeSet attrs) {  
  10.         super(context, attrs);  
  11.     }  
  12.     public ScrollForeverTextView(Context context, AttributeSet attrs,  
  13.             int defStyle) {  
  14.         super
    (context, attrs, defStyle);  
  15.     }  
  16.     @Override
  17.     publicboolean isFocused() {  
  18.         returntrue;  
  19.     }  
  20. }  

使用時同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. publicclass AutoScrollTextView extends TextView implements OnClickListener {  
  13.     publicfinalstatic String TAG = AutoScrollTextView.class.getSimpleName();  
  14.     privatefloat textLength = 0f;// 文字長度
  15.     privatefloat viewWidth = 0f;  
  16.     privatefloat step = 0f;// 文字的橫座標
  17.     privatefloat y = 0f;// 文字的縱座標
  18.     privatefloat temp_view_plus_text_length = 0.0f;// 用於計算的臨時變數
  19.     privatefloat temp_view_plus_two_text_length = 0.0f;// 用於計算的臨時變數
  20.     publicboolean isStarting = false;// 是否開始滾動
  21.     private Paint paint = null;// 繪圖樣式
  22.     private CharSequence text = "";// 文字內容
  23.     privatefloat speed = 0.5f;  
  24.     privateint textColor=0xFF000000;  
  25.     publicint getTextColor() {  
  26.         return textColor;  
  27.     }  
  28.     publicvoid setTextColor(int color) {  
  29.         this.textColor = color;  
  30.     }  
  31.     publicfloat getSpeed() {  
  32.         return speed;  
  33.     }  
  34.     publicvoid setSpeed(float speed) {  
  35.         this.speed = speed;  
  36.     }  
  37.     public AutoScrollTextView(Context context) {  
  38.         super(context);  
  39.         initView();  
  40.     }  
  41.     public AutoScrollTextView(Context context, AttributeSet attrs) {  
  42.         super(context, attrs);  
  43.         initView();  
  44.     }  
  45.     public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {  
  46.         super(context, attrs, defStyle);  
  47.         initView();  
  48.     }  
  49.     privatevoid initView() {  
  50.         setOnClickListener(this);  
  51.     }  
  52.     publicvoid init(float width) {  
  53.         text=super.getText();  
  54.         paint = super.getPaint();  
  55. //      Paint paint=new Paint();
  56.         text = getText().toString();  
  57.         textLength = paint.measureText(text.toString());  
  58. //      viewWidth = getWidth();
  59. //      if (viewWidth == 0) {
  60. //          if (windowManager != null) {
  61. //              Display display = windowManager.getDefaultDisplay();
  62. //              viewWidth = display.getWidth();
  63. //          }
  64. //      }
  65.         viewWidth=width;  
  66.         step = textLength;  
  67.         temp_view_plus_text_length = viewWidth + textLength;  
  68.         temp_view_plus_two_text_length = viewWidth + textLength * 2;  
  69.         y = getTextSize() + getPaddingTop();  
  70.         paint.setColor(textColor);  
  71.     }  
  72.     @Override
  73.     public Parcelable onSaveInstanceState() {  
  74.         Parcelable superState = super.onSaveInstanceState();  
  75.         SavedState ss = new SavedState(superState);  
  76.         ss.step = step;  
  77.         ss.isStarting = isStarting;  
  78.         return ss;  
  79.     }  
  80.     @Override
  81.     publicvoid onRestoreInstanceState(Parcelable state) {  
  82.         if (!(state instanceof SavedState)) {  
  83.             super.onRestoreInstanceState(state);  
  84.             return;  
  85.         }  
  86.         SavedState ss = (SavedState) state;  
  87.         super.onRestoreInstanceState(ss.getSuperState());  
  88.         step = ss.step;  
  89.         isStarting = ss.isStarting;  
  90.     }  
  91.     publicstaticclass SavedState extends BaseSavedState {  
  92.         publicboolean isStarting = false;  
  93.         publicfloat step = 0.0f;  
  94.         SavedState(Parcelable superState) {  
  95.             super(superState);  
  96.         }  
  97.         @Override
  98.         publicvoid writeToParcel(Parcel out, int flags) {  
  99.             super.writeToParcel(out, flags);  
  100.             out.writeBooleanArray(newboolean[] { isStarting });  
  101.             out.writeFloat(step);  
  102.         }  
  103.         publicstaticfinal Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {  
  104.             public SavedState[] newArray(int size) {  
  105.                 returnnew SavedState[size];  
  106.             }  
  107.             @Override
  108.             public SavedState createFromParcel(Parcel in) {  
  109.                 returnnew SavedState(in);  
  110.             }  
  111.         };  
  112.         private SavedState(Parcel in) {  
  113.             super(in);  
  114.             boolean[] b = null;  
  115.             in.readBooleanArray(b);  
  116.             if (b != null && b.length > 0)  
  117.                 isStarting = b[0];  
  118.             step = in.readFloat();  
  119.         }  
  120.     }  
  121.     publicvoid startScroll() {  
  122.         isStarting = true;  
  123.         invalidate();  
  124.     }  
  125.     publicvoid stopScroll() {  
  126.         isStarting = false;  
  127.         invalidate();  
  128.     }  
  129.     @Override
  130.     publicvoid onDraw(Canvas canvas) {  
  131. //      super.onDraw(canvas);
  132.         canvas.drawText(text,0,text.length(), temp_view_plus_text_length - step, y, paint);  
  133.         if (!isStarting) {  
  134.             return;  
  135.         }  
  136.         step += speed;  
  137.         if (step > temp_view_plus_two_text_length)  
  138.             step = textLength;  
  139.         invalidate();  
  140.     }  
  141.     @Override
  142.     publicvoid onClick(View v) {  
  143.         if (isStarting)  
  144.             stopScroll();  
  145.         else
  146.             startScroll();  
  147.     }  
  148. }  

使用:
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),否則停頓現象比較明顯。

相關推薦

TextView馬燈-實現方式

Android中TextView跑馬燈有多種實現方式; 一種是自定義控制元件,另外一種是寫一個工具類 自定義控制元件 /** * Created by iblade.Wang on 2018/10/24 10:29 */ public class Marqu

TextView馬燈實現

普通的TextView可以實現跑馬燈,但是隻有當焦點在它上面時才有效。 如何做一個自動的跑馬燈呢? 第一種:繼承TextView,然後重寫isFocused()方法就可以了,簡單! Java程式碼   import android.content.Context;  

一、Android 實現TextView馬燈效果

超長的文字在有限的佈局中,實現一行迴圈顯示?如果是隻有一個TextView實現跑馬燈效果可以簡單使用以下方式來實現: <TextView android:layout_width="wrap_content" android:layout

Android 文字自動滾動(馬燈)效果的實現方法[特別好使]

    public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {         super(context, attrs, defStyle);         initView();     }      

Android 文字自動滾動(馬燈)效果的實現方法

    public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {         super(context, attrs, defStyle);         initView();     }      

簡單實現TextVIew馬燈效果

<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="莫聽穿林打葉聲,何妨吟嘯且徐行。竹杖芒鞋輕勝馬,誰怕?一蓑煙雨任平生。 "

Android TextView文字鏤空效果的實現

一圖勝千言 文字鏤空效果主要有兩種實現方式: 1,自動義View,在canvas中繪製圓角矩形作為背景,然後繪製文字,通過PorterDuff.Mode.DST_OUT把背景擦除,實現鏤空效果。如上圖中的第一個。 2,自定義TextView,定義兩Bitmap,分別在Bitma

關於Android實現TextView馬燈效果

在xml屬性中設定 <TextView android:width="wrap_content" android:height="wrap_content" android:singleLine="true" andr

android學習:TextView 馬燈實現

最近無意間看到了涉及到跑馬燈效果的程式碼,於是在網上查閱了很多資料,在這裡對自己看的一些文章進行一下總結,順便加上自己的一些體會。 讓我們一步步逐漸向下。 首先我們要實現走馬燈這樣一個效果,通常來說都是在TextView這個控制元件中來實現的,而且其中的文

左邊固定,右邊自適應布局的實現

定位 abs red logs light 正常 idt blue mar html結構: <body> <div class="left"></div> <div class="right"></div

easyui combobox 三級級聯 input 實現

data edit pat adc inpu idt cts wid req /**<img src=‘${pageContext.request.contextPath}/images/block.png‘/> * @param 默認載入 省市 */

CSS3實現五子棋Web小遊戲,Canvas畫布和DOM實現,並且具有悔棋和撤銷悔棋功能。

posit oct padding 角色 sar pac osi fse ech 用Canvas實現五子棋的思路: 1、點擊棋盤,獲取坐標x,y,計算出棋子的二維數組坐標i和j, 2、棋子的實現,先arc一個圓,再填充漸變色。 3、下完一步棋後切換畫筆和角色。 4、贏法算法

[轉]Web APi之認證(Authentication)實現方式【二】(十三)

用戶數 ted das 客戶 元素 基礎 目標 開始 net 本文轉自:http://www.cnblogs.com/CreateMyself/p/4857799.html 前言 上一節我們詳細講解了認證及其基本信息,這一節我們通過兩種不同方式來實現認證,並且分析如

android縮放動畫的實現方法

get odi omx rac tor Coding eight rpo odin 在android開發。我們會常常使用到縮放動畫,普通情況下縮放動畫有兩種實現方式。一種是直接通過java代碼去實現,第二種是通過配置文件實現動畫,以下是兩種動畫的基本是用法: Ja

多線程實現方式的區別

http [] tick 避免 main 單繼承 style 區別 tar 請解釋Thread類與Runnable接口實現多線程的區別?(請解釋多線程兩種實現方式的區別?) 1. Thread類時Runnable接口的子類,使用Runnable接口實現多線程可以避免單繼承局

JPA 派生標識符的實現方式

string column public tid man pri one embed page 方法一:@Entity@IdClass(ModuleId.class)public class Module { @Id private Integer index;

斐波那契數列的實現(遞歸和非遞歸)

result 數列 == 非遞歸 fib color 效率 i++ 思想 查找斐波納契數列中第 N 個數。 所謂的斐波納契數列是指: 前2個數是 0 和 1 。 第 i 個數是第 i-1 個數和第i-2 個數的和。 斐波納契數列的前10個數字是: 0, 1, 1, 2,

14、Fibonacci的實現方式

等於 cheng pos art log ref clas gpo tar 斐波納契數列,又稱黃金分割數列,指的是這樣一個數列:1、1、2、3、5、8、13、21、……在數學上,斐波納契數列以如下被以遞歸的方法定義:F0=0,F1=1,Fn=F(n-1)+F(n-2)(n&

Web APi之認證(Authentication)實現方式【二】(十三)

基於web 推薦 zed {0} scheme sage https 函數 ges 原文:Web APi之認證(Authentication)兩種實現方式【二】(十三)前言 上一節我們詳細講解了認證及其基本信息,這一節我們通過兩種不同方式來實現認證,並且分析如何合理的利用

spring ----> aop的實現方式

select imp ack exe readv expr gpo for public 實現1:基於xml 1 package com.rr.spring3.interf; //接口 2 3 public interface SayHello { 4 5