问题描述
我写了一个小活动,可以在两个视图之间切换.现在,我尝试添加一些动画(淡入/淡出效果).有人可以解释一下该怎么做吗?
I've wrote a small activity that is able to switch between two views. Now I am trying to add some animation (fade-in/fade-out effect). Can anybody explain me how to do that right?
我自己尝试执行此操作的确有点问题(如果我将非常快地单击按钮,则我的应用程序将冻结).我使用下面列出的代码:
My own attempt to do this works kinda buggy (if I will click buttons very fast, my application freezes). I use code listed below:
public class WelcomeActivity extends Activity {
private boolean isLogin = false;
private String KEY_IS_LOGIN = "KEY_IS_LOGIN";
private Animation anim_fadein;
private RelativeLayout welcome, login;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
welcome = (RelativeLayout)getLayoutInflater().inflate(R.layout.activity_welcome_menu, null);
login = (RelativeLayout)getLayoutInflater().inflate(R.layout.activity_welcome_login, null);
anim_fadein = AnimationUtils.loadAnimation(this, R.anim.anim_fadein);
if (savedInstanceState != null)
isLogin = savedInstanceState.getBoolean(KEY_IS_LOGIN, false);
if (isLogin)
setContentView(login);
else
setContentView(welcome);
}
@Override
public void onBackPressed() {
if (isLogin) {
setContentView(welcome);
welcome.startAnimation(anim_fadein);
isLogin = false;
} else {
super.onBackPressed();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putBoolean(KEY_IS_LOGIN, isLogin);
super.onSaveInstanceState(outState);
}
public void onButton1Click(View v) {
setContentView(login);
login.startAnimation(anim_fadein);
}
public void onButtonLoginClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
public void onButtonBackClick(View v) {
setContentView(welcome);
welcome.startAnimation(anim_fadein);
}
动画XML文件:
<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="800" />
提前谢谢!
推荐答案
我过去完成此操作的方法是使用 ViewFlipper 类,并利用程序包提供的内置动画功能.
The way I have done this in the past is by using the ViewFlipper class and utilizing the built-in animation functions that the package provides.
这里是有关如何执行此操作的示例;以我的经验,过渡非常顺利:
Here is an example on how to do this; in my experience the transitions have been very smooth:
<LinearLayout
//Ommitted...
<ViewFlipper
android:id="@+id/[your_id_here]"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
<!--Your first layout XML here...-->
</RelativeLayout>
<RelativeLayout
<!--Your second layout XML here...-->
</RelativeLayout>
</ViewFlipper>
</LinearLayout>
请注意,您不必使用相对布局,为了清楚起见,我只是使用它们.
Please note that you do not have to use relative layouts, I simply used them for the sake of clarity.
在您的活动中获取对 ViewFlipper
的引用:
Get a reference to the ViewFlipper
in your activity:
ViewFlipper v = (ViewFlipper) findViewById(R.id.[your_id]);
根据需要设置动画:
v.setInAnimation(AnimationUtils.loadAnimation([your_activity_name].this, R.anim.[your_in_animation here]));
v.setOutAnimation(AnimationUtils.loadAnimation([your_activity_name].this, R.anim.[your_out_animation here]));
请注意,您可以在以下目录的Android类文件中找到一些非常好的预制动画:
Please note that you can find some really good prebuilt animations in the Android class files located in the following directory:
[android-sdks]/samples/android-[VERSION_NUMBER_HERE]/ApiDemos/res/anim
如果可以的话,我强烈建议您使用它们-这样可以节省很多时间.
I highly recommend using these if you can - it will save you much time.
现在,如果要在视图之间切换,请使用以下命令:
Now, if you wish to switch between the views, use the following commands:
v.showNext();
v.showPrevious();
您可能必须稍稍更改动画文件,以确保动画正确过渡(即,使右和向左淡出动画).
You might have to change the animation files slightly to make sure the animations transition properly (i.e. make a fade right and left animation).
希望这会有所帮助!
这篇关于使用淡入淡出动画在视图之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!