gpt4 book ai didi

android - 淡出当前 Activity 的 View

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:00:42 25 4
gpt4 key购买 nike

我希望我的 Activity View 在单击按钮时淡出。我将动画代码放在按钮的 OnClickListener() 中,但没有发生淡出。那么知道如何在单击按钮时淡出当前 Activity 的 View 吗?提前致谢......

实际上,我的目的是,在我的应用程序中,当 Activity 开始时,该 Activity 的 View 将淡入,当 Activity 结束时,它的 View 将淡出,然后新 Activity 的 View 将淡入。下面,我是给我的代码,请需要帮助来找出那里的问题.....

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);

}
});

}

}

最佳答案

在您的 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);
}
});

并且您的布局 xml 应该以 id="@+id/main"的 View 开头,并包含一个 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>

关于android - 淡出当前 Activity 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6715746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com