ObjectAnimator:从右到左查看移动动画

时间:2017-08-11 08:03:13

标签: android

从左到右查看移动动画效果正常

int startXposition = 5; // px
int endXposition = 960; // px

ImageView imageView = new ImageView(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50);
imageView.setImageResource(imageSrc);
params.setMargins(startXposition, 5, 0 ,0);
imageView.setLayoutParams(params);
mMainRootContrainer.addView(imageView);
ObjectAnimator objectanimatorX = ObjectAnimator.ofFloat(imageView, "x", endXposition);
...

但是如何从右到左动画?当开始x位置值大于结束x位置值?

int startXposition = 1800;
int endXposition = 960;
...

当我设置它时,更大的动画并不显示;

2 个答案:

答案 0 :(得分:1)

从右向左移动视图

<强> slide_in_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:duration="@android:integer/config_mediumAnimTime"
    android:fromXDelta="100%p"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:toXDelta="0" />

答案 1 :(得分:0)

实际上它是有效的我只是忘记了我的监听器,如果开始x位置大于结束x位置,则移除了我的imageView的可见性

objectanimatorX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        Float value = (Float) animation.getAnimatedValue();
                        if (value >= endXposition) {
                            imageView.setVisibility(View.GONE);
                        }
                    }
                });