如何使用动画从右向左连续移动视图?

问题描述 投票:0回答:1

我想显示一个有关我正在使用弹出窗口的导航指南,在该弹出窗口中我有一个手形图像,该图像应该从窗口的右向左连续移动。它应从右开始,再从右向左再向左滑动,应显示为滑动。

我正在使用视图寻呼机,因此我想通过显示动画指南来进行指导。

我尝试过这样。

    ImageView imageViewSwipeHand =
            popupView.findViewById(R.id.imageView_swipe_hand);

    Animation RightSwipe = AnimationUtils.loadAnimation(
            context,
            R.anim.swipe_right_to_left
    );

    RightSwipe.setRepeatMode(Animation.RESTART);


    RightSwipe.setInterpolator(new LinearInterpolator());
    RightSwipe.setRepeatCount(Animation.INFINITE);

    imageViewSwipeHand.startAnimation(RightSwipe);

它从其位置向右移动到主屏幕消失了一秒钟,然后再次停在指定位置。

从右到左的动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android = "http://schemas.android.com/apk/res/android"
     android:shareInterpolator = "false">
    <translate
        android:duration = "700"
        android:fromXDelta = "0%"
        android:fromYDelta = "0%"
        android:toXDelta = "100%"
        android:toYDelta = "0%" />
</set>

布局:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android"
                                                   xmlns:app = "http://schemas.android.com/apk/res-auto"
                                                   android:layout_width = "match_parent"
                                                   android:layout_height = "match_parent"
                                                   android:background = "@drawable/layout_background">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:padding = "@dimen/margin_20"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toTopOf = "parent">

        <ImageView
            android:id = "@+id/imageView_swipe_hand"
            android:layout_width = "70dp"
            android:layout_height = "70dp"
            android:layout_marginTop = "@dimen/margin_20"
            android:src = "@drawable/ic_swipe_hand"
            app:layout_constraintRight_toRightOf = "parent"
            app:layout_constraintTop_toTopOf = "parent" />

        <TextView
            android:id = "@+id/textView_swipe"
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:layout_marginTop = "@dimen/margin_30"
            android:fontFamily = "@font/open_sans_bold"
            android:text = "@string/swipe_instruction"
            android:textColor = "@android:color/black"
            android:textSize = "@dimen/text_size_14"
            app:layout_constraintEnd_toEndOf = "parent"
            app:layout_constraintStart_toStartOf = "parent"
            app:layout_constraintTop_toBottomOf = "@id/imageView_swipe_hand" />

        <Button
            android:id = "@+id/button_got_it"
            style = "?android:attr/borderlessButtonStyle"
            android:layout_width = "@dimen/margin_240"
            android:layout_height = "@dimen/margin_40"
            android:layout_centerHorizontal = "true"
            android:layout_marginTop = "@dimen/margin_20"
            android:background = "@drawable/button_background"
            android:fontFamily = "@font/open_sans_semibold"
            android:text = "@string/got_it"
            android:textAllCaps = "false"
            android:textColor = "@android:color/black"
            android:textSize = "@dimen/text_size_14"
            app:layout_constraintEnd_toEndOf = "parent"
            app:layout_constraintHorizontal_bias = "0.497"
            app:layout_constraintLeft_toLeftOf = "parent"
            app:layout_constraintRight_toRightOf = "parent"
            app:layout_constraintStart_toStartOf = "parent"
            app:layout_constraintTop_toBottomOf = "@+id/textView_swipe" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

PopUp窗口类

    public void showPopupWindow(final View view, AppCompatActivity context) {

        //Create a View object yourself through inflater
        LayoutInflater inflater = (LayoutInflater) view.getContext()
                .getSystemService(view.getContext().LAYOUT_INFLATER_SERVICE);
        View popupView = inflater.inflate(R.layout.instruction_layout, null);


        //Make Inactive Items Outside Of PopupWindow
        boolean focusable = true;

        PopupWindow popupWindow = new PopupWindow(
                popupView,
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT
        );

        popupWindow.showAtLocation(view, Gravity.CENTER,
                0, 0
        );

        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);

        ImageView imageViewSwipeHand =
                popupView.findViewById(R.id.imageView_swipe_hand);

        Animation RightSwipe = AnimationUtils.loadAnimation(
                context,
                R.anim.swipe_right_to_left
        );

        RightSwipe.setRepeatMode(Animation.RESTART);

        RightSwipe.setInterpolator(new LinearInterpolator());
        RightSwipe.setRepeatCount(Animation.INFINITE);

        imageViewSwipeHand.startAnimation(RightSwipe);

        Button button = popupView.findViewById(R.id.button_got_it);

        button.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {

                popupWindow.dismiss();

            }
        });

    }

请同样提供帮助。谢谢..

java android animation android-popupwindow
1个回答
0
投票

您可以尝试使用此方法代替XML动画设置

        Animation RightSwipe = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, 0f);
        RightSwipe.setDuration(1000);
        RightSwipe.setFillAfter(true);
        RightSwipe.setRepeatCount(Animation.INFINITE);
© www.soinside.com 2019 - 2024. All rights reserved.