效果图:
主要原理:就是利用nineold提供的动画帮助类,做一个放大效果,子线程或者hanlder发消息延时两秒,然后再执行放大效果对图片
当然需要添加依赖
compile 'com.nineoldandroids:library:2.4.0'
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/default_splash"/>
</RelativeLayout>
代码:
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
splash = (ImageView) findViewById(R.id.splash);
//方法一:开启一个子线程执行跳转任务
new Thread(){
@Override
public void run() {
super.run();
try {
Thread.sleep(2000);
Splash.this.runOnUiThread(new Runnable() {
@Override
public void run() {
animateImage();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
//放大图片
public void animateImage() {
ObjectAnimator animatorX = ObjectAnimator.ofFloat(splash, "scaleX", 1f,
1.2f);
ObjectAnimator animatorY = ObjectAnimator.ofFloat(splash, "scaleY", 1f,
1.2f);
AnimatorSet set = new AnimatorSet();
set.setDuration(2000).play(animatorX).with(animatorY);
set.start();
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
startActivity(new Intent(Splash.this, MainActivity.class));
Splash.this.finish();
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
});
}
ok 本文结束,一个简单有趣的splash