android 好看的开屏动画代码
时间: 2023-09-02 11:13:09 浏览: 35
以下是一个简单的 Android 开屏动画代码:
1. 创建 activity_splash.xml,添加一个 ImageView 控件,设置动画效果:
```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">
<ImageView
android:id="@+id/img_splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/splash_image" />
</RelativeLayout>
```
2. 在 SplashActivity.java 中设置动画:
```java
public class SplashActivity extends AppCompatActivity {
private static final int ANIMATION_DURATION = 2000;
private ImageView imgSplash;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
imgSplash = findViewById(R.id.img_splash);
// 设置动画
Animation anim = AnimationUtils.loadAnimation(this, R.anim.splash_animation);
imgSplash.startAnimation(anim);
// 跳转到主界面
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}, ANIMATION_DURATION);
}
}
```
3. 创建 res/anim/splash_animation.xml,设置动画效果:
```xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000" />
```
这个动画会让图片从不透明到透明,持续 2 秒钟。你可以根据需要调整动画效果。