Android 如何将一个imageview设置为向上移动到另一个视图并再次返回到原始位置的动画

Android 如何将一个imageview设置为向上移动到另一个视图并再次返回到原始位置的动画,android,animation,imageview,Android,Animation,Imageview,我想以这样一种方式设置ImageView的动画,即它开始从初始位置移动到按钮(向上移动ImageView),然后再返回到初始位置,并无限重复。我对动画制作完全是新手,所以请指导我怎么做 my layout.xml如下所示: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="h

我想以这样一种方式设置ImageView的动画,即它开始从初始位置移动到按钮(向上移动ImageView),然后再返回到初始位置,并无限重复。我对动画制作完全是新手,所以请指导我怎么做

my layout.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".fragment.Scratch1Fragment">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/btn_scratch_now"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/buttonStyle"
            android:text="@string/str_scratch_now"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintVertical_bias="0.2"/>

        <View
            android:id="@+id/upper_bound"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@color/primary"
            android:layout_marginEnd="10dp"
            android:layout_marginStart="10dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintVertical_bias="0.5"/>

        <TextView
            android:id="@+id/text_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/str_scratch"
            style="@style/tvDarkLargeStyle"
            android:gravity="center"
            android:textAlignment="center"
            app:layout_constraintTop_toBottomOf="@+id/upper_bound"
            />

        <View
            android:id="@+id/lower_bound"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@color/primary"
            android:layout_marginEnd="10dp"
            android:layout_marginStart="10dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/text_view"
            />

        <TextView
            android:id="@+id/tv_tap_here"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/lower_bound"
            android:layout_margin="20dp"
            android:padding="10dp"
            android:gravity="center"
            style="@style/tvLightLargeStyle"
            android:textAlignment="center"
            android:text="Tap here"/>

        <ImageView
            android:id="@+id/img_hand"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/clicker_hand_64"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="@+id/upper_bound"/>

    </android.support.constraint.ConstraintLayout>

</FrameLayout>
我也用
ObjectAnimatior
而不是
ValueAnimator
尝试了同样的方法。但不起作用。

试试这个:

ObjectAnimator slideUp = ObjectAnimator.ofFloat(mImageView,"translationY",-btn_scratch_now.getTop());
    slideUp.setRepeatMode(ObjectAnimator.RESTART);
    slideUp.setRepeatCount(ObjectAnimator.INFINITE);
    slideUp.start();

最后,我在这篇文章中提供的所有评论和链接的帮助下找到了我的答案。我不知道这是否是理想的解决方案,但它正在发挥作用。如果有人有更好的解决方案,请提供

我所做的是获取按钮的位置(在解决方案之前是0)

为了获得按钮的中心,我使用了以下行:

float center = (btnScratch.getTop() + btnScratch.getBottom()) / 2;
对于动画:

private void animateUpToButton(final float distance) {

    ObjectAnimator imageAnimator = ObjectAnimator.ofFloat(imgHand, "translationY", 0f, distance);
    imageAnimator.setDuration(ANIM_DURATION);
    imageAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
    imageAnimator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animator) {

        }

        @Override
        public void onAnimationEnd(Animator animator) {
            animateBackToOrigin(distance);
        }

        @Override
        public void onAnimationCancel(Animator animator) {

        }

        @Override
        public void onAnimationRepeat(Animator animator) {

        }
    });
    imageAnimator.start();
}
并且,
onAnimationEnd()
,我添加了另一个动画,以使图像返回初始位置

private void animateBackToOrigin(final float distance) {
    ObjectAnimator imageAnimator = ObjectAnimator.ofFloat(imgHand, "translationY", distance, 0f);
    imageAnimator.setDuration(ANIM_DURATION);
    imageAnimator.setInterpolator(new LinearInterpolator());
    imageAnimator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animator) {

        }

        @Override
        public void onAnimationEnd(Animator animator) {
            animateUpToButton(distance);
        }

        @Override
        public void onAnimationCancel(Animator animator) {

        }

        @Override
        public void onAnimationRepeat(Animator animator) {

        }
    });
    imageAnimator.start();
}

再一次
onAnimationEnd()
,我添加了动画,将图像移向按钮。这样,动画将一个接一个地继续。

原始位置是什么意思?您想将图像向上移动还是向下移动,或者向右移动还是向左移动。请澄清!我想将图像向上移动到按钮上。然后再回到原来的位置。(指初始位置)借助于此尝试。如果你有任何问题,请再问一次!谢谢你的帮助@HeisenBrg。我将尝试你的解决方案。嗨,Riddhi,这个链接可能会帮助你,
private void animateUpToButton(final float distance) {

    ObjectAnimator imageAnimator = ObjectAnimator.ofFloat(imgHand, "translationY", 0f, distance);
    imageAnimator.setDuration(ANIM_DURATION);
    imageAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
    imageAnimator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animator) {

        }

        @Override
        public void onAnimationEnd(Animator animator) {
            animateBackToOrigin(distance);
        }

        @Override
        public void onAnimationCancel(Animator animator) {

        }

        @Override
        public void onAnimationRepeat(Animator animator) {

        }
    });
    imageAnimator.start();
}
private void animateBackToOrigin(final float distance) {
    ObjectAnimator imageAnimator = ObjectAnimator.ofFloat(imgHand, "translationY", distance, 0f);
    imageAnimator.setDuration(ANIM_DURATION);
    imageAnimator.setInterpolator(new LinearInterpolator());
    imageAnimator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animator) {

        }

        @Override
        public void onAnimationEnd(Animator animator) {
            animateUpToButton(distance);
        }

        @Override
        public void onAnimationCancel(Animator animator) {

        }

        @Override
        public void onAnimationRepeat(Animator animator) {

        }
    });
    imageAnimator.start();
}