原文發佈於個人博客Android猿java
以前注意到coding APP啓動頁非常酷炫,今天咱們使用RxJava和屬性動畫模仿實現其效果。android
注意,咱們這裏讓WelcomeActivity繼承Activity不要繼承AppCompatActivity,由於AppCompatActivity會默認去加載主題,形成卡頓git
public class WelcomeActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); } }
很少說直接上代碼:github
<?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/iv_entry" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:src="@drawable/welcomimg1"/> <View android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/welcomimg_bg"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="100dp" android:gravity="center" android:text="xialong" android:textColor="@android:color/white" android:textSize="23sp"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/google_logo" android:layout_alignParentBottom="true" android:layout_marginBottom="60dp" android:layout_centerInParent="true" android:tint="@android:color/white" /> </RelativeLayout>
這裏咱們用了相對佈局,在ImageView上覆蓋一個View,該View用漸變色背景welcomimg_bg.xml以暗化圖片,welcomimg_bg.xml代碼以下:dom
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:angle="90" android:startColor="@color/black" android:endColor="@android:color/transparent" /> </shape>
其中startColor表示起始顏色,endColor表示結束顏色,angle=90 表示顏色從下往上漸變。ide
最後咱們的WelcomeActivity.java長這樣:佈局
public class WelcomeActivity extends Activity { @Bind(R.id.iv_entry) ImageView mIVEntry; private static final int ANIM_TIME = 2000; private static final float SCALE_END = 1.15F; private static final int[] Imgs={ R.drawable.welcomimg1,R.drawable.welcomimg2, R.drawable.welcomimg3,R.drawable.welcomimg4, R.drawable.welcomimg5, R.drawable.welcomimg6, R.drawable.welcomimg7,R.drawable.welcomimg8, R.drawable.welcomimg9,R.drawable.welcomimg10, R.drawable.welcomimg11,R.drawable.welcomimg12,}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); ButterKnife.bind(this); Random random = new Random(SystemClock.elapsedRealtime());//SystemClock.elapsedRealtime() 從開機到如今的毫秒數(手機睡眠(sleep)的時間也包括在內) mIVEntry.setImageResource(Imgs[random.nextInt(Imgs.length)]); Observable.timer(1000, TimeUnit.MILLISECONDS) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<Long>() { @Override public void call(Long aLong) { startAnim(); } }); } private void startAnim() { ObjectAnimator animatorX = ObjectAnimator.ofFloat(mIVEntry, "scaleX", 1f, SCALE_END); ObjectAnimator animatorY = ObjectAnimator.ofFloat(mIVEntry, "scaleY", 1f, SCALE_END); AnimatorSet set = new AnimatorSet(); set.setDuration(ANIM_TIME).play(animatorX).with(animatorY); set.start(); set.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { startActivity(new Intent(WelcomeActivity.this, MainActivity.class)); WelcomeActivity.this.finish(); } }); } }
這裏的RxJava使用了timer操做符,它的意思是延遲執行某個操做,第一個參數表示延遲時間,第二個參數是時間單位。
好了,就醬。動畫
須要完整代碼能夠戳這裏代碼傳送門this