基于RxJava实现酷炫启动页

基于RxJava实现酷炫启动页,第1张

概述前言RxJava在GitHub主页上的自我介绍是\"alibraryforcomposingasynchronousandevent-basedprogramsusingobservablesequencesfortheJavaVM\"(一个在JavaVM上使用可观测的序列来组成异步的、基于事件的程序的库)。这

前言

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

之前注意到Coding APP启动页很是酷炫,今天我们使用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",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开发有所帮助。

总结

以上是内存溢出为你收集整理的基于RxJava实现酷炫启动页全部内容,希望文章能够帮你解决基于RxJava实现酷炫启动页所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1148775.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存