开始Activity有动画黑屏
Start Activity with animation black screen
您好,我在打开和完成活动时从左到右和从右到左添加动画,但是当我放置 overridePendingIntent
它显示黑屏...
这是我的left_to_right.xml动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="-100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
这是right_to_left.xml动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="100%"
android:toYDelta="0%" />
</set>
我正在这样开始 Activity :
startActivity(Intent(this, SignInActivity::class.java))
overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left)
然后像这样完成:
finish()
overridePendingTransition(R.anim.right_to_left, R.anim.left_to_right)
但是做的时候黑屏...
这是我的主题
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="android:windowDisablePreview">true</item>
</style>
</resources>
我正在使用的活动
android:theme="@style/Theme.MaterialComponents.Light.NoActionBar"
我错过了什么?
第一个 ACTIVITY:
我使用 Button
:
用 TargetActivity
(第二个 Activity
)膨胀 MainActivity
来重建情况
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_in = findViewById(R.id.button_in);
button_in.setOnClickListener(v -> {
Intent intent = new Intent(this, TargetActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
});
}
}
activity_main.xml
包含第一个Button
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="MainActivity">
<Button
android:id="@+id/button_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20sp"
android:backgroundTint="#2962FF"
android:text="To TargetActivity" />
</RelativeLayout>
第二个ACTIVITY:
第二个 Activity
是 finish()
的 TargetActivity
,动画如下:
public class TargetActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_target);
Button button_out = findViewById(R.id.button_out);
button_out.setOnClickListener(v -> {
this.finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
});
}
}
还有另一个 Layout
第二个 Button
叫做 activity_target.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFAB00"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".TargetActivity">
<Button
android:id="@+id/button_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20sp"
android:textColor="#ffffff"
android:backgroundTint="#2962FF"
android:text="To MainActivity" />
</RelativeLayout>
4 动画:
您只需要 anim
文件夹中的 4 个动画。 2 表示从左到右,反之亦然。
1.) slide_in_left.xml
:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="-100%p"
android:toXDelta="0" />
</set>
2.) slide_out_left.xml
:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:toXDelta="-100%p" />
</set>
3.) slide_in_right.xml
:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="100%p"
android:toXDelta="0" />
</set>
4.) slide_out:right.xml
:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:toXDelta="100%p" />
</set>
最后不要忘记在 AndroidManifest.xml
中添加 TargetActivity
:
<activity android:name=".TargetActivity"/>
您好,我在打开和完成活动时从左到右和从右到左添加动画,但是当我放置 overridePendingIntent
它显示黑屏...
这是我的left_to_right.xml动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="-100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
这是right_to_left.xml动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="100%"
android:toYDelta="0%" />
</set>
我正在这样开始 Activity :
startActivity(Intent(this, SignInActivity::class.java))
overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left)
然后像这样完成:
finish()
overridePendingTransition(R.anim.right_to_left, R.anim.left_to_right)
但是做的时候黑屏...
这是我的主题
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="android:windowDisablePreview">true</item>
</style>
</resources>
我正在使用的活动
android:theme="@style/Theme.MaterialComponents.Light.NoActionBar"
我错过了什么?
第一个 ACTIVITY:
我使用 Button
:
TargetActivity
(第二个 Activity
)膨胀 MainActivity
来重建情况
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_in = findViewById(R.id.button_in);
button_in.setOnClickListener(v -> {
Intent intent = new Intent(this, TargetActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
});
}
}
activity_main.xml
包含第一个Button
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="MainActivity">
<Button
android:id="@+id/button_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20sp"
android:backgroundTint="#2962FF"
android:text="To TargetActivity" />
</RelativeLayout>
第二个ACTIVITY:
第二个 Activity
是 finish()
的 TargetActivity
,动画如下:
public class TargetActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_target);
Button button_out = findViewById(R.id.button_out);
button_out.setOnClickListener(v -> {
this.finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
});
}
}
还有另一个 Layout
第二个 Button
叫做 activity_target.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFAB00"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".TargetActivity">
<Button
android:id="@+id/button_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20sp"
android:textColor="#ffffff"
android:backgroundTint="#2962FF"
android:text="To MainActivity" />
</RelativeLayout>
4 动画:
您只需要 anim
文件夹中的 4 个动画。 2 表示从左到右,反之亦然。
1.) slide_in_left.xml
:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="-100%p"
android:toXDelta="0" />
</set>
2.) slide_out_left.xml
:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:toXDelta="-100%p" />
</set>
3.) slide_in_right.xml
:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="100%p"
android:toXDelta="0" />
</set>
4.) slide_out:right.xml
:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:toXDelta="100%p" />
</set>
最后不要忘记在 AndroidManifest.xml
中添加 TargetActivity
:
<activity android:name=".TargetActivity"/>