Objective
Rxjava's self-introduction on the GitHub homepage is "A library for composing asynchronous and event-based the using programs observable for th e java VM (a library that uses an observable sequence on a Java VM to compose an asynchronous, event-based program). This is Rxjava, which is very precise.
Before noticing the Coding app startup page is cool, today we use Rxjava and attribute animation to achieve its effect.
Let's see the effect first.
First, new startup page welcomeactivity
Note that we are here to let WelcomeActivity
inheritance Activity
not inherit AppCompatActivity
, because AppCompatActivity
the default goes to load the theme, causing the cotton
public class Welcomeactivity extends activity {
@Override
protected void onCreate (Bundle savedinstancestate) { C10/>super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_welcome);
}
Second, define the Guide page layout activity_welcome.xml
Not much to say directly on the code:
<?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= "fi" Txy "android:src=" @drawable/welcomimg1 "/> <view android:layout_width=" Match_parent "android:layout_he ight= "Match_parent" android:background= "@drawable/welcomimg_bg"/> <textview android:layout_width= "Match_ Parent "android:layout_height=" Wrap_content "android:layout_alignparentbottom=" true "Android:layout_marginbott Om= "100DP" android:gravity= "center" android:text= "Xialong" android:textcolor= "@android: Color/white" Androi
D: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>
Here we use a relative layout, ImageView
covering a view, which darkens the picture with a gradient background welcomimg_bg.xml
.
The welcomimg_bg.xml code is as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <shape xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
<gradient
android:angle="
android:startcolor= "@color/black"
android: Endcolor= "@android: Color/transparent"
/>
</shape>
Where StartColor represents the starting color,EndColor represents the ending color, andangle=90 indicates that the color is gradient from bottom to top.
Third, randomly select the picture and use Rxjava to start the animation
At last our WelcomeActivity.java
long is like this:
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.DRAWABL
E.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 () Number of milliseconds from power-on to current (sleep)
The time is included) Miventry.setimageresource (Imgs[random.nextint (imgs.length))); Observable.timer (1000, timeunit.milliseconds). Observeon (AndroidscheduleRs.mainthread ()). Subscribe (New action1<long> () {@Override public void call (Lo
Ng 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 ();
}
}); }
}
The Rxjava here uses the timer operator, which means delaying an operation, the first parameter represents the delay time, and the second parameter is the time unit.
All right, just the sauce. The above is to use Rxjava to create a cool start page of the full content, I hope this article on the development of Android to help you.