【问题标题】:TextView scrolling and animation in AndroidAndroid中的TextView滚动和动画
【发布时间】:2011-04-17 11:56:18
【问题描述】:

我已经成功启用了 Android TextView 的滚动功能,方法是将其放置在 ScrollView 中,或者使用 TextView 的 setMovementMethod(例如 myTextView.setMovementMethod(new ScrollingMovementMethod()); )。

但是,理想情况下,我希望看到 TextView 以类似于 iPhone/IPod touch 的方式滚动,其中文​​本过冲并反弹回来。在模拟器中,TextView 只是滚动到开头或结尾,没有任何动画效果。

是否有一种简单的方法来启用这种滚动行为或使用 Android 的动画功能和 OvershootInterpolator 的其他方法?

【问题讨论】:

  • 如果它解决了您的问题,请接受答案,如果您自己解决,请添加答案。

标签: android scroll textview


【解决方案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();
}

这个动画正是你想要的。

【讨论】:

  • 感谢您的回复,它对我有用。 getTextViewWidth() 方法有问题,因为 measuredWidth 似乎没有包含文本视图的填充,但通过将填充添加到最终结果很容易解决。
【解决方案2】:

听起来像默认的 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 方法。它应该默认工作。

【讨论】:

    【解决方案3】:

    对于完整的 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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-31
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 2011-04-22
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      相关资源
      最近更新 更多