RxJava实践之打造酷炫启动页

简介:

之前注意到coding APP启动页很是酷炫,今天我们使用RxJava和属性动画模仿实现其效果。

一、新建启动页WelcomeActivity

注意,我们这里让WelcomeActivity继承Activity不要继承AppCompatActivity,因为AppCompatActivity会默认去加载主题,造成卡顿

 
 
  1.  public class WelcomeActivity extends Activity { 
  2.  
  3.     @Override 
  4.     protected void onCreate(Bundle savedInstanceState) { 
  5.         super.onCreate(savedInstanceState); 
  6.         setContentView(R.layout.activity_welcome); 
  7.     } 

二、定义引导页布局activity_welcome.xml

不多说直接上代码:

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent"
  5.  
  6.     <ImageView 
  7.         android:id="@+id/iv_entry" 
  8.         android:layout_width="match_parent" 
  9.         android:layout_height="match_parent" 
  10.         android:scaleType="fitXY" 
  11.         android:src="@drawable/welcomimg1"/> 
  12.  
  13.     <View 
  14.         android:layout_width="match_parent" 
  15.         android:layout_height="match_parent" 
  16.         android:background="@drawable/welcomimg_bg"/> 
  17.  
  18.  
  19.     <TextView 
  20.         android:layout_width="match_parent" 
  21.         android:layout_height="wrap_content" 
  22.         android:layout_alignParentBottom="true" 
  23.         android:layout_marginBottom="100dp" 
  24.         android:gravity="center" 
  25.         android:text="xialong" 
  26.         android:textColor="@android:color/white" 
  27.         android:textSize="23sp"/> 
  28.  
  29.     <ImageView 
  30.         android:layout_width="wrap_content" 
  31.         android:layout_height="wrap_content" 
  32.         android:src="@mipmap/google_logo" 
  33.         android:layout_alignParentBottom="true" 
  34.         android:layout_marginBottom="60dp" 
  35.         android:layout_centerInParent="true" 
  36.         android:tint="@android:color/white" /> 
  37. </RelativeLayout> 

这里我们用了相对布局,在ImageView上覆盖一个View,该View用渐变色背景welcomimg_bg.xml以暗化图片,welcomimg_bg.xml代码如下:

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
  4.     <gradient 
  5.         android:angle="90" 
  6.         android:startColor="@color/black" 
  7.         android:endColor="@android:color/transparent" 
  8.         /> 
  9.  
  10. </shape> 

其中startColor表示起始颜色,endColor表示结束颜色,angle=90 表示颜色从下往上渐变。

三、随机选取图片并使用RxJava启动动画

最后我们的WelcomeActivity.java长这样:

 
 
  1. public class WelcomeActivity extends Activity { 
  2.  
  3.     @Bind(R.id.iv_entry) 
  4.     ImageView mIVEntry; 
  5.  
  6.     private static final int ANIM_TIME = 2000; 
  7.  
  8.     private static final float SCALE_END = 1.15F; 
  9.  
  10.     private static final int[] Imgs={ 
  11.             R.drawable.welcomimg1,R.drawable.welcomimg2, 
  12.             R.drawable.welcomimg3,R.drawable.welcomimg4, 
  13.             R.drawable.welcomimg5, R.drawable.welcomimg6, 
  14.             R.drawable.welcomimg7,R.drawable.welcomimg8, 
  15.             R.drawable.welcomimg9,R.drawable.welcomimg10, 
  16.             R.drawable.welcomimg11,R.drawable.welcomimg12,}; 
  17.  
  18.     @Override 
  19.     protected void onCreate(Bundle savedInstanceState) { 
  20.         super.onCreate(savedInstanceState); 
  21.         setContentView(R.layout.activity_welcome); 
  22.         ButterKnife.bind(this); 
  23.  
  24.         Random random = new Random(SystemClock.elapsedRealtime());//SystemClock.elapsedRealtime() 从开机到现在的毫秒数(手机睡眠(sleep)的时间也包括在内) 
  25.         mIVEntry.setImageResource(Imgs[random.nextInt(Imgs.length)]); 
  26.  
  27.         Observable.timer(1000, TimeUnit.MILLISECONDS) 
  28.                 .observeOn(AndroidSchedulers.mainThread()) 
  29.                 .subscribe(new Action1<Long>() 
  30.                 { 
  31.  
  32.                     @Override 
  33.                     public void call(Long aLong) 
  34.                     { 
  35.                         startAnim(); 
  36.                     } 
  37.                 }); 
  38.     } 
  39.  
  40.  
  41.     private void startAnim() { 
  42.  
  43.         ObjectAnimator animatorX = ObjectAnimator.ofFloat(mIVEntry, "scaleX", 1f, SCALE_END); 
  44.         ObjectAnimator animatorY = ObjectAnimator.ofFloat(mIVEntry, "scaleY", 1f, SCALE_END); 
  45.  
  46.         AnimatorSet set = new AnimatorSet(); 
  47.         set.setDuration(ANIM_TIME).play(animatorX).with(animatorY); 
  48.         set.start(); 
  49.  
  50.         set.addListener(new AnimatorListenerAdapter() 
  51.         { 
  52.  
  53.             @Override 
  54.             public void onAnimationEnd(Animator animation) 
  55.             { 
  56.  
  57.                 startActivity(new Intent(WelcomeActivity.this, MainActivity.class)); 
  58.                 WelcomeActivity.this.finish(); 
  59.             } 
  60.         }); 
  61.     } 

这里的RxJava使用了timer操作符,它的意思是延迟执行某个操作,第一个参数表示延迟时间,第二个参数是时间单位。

好了,就酱。

需要完整代码可以戳这里代码传送门

 
 
  1. #RxJava Android启动页 





作者:xialong
来源:51CTO
目录
相关文章
|
4月前
|
XML Java Android开发
Android App开发手机阅读中实现平滑翻书效果和卷曲翻书动画实战(附源码 简单易懂 可直接使用)
Android App开发手机阅读中实现平滑翻书效果和卷曲翻书动画实战(附源码 简单易懂 可直接使用)
70 0
|
4月前
|
XML 前端开发 Java
Android App开发动画特效中遮罩动画的讲解及实战演示(附源码 简单易懂 可直接使用)
Android App开发动画特效中遮罩动画的讲解及实战演示(附源码 简单易懂 可直接使用)
53 0
|
4月前
|
XML Java Android开发
Android App开发中补间动画的讲解以及实现钟摆动画效果实战(附源码 简单易懂 可直接使用)
Android App开发中补间动画的讲解以及实现钟摆动画效果实战(附源码 简单易懂 可直接使用)
29 0
Android App开发中补间动画的讲解以及实现钟摆动画效果实战(附源码 简单易懂 可直接使用)
|
4月前
|
XML Java Android开发
Android Studio App开发中多线程的讲解与实现新闻轮播滚动实战(附源码 超详细必看)
Android Studio App开发中多线程的讲解与实现新闻轮播滚动实战(附源码 超详细必看)
30 0
|
4月前
|
XML Java Android开发
Android App开发动画特效中帧动画和电影淡入淡出动画的讲解及实战(附源码和演示视频 简单易懂)
Android App开发动画特效中帧动画和电影淡入淡出动画的讲解及实战(附源码和演示视频 简单易懂)
44 0
|
4月前
|
XML Java Android开发
Android App开发手机阅读中贝塞尔曲线的原理讲解及实现波浪起伏动画实战(附源码和演示视频 可直接使用)
Android App开发手机阅读中贝塞尔曲线的原理讲解及实现波浪起伏动画实战(附源码和演示视频 可直接使用)
45 0
|
9月前
|
存储 设计模式 XML
QMUI实战(四)— QMUI 换肤的实现
QMUI 2 一个最大的特色就是支持了换肤(夜间模式),今天就来聊聊 QMUI 换肤的使用与实现。
673 0
|
10月前
|
前端开发
前端换肤,聊一聊主题切换那些事
前端换肤,聊一聊主题切换那些事
148 0
女朋友想要听歌,我反手用Flutter做了2个音乐播放器,给她拿捏了🎧
有很多小伙伴和我说,网上关于Flutter的音乐播放器资料太少了,我反手掉了10根头发给他们做了这样的音乐播放器,你就说得不得劲吧😎
|
JSON iOS开发 数据格式
iOS开发 - 关于启动页动画的杂谈
iOS开发 - 关于启动页动画的杂谈
211 0
iOS开发 - 关于启动页动画的杂谈