【问题标题】:Android: button doesn't work after translate animationAndroid:翻译动画后按钮不起作用
【发布时间】:2015-08-05 02:41:15
【问题描述】:

我创建了一个按钮,只要按下它就可以向下翻译。我在 anim 文件夹中创建了一个 xml 文件来设置向下平移。

翻译有效,但动画完成后按钮恢复到其原始位置。为了解决这个问题,我使用了代码:

translationDown.setFillAfter(true);
translationDown.setFillEnabled(true);

虽然按钮在完成平移动画后停留在其位置,但它停止工作并且在再次按下时不会从该位置向下平移。

如何让按钮每次按下时向下平移?

编辑:

根据要求,这里是翻译的代码,存储在translate_down.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:fromYDelta="0"
        android:toYDelta="300"
        android:duration="250"
        />

</set>

这是我的 java 文件中的代码:

final Animation slideDown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate_down);
relativeLayoutButtons.startAnimation(slideDown);

【问题讨论】:

  • 发布您的翻译代码。您必须在动画完成后使用 view.setTranslationX() 和 view.setTranslationY()
  • 嗨,Harish,我为我的翻译动画添加了代码
  • 检查 ytRino 的回答。但它应该只适用于 API 11+。

标签: android animation button


【解决方案1】:

这是因为动画使用了您的视图(即您的按钮)的表示(位图副本)。当动画正在进行时,位图副本不是你的视图正在被翻译。动画完成后,你的实际视图并没有移动。

使用 translationDown.setFillAfter(true) 和 translationDown.setFillEnabled(true) 将无济于事,因为它们用于动画链接,即如果您想使用另一个动画对象继续动画。

您需要做的是在动画之后设置视图的位置属性。这是一个如何实现的示例:

final Animation slideDown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate_down);
relativeLayoutButtons.startAnimation(slideDown);


relativeLayoutButtons.getAnimation().setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {

                            // Set the position properties of your view here.
                            // e.g.
                            relativeLayoutButtons.setTranslationX(50);
                            relativeLayoutButtons.setTranslationY(50);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });

【讨论】:

    【解决方案2】:

    好吧,我敢打赌你会使用ontouch() 而不是onClick()。一旦你改变了Button的位置,我认为它只能在原来的位置上点击。

    试试下面的方法。

    Button bt = (Button) findViewById(R.id.button);
    
    bt.setOnTouchListener(new View.OnTouchListener() {
    
    @Override
    public boolean onTouch(View v, MotionEvent event){
            switch(event.getAction())
            {
                case MotionEvent.ACTION_UP :
                {
                    // Do whatever you want here.
                }
            return true;
    }});
    

    【讨论】:

      【解决方案3】:

      旧的动画 API 只是移动视图的绘图,而不是移动实际的视图位置。 所以,按下翻译视图没有响应(视图实际上仍然在原始位置。)

      使用新的 Animator API。此 API 移动实际视图位置。

      view.animate().translationY(300).setDuration(250).start();
      

      介绍见this google's blog post

      【讨论】:

        猜你喜欢
        • 2012-10-30
        • 2014-05-02
        • 2015-08-26
        • 1970-01-01
        • 2018-09-06
        • 2016-04-14
        • 2018-09-28
        • 1970-01-01
        • 2019-10-16
        相关资源
        最近更新 更多