Android在滑动手势上显示dimsiss图像视图

Android在滑动手势上显示dimsiss图像视图,android,android-layout,animation,imageview,swipe,Android,Android Layout,Animation,Imageview,Swipe,在RelativeLayout中,我在TextView下有一个TextView和一个按钮,还有一个ImageView,它覆盖了这两个对象。当用户打开活动时,ImageView应涵盖其他项目。 当用户在图像(左或右)上滑动时,它应该关闭,以便用户可以操作按钮并读取文本字段 使用onClick侦听器,我可以在单击时关闭图像,但我想要一个动画,就像在RecicleView中滑动以删除元素一样。您可以将所需的动画添加到onClick事件中 1) 在res目录中创建(或添加,如果存在)anim文件夹,并放

在RelativeLayout中,我在TextView下有一个TextView和一个按钮,还有一个ImageView,它覆盖了这两个对象。当用户打开活动时,ImageView应涵盖其他项目。 当用户在图像(左或右)上滑动时,它应该关闭,以便用户可以操作按钮并读取文本字段


使用onClick侦听器,我可以在单击时关闭图像,但我想要一个动画,就像在RecicleView中滑动以删除元素一样。

您可以将所需的动画添加到onClick事件中

1) 在res目录中创建(或添加,如果存在)anim文件夹,并放置slide_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

   <translate
        android:fromXDelta="0%p"
        android:toXDelta="75%p"
        android:duration="800" />
</set>
可以修改xml中的动画规格,以便获得所需的效果

然后实现AnimatorListener,这样就可以在动画结束时关闭图像视图

animSlide.setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            //remove image view from the layout
        }
    });
animSlide.setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            //remove image view from the layout
        }
    });