[關鍵字] Android / TextView / Speed / Marquee / Scroller / Velocity

 

上一篇我們約略知道了如果要改變跑馬燈的速度就要由 Scroller 下手,

所以這一篇我們就實際開始動動手腳。

 

首先建構一個 Class 繼承 TextView,然後利用我們自己定義的 Scroller 去

改變文字移動的行為,程式碼如下:

(MarqueeTextView.java)

 package com.anythru.trymarqueetextview;  
 import android.content.Context;  
 import android.graphics.Rect;  
 import android.util.AttributeSet;  
 import android.util.Log;  
 import android.view.animation.LinearInterpolator;  
 import android.widget.Scroller;  
 import android.widget.TextView;  
 public class MarqueeTextView extends TextView {  
   Context context;  
   Scroller mScroller;  
   int mDistance;  
   int mDuration;  
   float mVelocity;  
   public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {  
     super(context, attrs, defStyle);  
     this.context = context;  
     setSingleLine();  
     // 在 new Scroller 時候加入 LinearInterpolater(線性插補器) 參數  
     // 此參數可以讓我們的跑馬燈以線性等速度移動,不然預設是黏滯地(viscous)移動  
     mScroller = new Scroller(context, new LinearInterpolator());  
     setScroller(mScroller);  
   }  
   public MarqueeTextView(Context context, AttributeSet attrs) {  
     super(context, attrs);  
     this.context = context;  
     setSingleLine();  
     mScroller = new Scroller(context, new LinearInterpolator());  
     setScroller(mScroller);  
   }  
   public MarqueeTextView(Context context) {  
     super(context);  
     this.context = context;  
     setSingleLine();  
     mScroller = new Scroller(context, new LinearInterpolator());  
     setScroller(mScroller);  
   }  
   @Override  
   public void computeScroll() {  
     super.computeScroll();  
     if(mScroller.computeScrollOffset()){  
       scrollTo(mScroller.getCurrX(), mScroller.getCurrY());  
     } else {  
       if(mScroller.isFinished()){  
         mScroller.startScroll(-getWidth(), 0, calculateMoveDistance(false, mVelocity), 0, mDuration);  
       }  
     }  
   }  
   /**  
    * 計算應該移動的距離  
    * @param isFirstRun 是否是第一輪  
    * @param velocity 速率  
    * @return  
    */  
   private int calculateMoveDistance(boolean isFirstRun, float velocity){  
     Rect rect = new Rect();  
     String textString = (String) getText();  
     getPaint().getTextBounds(textString, 0, textString.length(), rect);  
     int moveDistance = rect.width();  
     rect = null;  
     this.mDistance = isFirstRun ? moveDistance : moveDistance + getWidth();  
     this.mDuration = (int) (velocity * mDistance);  
     return this.mDistance;  
   }  
   /**  
    * 從這裡設定速度值  
    * @param velocity 速度值越小越快  
    */  
   public void scrollText(float velocity){  
     this.mVelocity = velocity;  
     mScroller.startScroll(0, 0, calculateMoveDistance(true, velocity), 0, mDuration);  
   }  
 }  

 然後在我們的 Activity 裡頭,可以更簡化的啟動它:

   
 public class MainActivity extends Activity {  
   
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     // 簡化到只需要三行  
     MarqueeTextView MarqueeTV = new MarqueeTextView(this);  
     MarqueeTV.setText("THIS IS A LONG LONG STRING....");  
     MarqueeTV.setTextSize(TypedValue.COMPLEX_UNIT_SP, 32);  
       
     MarqueeTextView MarqueeTV2 = new MarqueeTextView(this);  
     MarqueeTV2.setText("THIS IS A LONG LONG STRING....");  
     MarqueeTV2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 32);  
       
     LinearLayout baseRL = (LinearLayout) findViewById(R.id.base_linearlayout);  
     baseRL.addView(MarqueeTV, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));  
     baseRL.addView(MarqueeTV2, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));  
       
     // 從這裡啟動  
     MarqueeTV.scrollText(20);  
     MarqueeTV2.scrollText(10);  
       
   }  

於是我們就可以擁有隨我們喜好速度的跑馬燈文字:

Screen Shot 2013-09-02 at 10.26.31 PM  

 

 

 

 

 

arrow
arrow
    全站熱搜

    keep walking 發表在 痞客邦 留言(0) 人氣()