android - Android 中的 TextView 滚动和动画

标签 android scroll textview

我已经通过将 Android TextView 放置在 ScrollView 中或使用 TextView 的 setMovementMethod(例如,myTextView.setMovementMethod(new ScrollingMovementMethod());)成功地启用了滚动。

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

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

最佳答案

我遇到了和你一样的问题。经过数小时的深入研究,我做了这个:
在您的 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();
}

这个动画正在做你想要的。

关于android - Android 中的 TextView 滚动和动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3799406/

相关文章:

android - 如何在 kotlin 中使用 http 请求将图像上传到服务器?

android - 如何以编程方式在android中打开目录

javascript - 在顶部添加元素时保持滚动位置在 Firefox 中有效,但在 Chrome 中无效

android - 如何设置TextView结构的颜色?

android - textview 下划线在真实设备上不显示

java - 使用 userID 获取 Facebook 的大头像

android - 使用处理程序自动滚动 WebView

javascript - 如果 body 有一个类在滚动原生 javascript 上删除

Android在文本周围画圈

java - 为什么我的低电量警报代码不起作用?