【问题标题】:Animating button presses / clicks in AndroidAndroid中的动画按钮按下/点击
【发布时间】:2014-10-15 09:13:54
【问题描述】:

我正在尝试创建一个按钮单击动画,例如当你按下按钮时,按钮会缩小一点,当你松开时,它会重新放大。如果您只需轻按,您就可以将新闻稿和新闻稿串在一起。

我设置了一个 onTouchListener 和几个 XML 定义的 AnimatorSet,一个用于新闻,一个用于发布。在ACTION_DOWN 上发布新闻,ACTION_UPACTION_CANCEL 上发布。当您按住按钮时,这工作正常,然后稍后释放。但是快速点击,释放动画会在按下完成之前触发,结果通常是根本没有动画。

我希望我可以使用 AnimatorSet 的顺序功能将发布动画粘贴到可能已经运行的新闻动画的末尾,但没有运气。我确信我可以用回调来装点东西,但这看起来很乱。

这里最好的方法是什么?谢谢!

【问题讨论】:

    标签: android animation user-interface


    【解决方案1】:

    我构建了一个示例 Button 类,用于缓冲动画并根据您的需要在队列中执行它们:

    public class AnimationButton extends Button 
    {
        private List<Animation> mAnimationBuffer = new ArrayList<Animation>();;
        private boolean mIsAnimating;
    
        public AnimationButton(Context context) 
        {
            super(context);
    
        }
    
        public AnimationButton(Context context, AttributeSet attrs) 
        {
            super(context, attrs);
    
        }
    
        public AnimationButton(Context context, AttributeSet attrs, int defStyle) 
        {
            super(context, attrs, defStyle);
    
        }
    
    
        @Override
        public boolean onTouchEvent(MotionEvent event) 
        {
            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                generateAnimation(1, 0.75f);
                triggerNextAnimation();
            }
            else if (event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP)
            {
                generateAnimation(0.75f, 1);
                triggerNextAnimation();
            }
    
            return super.onTouchEvent(event);
        }
    
        private void generateAnimation(float from, float to)
        {
            ScaleAnimation scaleAnimation = new ScaleAnimation(from, to, from, to);
            scaleAnimation.setFillAfter(true);
            scaleAnimation.setDuration(500);
            scaleAnimation.setAnimationListener(new ScaleAnimation.AnimationListener() 
            {       
                @Override
                public void onAnimationStart(Animation animation) { }
    
                @Override
                public void onAnimationRepeat(Animation animation) { }
    
                @Override
                public void onAnimationEnd(Animation animation) 
                {               
                    mIsAnimating = false;
                    triggerNextAnimation();
                }
            });
    
            mAnimationBuffer.add(scaleAnimation);
        }
    
        private void triggerNextAnimation()
        {
            if (mAnimationBuffer.size() > 0 && !mIsAnimating)
            {
                mIsAnimating = true;
                Animation currAnimation = mAnimationBuffer.get(0);
                mAnimationBuffer.remove(0);
    
                startAnimation(currAnimation);
            }
        }
    
    }
    

    希望这会有所帮助:)

    【讨论】:

    • 是的,这是我正在考虑的那种手动实现......但希望有更简洁和内置的东西。
    【解决方案2】:

    看起来 L Preview 添加了为 xml 文件中的状态更改指定动画的功能。

    https://developer.android.com/preview/material/animations.html#viewstate

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 2017-05-18
      • 2019-12-16
      • 2017-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多