基于RxJava实现酷炫启动页

作者:简书作者 时间:2023-09-26 21:50:47 

前言

RxJava 在 GitHub 主页上的自我介绍是 "a library for composing asynchronous and event-based programs using observable sequences for the Java VM"(一个在 Java VM 上使用可观测的序列来组成异步的、基于事件的程序的库)。这就是 RxJava ,概括得非常精准。

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

先来看看效果

基于RxJava实现酷炫启动页

一、新建启动页WelcomeActivity

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


 public class WelcomeActivity extends Activity {

@Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_welcome);
 }
}

二、定义引导页布局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上覆盖一个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表示结束颜色,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();
     }
   });
 }
}

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

好了,就酱。以上就是用RxJava打造酷炫启动页的全部内容,希望本文对大家学习Android开发有所帮助。

来源:http://www.jianshu.com/p/d36ebec6edab

标签:rxjava
0
投稿

猜你喜欢

  • android BitmapFactory.Options使用方法详解

    2023-05-04 08:50:20
  • 在C#中新手易犯的典型缺陷

    2023-08-26 01:24:20
  • 关于C#理解装箱与拆箱

    2023-06-18 21:07:50
  • Android RadioButton单选框的使用方法

    2021-10-02 14:37:20
  • Android实现图像灰度化、线性灰度变化和二值化处理方法

    2021-10-17 15:49:10
  • IDEA 端口占用的解决方法(推荐)

    2023-09-05 06:05:15
  • Android Studio实现音乐播放器的全过程(简单易上手)

    2023-07-10 08:02:43
  • Android中执行java命令的方法及java代码执行并解析shell命令

    2022-08-27 15:45:13
  • android 获取视频第一帧作为缩略图的方法

    2022-11-22 19:30:25
  • C#中Foreach循环遍历的本质与枚举器详解

    2022-08-04 05:31:12
  • 浅谈利用Session防止表单重复提交

    2022-02-08 00:45:42
  • DataGridView控件显示行号的正确代码及分析

    2022-07-26 22:05:50
  • android studio 使用Mocklocation虚拟定位

    2022-12-31 12:26:34
  • C#泛型详解及关键字作用

    2023-04-07 20:23:12
  • 深入理解C#序列化与反序列化的详解

    2022-06-23 05:11:58
  • SpringBoot使用AOP+注解实现简单的权限验证的方法

    2022-07-29 00:59:09
  • Java基础将Bean属性值放入Map中的实例

    2023-10-11 13:57:40
  • C#实现对Json字符串处理实例

    2023-06-21 08:26:24
  • Spring Boot中如何使用断路器详解

    2022-03-03 06:34:49
  • Android简单获取经纬度的方法

    2021-07-28 05:26:47
  • asp之家 软件编程 m.aspxhome.com