在Android中动画按钮的按下/点击 [英] Animating button presses / clicks in Android
问题描述
我正在尝试创建按钮点击动画,例如按下该按钮时会稍微缩小,释放时会放大。
I'm trying to create a button-click animation, e.g. the button scales down a little when you press it, scales back up when you release. If you simply tap, you get the press and release strung together.
我设置了一个onTouchListener和两个XML定义的AnimatorSet,一个用于媒体,一个用于媒体。释放。在 ACTION_DOWN
上运行新闻,在 ACTION_UP
或 ACTION_CANCEL
。当您按住按钮,然后稍后松开时,此方法可以正常工作。但是只要快速点击一下,释放动画就会在完成新闻之前触发,并且通常结果根本是没有动画。
I set up an onTouchListener and a couple XML-defined AnimatorSets, one for the press and one for the release. Ran the press on ACTION_DOWN
, the release on ACTION_UP
or ACTION_CANCEL
. This works fine when you press and hold the button, then release a little later. But with a quick tap, the release animation triggers before the press one is done, and often the result is no animation at all.
我希望我可以使用AnimatorSet的将发布动画粘贴到可能已经运行的新闻动画的末尾的连续功能,但是没有运气。我敢肯定我可以通过回调来组装一些东西,但这似乎很麻烦。
I'd hoped I could use AnimatorSet's sequential capabilities to stick the release animation onto the end of the possibly-already-running press animation, but no luck. I'm sure I could rig something up with callbacks, but that seems messy.
这里最好的方法是什么?
What's the best approach here? Thanks!
推荐答案
我建立了一个示例Button类,该类可以缓冲动画并根据需要在队列中执行动画:
I built a sample Button class that buffers animations and executes them in a queue according to what you need:
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);
}
}
}
希望如此帮助:)
这篇关于在Android中动画按钮的按下/点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!