Android动画后按钮无法工作

3
我在Android中创建了一个视图,需要从底部向上和相反方向进行动画。我已经使用TranslateAnimation成功实现了这一点。但问题是,我的视图上有几个按钮。当动画播放时,它们的触摸点仍然停留在原始位置,不会移动到新位置。所以当我再次点击按钮的原始位置时,从上到下的动画会运行,但按钮不在那里。
我已经搜索了互联网,人们说要用参数调用view.layout,但我的问题是如何获取视图的最新位置,因为我尝试在动画开始和结束时获取位置,但位置保持不变。
另外,请不要给出答案,说它实际上并没有移动视图,而是创建了一个副本并将其移动等等,因为我已经搜索过了,但找不到适当的解决方案描述或实现。
以下是代码:
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>

我想做的就是创建一个简单的菜单,里面有几个按钮,在按钮点击时可以滑入和滑出。


给你!我已经添加了代码。 - Farooq Arshed
顺便提一下,还有另外两个drwable xml文件,我没有提到,它们基本上是一样的,只是图片有所不同。 - Farooq Arshed
兄弟,这是我的问题,我已经解决了,请检查我的答案。虽然有点晚,但希望你能得到一些东西。 - Farhan Shah
3个回答

2
简单解决方案:只需使用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) {

                    }
                });

适用于我。 - Vishva Vijay
这并不是问题所在。这将移除动画,视图将被发送到动画之前的位置。 - dreameral

1

我之前也遇到了同样的问题。你可以在这里找到我的答案:

如何在动画后应用视图位置的更改?

另一种方法是使用animate()函数或ObjectAnimator。这两种动画方式都会影响视图对象本身,而不仅仅是在屏幕上动画像素而不更新视图位置的内部值。你可以在这里找到更多关于这些函数的信息:

animate()(又名ViewPropertyAnimator): http://developer.android.com/guide/topics/graphics/prop-animation.html#view-prop-animator

ObjectAnimator:http://developer.android.com/guide/topics/graphics/prop-animation.html#object-animator

整个动画章节是非常值得一读的。可能需要20分钟左右,但之后你会对Android上的动画有更清晰的认识。

0

我无法找到问题,即在同一按钮上单击应用动画后,该按钮无法第二次点击。然而,developer.android表示:视图动画不会更改动画视图的边界,它只会动画化视图(外观),但在我看来,在按钮的情况下,它确实会更改边界。我已经在比例动画上进行了检查。我已经通过调用button1.setScaleX(1f);和button1.setScaleY(1f);重置我的按钮边界,并为动画创建了此方法:

private void propScaleDownAnim(Button btn, int startDly){
  btn.animate().scaleX(0f).scaleY(0f).setDuration(250).setStartDelay(startDly)
            .withEndAction(new Runnable() {
                @Override
                public void run() {
                    //animation ended
                    btn.setVisibility(View.INVISIBLE);
                    }
            }); }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接