一、动画基础知识
1、动画种类: Android动画有四大类,分别是AlphaAnimation、ScaleAnimation、TranslateAnimation和RotateAnimation,功能对照如下:
AlphaAnimation 渐变透明动画效果 ScaleAnimation 渐变缩放动画效果 TranslateAnimation 画面移动动画效果 RotateAnimation 画面旋转动画效果
2 、动画实现方式:动画实现方式有两种,分别是静态设置(xml)和动态设置(java代码)
3、动画模式:动画模式有两种,一种是渐变模式(AlphaAnimation、ScaleAnimation),一种是画面模式(TranslateAnimation、RotateAnimation)。
二、动画实现
实现步骤:
- 定义动画
- 设置动画属性,如时长、起始和终止
- 播放动画
1. xml实现方式:在res目录下新建anim目录-->新建动画xml文件-->定义属性,如下:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.3"
android:toXScale="1.2"
android:fromYScale="0.3"
android:toYScale="1.2"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"
android:fillAfter="true">
</scale>
然后在相应activity下编辑如下代码:
Animation anim = AnimationUtils.loadAnimation(this, R.anim.scale_animation);
openWeb.startAnimation(anim);
2. 动态实现:
ScaleAnimation sa = new ScaleAnimation(0.3f, 1.2f, 0.3f, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF,(float) 0.5);
sa.setDuration(3000);
sa.setFillAfter(true);
openWeb.startAnimation(sa);
三、应用的欢迎动画界面实现
但一般实际开发中不会单独使用一种动画,基本上都是几个动画的组合实现。下面就实现一个简单欢迎动画界面效果,代码如下:
新建进入动画集anim_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.4"
android:toAlpha="1.0"
android:duration="300"/>
<scale
android:fromXScale="1.0"
android:toXScale="1.2"
android:fromYScale="1.0"
android:toYScale="1.2"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"></scale>
</set>
新建结束动画集anim_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1"
android:toAlpha="0.4"
android:duration="2000"></alpha>
</set>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/welcom"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@drawable/welcom"
android:clipToPadding="true">
</LinearLayout>
public class WelcomActivity extends AppCompatActivity {
private LinearLayout welcom;
Animation anim1=null;
Animation anim2=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_welcom);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
welcom=(LinearLayout) findViewById(R.id.welcom);
anim1 = AnimationUtils.loadAnimation(this,R.anim.anim_in);
anim2 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.anim_out);
welcom.startAnimation(anim1);
anim1.setFillEnabled(true); //启动保持
anim1.setFillAfter(true);//最后一帧保持,否则会跳动到原始
anim1.setAnimationListener(new AnimationImpl() {
@Override
public void onAnimationEnd(Animation animation) {
welcom.startAnimation(anim2);
anim2.setFillAfter(true);
}
});
anim2.setAnimationListener(new AnimationImpl() {
@Override
public void onAnimationEnd(Animation animation) {
skip();
}
});
}
private void skip() {
startActivity(new Intent(this,LoginActivity.class));
finish();
}
}
abstract class AnimationImpl implements Animation.AnimationListener {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
abstract public void onAnimationEnd(Animation animation);
@Override
public void onAnimationRepeat(Animation animation) {
}
}
当然,还需要在AndroidManifest.xml中设置WelcomActivity为启动项,动画这里就不贴了。