javaandroidanimationandroid-popupwindow

How to move a view from right to left continuous using animations?


I want to show a navigation guide for that I am using popup window in which I have a hand image which should move from right to left of the window continuous. It should start from right, go to left again from right to left swipe should be shown.

I am using an view pager so I want to guide by showing a guide using animation.

I tried like this.

    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);

It is moving from its location to right to the main screen disapears for a second and again stops at the specified location.

Anim right to left

<?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>

layout :

<?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 window class

    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();

            }
        });

    }

Please help with the same. Thank you..


Solution

  • You can try this instead of XML animation setup

            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);