RxJava实践之打造酷炫启动页

移动开发 移动应用
WelcomeActivity继承Activity不要继承AppCompatActivity,因为AppCompatActivity会默认去加载主题,造成卡顿。

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

[[170939]]

一、新建启动页WelcomeActivity

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

 public class WelcomeActivity extends Activity { 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_welcome); 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

二、定义引导页布局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> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.

这里我们用了相对布局,在ImageView上覆盖一个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> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

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

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

***我们的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(); 
            } 
        }); 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.

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

好了,就酱。

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

#RxJava Android启动页 
  • 1.
责任编辑:武晓燕 来源: Android猿博客
相关推荐

2009-08-08 08:53:49

Windows 7Direct 11图形图象

2020-01-18 15:02:48

技术研发指标

2023-01-31 10:23:00

CSS倒影效果

2015-11-23 17:19:12

中国外包网

2015-10-20 15:58:28

弹力菜单android源码

2017-08-18 10:31:12

PC技术分体式

2020-01-03 10:50:16

Python编程语言Mac电脑

2023-04-26 15:27:11

JavaScript技巧元素

2022-02-11 16:01:14

C语言技巧命令

2014-09-01 15:49:18

智能穿戴智能设备可穿戴设备

2009-06-04 15:48:11

SUSE Linux解密

2017-05-02 09:55:02

2017-07-20 13:11:26

互联网

2019-08-01 09:17:06

工具插件开发

2013-05-02 09:40:20

2012-04-20 12:42:21

2011-09-15 13:25:18

IOS应用

2019-07-12 09:18:22

IntelliJ ID插件插件库

2020-04-10 21:33:10

物联网大数据物联网工厂

2015-11-19 00:51:02

音乐平台
点赞
收藏

51CTO技术栈公众号