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



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

我正在使用视图寻呼机,所以我想通过使用动画显示指南来指导。

我试过这样。

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>

弹出窗口类

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

请帮忙。谢谢。。

您可以尝试此操作而不是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);

最新更新