RxJava實(shí)踐之打造酷炫啟動(dòng)頁
之前注意到coding APP啟動(dòng)頁很是酷炫,今天我們使用RxJava和屬性動(dòng)畫模仿實(shí)現(xiàn)其效果。
一、新建啟動(dòng)頁WelcomeActivity
注意,我們這里讓W(xué)elcomeActivity繼承Activity不要繼承AppCompatActivity,因?yàn)锳ppCompatActivity會(huì)默認(rèn)去加載主題,造成卡頓
- public class WelcomeActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_welcome);
- }
- }
二、定義引導(dǎo)頁布局activity_welcome.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/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上覆蓋一個(gè)View,該View用漸變色背景welcomimg_bg.xml以暗化圖片,welcomimg_bg.xml代碼如下:
- <?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表示結(jié)束顏色,angle=90 表示顏色從下往上漸變。
三、隨機(jī)選取圖片并使用RxJava啟動(dòng)動(dòng)畫
***我們的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() 從開機(jī)到現(xiàn)在的毫秒數(shù)(手機(jī)睡眠(sleep)的時(shí)間也包括在內(nèi))
- 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操作符,它的意思是延遲執(zhí)行某個(gè)操作,***個(gè)參數(shù)表示延遲時(shí)間,第二個(gè)參數(shù)是時(shí)間單位。
好了,就醬。
需要完整代碼可以戳這里代碼傳送門
- #RxJava Android啟動(dòng)頁