我和你有同樣的問題。經過深入細緻的研究小時,我做了這個:
在你 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();
}
這個動畫正在做你想要的東西。
接受的答案,如果它解決您的問題,或添加答案如果你自己做的。 – Talha 2017-07-18 09:44:03