Jul*_*lia 3 java android android-animation
我正在尝试创建一个TextView从左到右移动并无限循环的动画.这是TextView我想要动画的:
<TextView
android:id="@+id/txtTitle"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="italic"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:ellipsize="end"
android:maxLines="1"
android:layout_centerHorizontal="true"
android:layout_below="@id/cardView" />
Run Code Online (Sandbox Code Playgroud)
这就是我试图动画的方式TextView:
Animation animation = new TranslateAnimation(0, -280, 0, 0);
animation.setDuration(9000);
animation.setRepeatMode(Animation.RELATIVE_TO_SELF);
animation.setRepeatCount(Animation.INFINITE);
textView.setAnimation(animation);
Run Code Online (Sandbox Code Playgroud)
我想要实现的是文本从屏幕中心开始,向右移动,一旦第一个字母离开屏幕,它应该重新出现在另一边.
Xav*_*ler 11
您可以通过简单的方式轻松实现您想要做的事情ValueAnimator.
您首先要做的是将两个相同版本的TextView动画放入您的布局中.在这个例子中,我的布局如下所示:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="32sp"
android:text="@string/hello_word"/>
<TextView
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="32sp"
android:text="@string/hello_word"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
然后 - 正如我已经提到的 - 你使用a ValueAnimator来为两者的translationX属性设置动画Views,但是用屏幕的宽度偏移一个(因为TextViews上面match_parent用作宽度,它们的宽度等于屏幕的宽度,这就是我的意思用来抵消其中一个人的位置).您的代码应如下所示:
final TextView first = (TextView) findViewById(R.id.first);
final TextView second = (TextView) findViewById(R.id.second);
final ValueAnimator animator = ValueAnimator.ofFloat(0.0f, 1.0f);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setInterpolator(new LinearInterpolator());
animator.setDuration(9000L);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
final float progress = (float) animation.getAnimatedValue();
final float width = first.getWidth();
final float translationX = width * progress;
first.setTranslationX(translationX);
second.setTranslationX(translationX - width);
}
});
animator.start();
Run Code Online (Sandbox Code Playgroud)
结果看起来应该是这样的:
| 归档时间: |
|
| 查看次数: |
2843 次 |
| 最近记录: |