2010-09-26 76 views
2

我已成功啓用滾動的Android TextView,方法是將其置於ScrollView中,或者使用TextView的setMovementMethod(例如,myTextView.setMovementMethod(new ScrollingMovementMethod());)。Android中的TextView滾動和動畫

但是,理想情況下,我希望看到TextView滾動的方式類似於IPhone/IPod touch,文本過沖和反彈。在模擬器中,TextView只需滾動到開頭或結尾,沒有任何動畫效果。

有沒有簡單的方法來啓用這種滾動行爲或其他一些使用Android動畫功能和OvershootInterpolator的方法?

+0

接受的答案,如果它解決您的問題,或添加答案如果你自己做的。 – Talha 2017-07-18 09:44:03

回答

1

我和你有同樣的問題。經過深入細緻的研究小時,我做了這個:
在你 XML佈局,你有這樣的事情:

<HorizontalScrollView 
     android:id="@+id/scroll" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fadingEdgeLength="0dp" 
     android:scrollbars="none" > 

     <TextView 
      android:id="@+id/textview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:maxLines="1" /> 

    </HorizontalScrollView> 

現在最有趣的部分:

private TextView mTextView... 
private Animation mAnimation... 

private void animateTextView() { 
    int textWidth = getTextViewWidth(mTextView); 
    int displayWidth = getDisplayWidth(mContext); 

    /* Start animation only when text is longer than dislay width. */ 
    if(displayWidth<textWidth) { 
     mAnimation = new TranslateAnimation(
       0, displayWidth-textWidth, 
       0, 0); 
     mAnimation.setDuration(3000); // Set custom duration. 
     mAnimation.setStartOffset(500); // Set custom offset. 
     mAnimation.setRepeatMode(Animation.REVERSE); // This will animate text back ater it reaches end. 
     mAnimation.setRepeatCount(Animation.INFINITE); // Infinite animation. 

     mTextView.startAnimation(mAnimation); 
    } 
} 

private int getDisplayWidth(Context context) { 
    int displayWidth; 

    WindowManager windowManager = (WindowManager)context.getSystemService(
      Context.WINDOW_SERVICE); 
    Display display = windowManager.getDefaultDisplay(); 
    Point screenSize = new Point(); 

    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB_MR2) { 
     display.getSize(screenSize); 
     displayWidth = screenSize.x; 
    } else { 
     displayWidth = display.getWidth(); 
    } 

    return displayWidth; 
} 

private int getTextViewWidth(TextView textView) { 
    textView.measure(0, 0); // Need to set measure to (0, 0). 
    return textView.getMeasuredWidth(); 
} 

這個動畫正在做你想要的東西。

0

聽起來像默認的ListView行爲可能是你想要的。在XML中定義ListView,然後設置一個在TextView中提供的自定義適配器。

<ListView android:id="@+id/ListView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

的ListView文檔:http://developer.android.com/reference/android/widget/ListView.html

你可以看到,有一個OnOverScrolled方法。它應該默認工作。

0

對於全TextView的滾動,我們可以編輯#franta kocourek指定的代碼下面

private void animateTextView() { 
    int textWidth = getTextViewWidth(txtLastLocation); 
    int displayWidth = Device.getDisplayWidth(getContext()); 

    if((displayWidth)<=textWidth) { 
     textViewAnimation = new TranslateAnimation(
       textWidth, -textWidth, 
       0, 0); 
     textViewAnimation.setDuration(10000); // Set custom duration. 
     textViewAnimation.setStartOffset(0);// Set custom offset. 
     textViewAnimation.setRepeatMode(Animation.RESTART); // This will animate text back ater it reaches end. 
     textViewAnimation.setRepeatCount(Animation.INFINITE); // Infinite animation. 

     mTextView.startAnimation(textViewAnimation); 
    } 
}