【问题标题】:Zoomout animation on button press in android在android中按下按钮时的缩小动画
【发布时间】:2015-03-09 08:05:17
【问题描述】:

当按下按钮时,我需要为该按钮设置动画....按钮在按下时会缩小一点,在释放时会缩小成原始大小..(非常像在火种中)

我用这个:

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

<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="0.9"
    android:toYScale="0.9" >
</scale>

但它不能根据我的需要工作,我需要在按下按钮之前保持小按钮。并在释放后恢复正常。

【问题讨论】:

  • 您需要两张图片,一张(大)和第二张(小),点击后您可以将小图片设置为背景。或者,如果您想在发布时显示旧图像,请使用 selector
  • 有没有办法使用动画来做到这一点..?不使用 2 张图片..
  • @GaganDeep - 你能分享你的解决方案吗?
  • @AtulOHolic 下面的回答对我有用,试试这个..

标签: android xml animation android-animation


【解决方案1】:

您可以像这样创建第二个动画以返回未点击状态:

<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fillAfter="true" 
    android:fromXScale="0.9"
    android:fromYScale="0.9"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.0"
    android:toYScale="1.0" >
</scale>

然后:

yourButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
          // start your first zoom out Animation here
                    break;

                case MotionEvent.ACTION_UP:
         // start zoom in animation which returns to original state
                 break;
            }
            return false;
        }
    });

不要忘记将动画中的 fillAfter 设置为 true,否则它会在持续时间结束后恢复到原始状态。 或者,如果您不想要第二个动画,您可以调用

yourButton.startAnimation(yourAnimation);

在 MotionEvent.ACTION_DOWN 内部 然后调用

yourButton.clearAnimation();

在 MotionEvent.ACTION_UP 中

【讨论】:

  • 它只会在特定时间缩小..但我需要它在发布之前保持小..
  • 您是否将 fillAfter 设置为 true?
  • 我们为什么不直接使用 View#setScaleX 和 View#setScaleY?
猜你喜欢
  • 2017-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-09
相关资源
最近更新 更多