android 好看的开屏动画代码
时间: 2023-09-02 07:13:08 浏览: 43
以下是一个简单的 Android 开屏动画代码,基于属性动画实现:
1. 在 res 目录下创建 anim 目录,然后新建一个名为 splash.xml 的动画文件:
```xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0" />
<scale
android:fromXScale="1.5"
android:fromYScale="1.5"
android:toXScale="1.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%" />
</set>
```
2. 在你的 SplashActivity 中使用属性动画播放该动画:
```java
public class SplashActivity extends AppCompatActivity {
private ImageView mSplashImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mSplashImage = findViewById(R.id.splash_image);
// 创建 Alpha 和 Scale 动画
AnimationSet animationSet = (AnimationSet) AnimationUtils.loadAnimation(this, R.anim.splash);
mSplashImage.startAnimation(animationSet);
// 动画结束后跳转到主界面
animationSet.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
@Override
public void onAnimationRepeat(Animation animation) {}
});
}
}
```
3. 在布局文件中添加 ImageView 控件:
```xml
<ImageView
android:id="@+id/splash_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/splash_image" />
```
这里的 `@drawable/splash_image` 是你自己定义的开屏图片资源。这样就可以实现一个简单的 Android 开屏动画了。