实现上述开机效果的动画,只需要三步。
1.Activity中的布局文件包含一个ImageView
2.通过welcome_anim.xml为ImageView设置动画效果的
3.ImageView启动动画效果mImageView.startAnimation(mAimation);
欢迎主界面文件:WelcomeActivity.java
通过Handler启动一个线程实现开机动画结束后,跳转到主activity。
public class WelcomeActivity extends Activity {
private static final String TAG ="LinkGame: WelcomeActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.d(TAG," onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
set2DAnim();
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
Log.d(TAG," run()");
Intent mIntent = new Intent(WelcomeActivity.this,MainActivity.class);
startActivity(mIntent);
WelcomeActivity.this.finish();
}
}, 4000);
}
public void set2DAnim(){
Log.d(TAG," set2DAnim()");
ImageView mImageView = (ImageView)findViewById(R.id.welcome_image);
final Animation mAimation = AnimationUtils.loadAnimation(this, R.anim.welcome_anim);
mImageView.startAnimation(mAimation);
}
}
welcome_anim.xml动画效果文件。
该文件在res/anim目录下。
通过一个set集合定义一组动作合集:透明度alpha,旋转rotate和缩放scale
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:duration="3000"
android:fillAfter="true">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"/>
<rotate
android:fromDegrees="0"
android:toDegrees="1080"
android:pivotX="50%"
android:pivotY="50%"/>
<scale
android:fromXScale="0.0"
android:toXScale="2.0"
android:fromYScale="0.0"
android:toYScale="2.0"
android:pivotX="50%"
android:pivotY="50%"/>
</set>
WelcomeActivity.java的布局文件:activity_welcome.xml
只包含一个ImageView组件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<ImageView
android:id="@+id/welcome_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
</LinearLayout>