我希望我的活动视图在点击按钮时淡出。我将动画代码放在按钮的OnClickListener()中,但淡出没有发生。所以任何想法如何在点击按钮时设法淡出当前活动的视图?在此先感谢.....
实际上,我的目的是,在我的应用程序中,当一项活动开始时,该活动的视图将淡入,当活动结束时,它的视图将淡出,然后新活动的视图将淡入。下面,我给我的代码,请帮助找出那里的问题.....
public class First extends Activity {
Animation slide;
View view;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = (View) findViewById(android.R.id.content);
slide = AnimationUtils.loadAnimation(this, R.anim.fade_in);
view.startAnimation(slide);
Button btn = (Button) findViewById(R.id.retour);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),Second.class);
slide = AnimationUtils.loadAnimation(First.this, R.anim.fade_out);
view.startAnimation(slide);
Test.group.replaceContentView("Second", intent, 0);
}
});
}
}
答案 0 :(得分:8)
在onCreate()方法上添加如下内容:
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final View l = findViewById(R.id.main);
Animation a = AnimationUtils.loadAnimation(
YourActivity.this, android.R.anim.fade_out);
a.setDuration(200);
a.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
// Do what ever you need, if not remove it.
}
public void onAnimationRepeat(Animation animation) {
// Do what ever you need, if not remove it.
}
public void onAnimationStart(Animation animation) {
// Do what ever you need, if not remove it.
}
});
l.startAnimation(a);
}
});
并且你的Layout xml应该以一个id =“@ + id / main”的视图开头,并包含一个id =“@ + id / button”的按钮
示例:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/main"
android:drawingCacheQuality="high"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
.....
<Button
android:id="@+id/button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fade out">
</Button>
答案 1 :(得分:1)
请试试下面的代码。
Button btn = (Button) findViewById(R.id.retour);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),Second.class);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
}
});
可能对您有所帮助。