我已经通过将 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/