溫馨提示×

Android開場動畫代碼怎么寫

小億
107
2023-08-17 17:21:00
欄目: 編程語言

Android開場動畫可以通過使用Android的動畫框架來實現。以下是一個例子,展示如何在Android開場動畫中淡入一個ImageView:

  1. 首先,在布局文件中添加一個ImageView并設置相應的屬性:
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/logo"
android:scaleType="centerCrop"
android:visibility="invisible" />
  1. 在Activity的onCreate()方法中獲取ImageView的引用,并創建一個AlphaAnimation對象:
ImageView imageView = findViewById(R.id.imageView);
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(1000);  // 設置動畫持續時間,單位為毫秒
  1. 在Activity的onWindowFocusChanged()方法中開始動畫,并設置ImageView可見:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
imageView.setVisibility(View.VISIBLE);
imageView.startAnimation(animation);
}
}

這樣,當Activity獲得焦點時,ImageView會以淡入的效果顯示出來。你可以根據需要調整動畫的屬性和效果,例如縮放、旋轉等。請注意,這只是一個簡單的開場動畫示例,你可以根據自己的需求進行修改和擴展。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女