在Android中单击按钮后使布局显示动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21261583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make a layout appear with animation after button click in Android
提问by Falling Into Infinity
I'm trying to implement a simple animation effect for login screen.
我正在尝试为登录屏幕实现一个简单的动画效果。
Here's the scenario,
这是场景,
1:Some text and a login button will be displayed by default.
1:默认会显示一些文本和登录按钮。
2:After clicking the login button a new frame layout will appear from bottom to top. This layout will prompt user to enter their username and password.
2:点击登录按钮后,会从下到上出现一个新的框架布局。此布局将提示用户输入其用户名和密码。
I can make an animation which will overlay from one parent to another. In this scenario, I'm looking for an animation that will appear without leaving the activity.
我可以制作一个从一个父级到另一个父级叠加的动画。在这种情况下,我正在寻找无需离开活动即可出现的动画。
回答by Amir
First set invisible
to your layout.
Set animation to slideUp and slideDown.
首先设置invisible
为您的布局。
将动画设置为向上滑动和向下滑动。
final LinearLayout layout = (LinearLayout)findViewById(R.id.yourlayout);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
Animation slideUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
Animation slideDown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);
if(layout.getVisibility()==View.INVISIBLE){
layout.startAnimation(slideUp);
layout.setVisibility(View.VISIBLE);
}
});
slide_up.xml(create in res/anim
directory)
slide_up.xml(在res/anim
目录中创建)
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:fromYDelta="500" android:duration="500"/>
</set>
slide_down.xml
幻灯片文件.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:fromYDelta="0" android:toYDelta="500" android:duration="500"/>
</set>
Note:You should edit values in slide_down.xml
and slide_up.xml
until get favorable result.
for example:change android:fromYDelta="500"
to android:fromYDelta="700"
注意:您应该在slide_down.xml
和 中编辑值,slide_up.xml
直到获得满意的结果。
例如:更改android:fromYDelta="500"
为android:fromYDelta="700"
回答by Bri6ko
Check out this tutorial, it explains some basic animations which you can customize for your need.
查看本教程,它解释了一些您可以根据需要自定义的基本动画。
http://www.androidhive.info/2013/06/android-working-with-xml-animations/
http://www.androidhive.info/2013/06/android-working-with-xml-animations/