android - 动画android后按钮不起作用

标签 android animation view translate-animation

我在 android 中创建了一个 View ,我需要从下到上对其进行动画处理,反之亦然。我已经使用 TranslateAnimation 成功地做到了这一点。但问题是我在 View 上有几个按钮。 WHen animated 那里的触摸点保留在原始位置并且不会移动到新位置。因此,当我再次单击按钮的原始位置时,从上到下的动画会运行,但按钮不在那里。

我在网上搜索过,有人说用参数调用 view.layout,但我的问题是如何获取 View 的最新位置,因为我试图获取动画开始和动画结束时的位置,它保持不变。

此外,请不要给出它实际上不会移动 View 的答案,它会创建一个副本并移动它等等,因为我已经搜索过但找不到描述或实现的适当解决方案。

代码如下:

import android.content.Context;
import android.graphics.Matrix;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.Transformation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import com.Card_Android.R;

public class GameMenuScreenAnimated extends LinearLayout {

private final View mainView;

public GameMenuScreenAnimated(Context context,
        final GameViewController dashboardVC) {

    super(context);
    LayoutInflater inflater = LayoutInflater.from(context);
    mainView = inflater.inflate(R.layout.main_menu, this);

    ImageButton btn = (ImageButton) mainView.findViewById(R.id.trade);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            System.out.println("yaaaaaay!!!!");

        }
    });

    slideDown(mainView);
}

public View getMainView() {
    return mainView;
}

private void slideUp(final View view) {

    Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
            0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
            0.5f);
    slide.setDuration(1000);
    slide.setFillAfter(true);
    slide.setFillEnabled(true);
    view.startAnimation(slide);
    slide.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            int[] startPosition = new int[2];
            view.getLocationOnScreen(startPosition);
            System.out.println("onAnimationStart " + startPosition[0]
                    + " , " + startPosition[1]);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
             int[] endPosition = new int[2];
             view.getLocationOnScreen(endPosition);
             System.out.println("onAnimationEnd " + endPosition[0] + " , "
             + endPosition[1]);

        }

    });

}

private final AnimationListener slideDownAnimationListener = new AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        final int left = mainView.getLeft();
        final int top = mainView.getTop();
        final int right = mainView.getRight();
        final int bottom = mainView.getBottom();
        mainView.layout(left, (int) Math.round(top + 0.25 * top), right,
                (int) Math.round(bottom + 0.25 * bottom));
    }
};

private final Animation slideDownAnimation = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
        Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.25f);

private void slideDown(final View view) {
    slideDownAnimation.setDuration(1000);
    slideDownAnimation.setFillAfter(true);
    slideDownAnimation.setFillEnabled(true);
    slideDownAnimation.setAnimationListener(slideDownAnimationListener);
    view.startAnimation(slideDownAnimation);
}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="bottom"
>
 <LinearLayout 
    android:id="@+id/tableLayout1"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:gravity="center"
    >

        <ImageButton
            android:id="@+id/trade"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="0dp"

            android:background="@null"
            android:scaleType="centerCrop"
            android:src="@drawable/upgrade_btn" />

    </LinearLayout>

 <LinearLayout 
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:gravity="center"
    android:layout_height="wrap_content"
    >

        <ImageButton
                    android:id="@+id/evolution"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="0dp"
                    android:background="@null"
                    android:scaleType="centerCrop"
                    android:src="@drawable/sell_btn" />
            <ImageButton
                    android:id="@+id/trade"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="0dp"
                    android:background="@null"
                    android:scaleType="centerCrop"
                    android:src="@drawable/upgrade_btn"
                     />

    </LinearLayout>
</LinearLayout>

可绘制的xml

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

<selector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/evolution">
<item android:state_pressed="true" android:drawable="@drawable/menu_off" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="@drawable/menu_on" /> <!-- focused -->
<item android:drawable="@drawable/menu_on" /> <!-- default -->
</selector>

我想要做的就是创建一个简单的菜单,其中包含几个按钮,这些按钮会在单击按钮时滑入和滑出。

最佳答案

简单的解决方案: 只需使用 AnimationListener 并使用 .clearAnimation()。

Animation animationLeft= AnimationUtils.loadAnimation(getContext(), R.anim.slide_right);
animationLeft.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        imageView.clearAnimation();//This Line Added
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });

关于android - 动画android后按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13318757/

相关文章:

android - android 中的 Branch.io 深层链接无论如何都会打开 Play 商店

ios - 我想在 iOS 中按角度旋转图像

动画帧渲染后的 JavaScript 运行函数

python - 将numpy结构化数组子集转换为numpy数组而不进行复制

Java - 从单独的类打开一个新 View

android - 如何在 calabash android 中设置 SCRENSHOT_VIA_USB=true 。

android - 了解 Android VSYNC 信号的必要性

android - 在 Kotlin 中支持 Lollipop

html - 为什么添加旋转动画会偏移我的图像?

SQL:如何使用SQL逻辑创建基于不同时间间隔的每日 View ?