[英]How to animate a button in Android to expand sideways?
I want a Button
(or any View
for that matter) to expand / stretch out sideways with an animation and I want it to happen at the click of another Button
.我想要一个
Button
(或任何与此相关的View
)通过动画横向扩展/伸展,我希望它在单击另一个Button
。 For example, before Button 1
is pressed, the Button 2
shouldn't exist.例如,在按下
Button 1
之前, Button 2
不应该存在。 But when Button 1
is pressed, the Button 2
should slowly come into existence with both its sides smoothly expanding to left and right to a certain size.但是当
Button 1
被按下时, Button 2
应该会慢慢出现,它的两侧会平滑地向左和向右扩展到一定的大小。
The following is a photo I drew to explain what I'm trying to achieve以下是我画的一张照片来解释我想要实现的目标
I have looked at several tutorials, but most of them had sliding animation from one fixed end to the other.我看过几个教程,但大多数都有从一个固定端到另一个固定端的滑动动画。 I have also tried scaling the X & Y of the button from value 0 to 1, but they just don't give the smooth animation effect (they just appear there out of nowhere) and the button doesn't expand / stretch sideways.
我还尝试将按钮的 X 和 Y 从值 0 缩放到 1,但它们只是没有提供平滑的动画效果(它们只是突然出现在那里)并且按钮不会向侧面展开/拉伸。
Any idea how this can be achieved?知道如何实现这一目标吗?
Thank you for your time!!谢谢你的时间!!
流畅的动画效果,可以使用ObjectAnimtor。
// scale down button2 at the start of an activity
btn2.setScaleX(0.1f);
// on the click of button 1 let the button2 scale
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btn2.animate()
.scaleX(4.0f)
.setDuration(1000)
.setInterpolator(new OvershootInterpolator(2.0f)) //Will give bounce effect to the scaling
.start();
}
});
first define 2 objects of Animation and Button then create a On Click listener for Button and write these codes in it:首先定义 Animation 和 Button 的 2 个对象,然后为 Button 创建一个 On Click 侦听器并在其中编写以下代码:
//here i define :Button btnoff //Animation animation1 //set on click listener in oncreate btnOff.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { animation1 = new ScaleAnimation(0,2,1,1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation1.setDuration(1200); btnOff.startAnimation(animation1);} });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.