创建逻辑:
1 准备动画资源:在res/anim文件夹下创建4个xml文件
2 加载动画:AnimationUtils.loadAnimation(Context context, int id)
3 开启动画:View.startAnimation(Animation animation)
相关的类:
动画 | 创建方式 | 获取Animation对象的方式 | 例子 |
---|---|---|---|
帧动画drawable animation | 代码创建 | getBackground() | AnimationDrawable animation = (AnimationDrawable) imageView.getBackground(); |
xml | 不需要获取 | android:background=”@drawable/animation_main” | |
补间动画View Animation | 代码创建 | new … | RotateAnimation animation = new RotateAnimation(1, 10); |
xml创建 | AnimationUtils.loadAnimation(…) | Animation animation = AnimationUtils.loadAnimation(context, R.anim.anim_translate) | |
属性动画Property Animator | 代码创建 | ObjectAnimator.ofFloat(…) | ObjectAnimator oa1 = ObjectAnimator.ofFloat(iv, “scaleX”, 1, 3, 1, 3,5, -2) |
xml创建 | AnimatorInflater.loadAnimator(…) | ObjectAnimator oa = (ObjectAnimator) AnimatorInflater.loadAnimator(context, R.animator.alpha) |
AnimationUtils:动画工具类(Android自带)
分类
rotate + alpha + scale + translate
相关的属性:
4种动画只有transition
使用百分比。
android:duration= 动画时长
android:repeatCount= 重复次数(0是不重复)
android:repeatMode= 重复的模式
android:fillAfter= 是否停留在动画结束的位置
android:startOffset= 动画延迟时间
rotate:
android:fromDegrees= 开始的角度
android:toDegrees= 结束的角度
android:pivotX= 轴心的X坐标
android:pivotY= 轴心的Y坐标
pivotX+pivotY:加%表示相对于自身,不加%表示像素:移动几个像素的距离
alpha:
android:fromAlpha= 开始的透明度
android:toAlpha= 结束的透明度
scale:
android:fromXScale= 开始时X轴缩放比
android:toXScale= 结束时X轴缩放比
android:fromYScale= 开始时Y轴缩放比
android:toYScale= 结束时Y轴缩放比
translate
Delta:加%表示相对于自身,不加%表示像素:移动几个像素的距离
android:fromXDelta= 开始时轴心X轴的坐标,
android:fromYDelta= 开始时轴心Y轴的坐标
android:toXDelta= 结束时轴心X轴的坐标
android:toYDelta= 结束时轴心Y轴的坐标
效果图:
1 准备动画资源:
anim_alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fillAfter="true"
android:fromAlpha="1"
android:repeatCount="1"
android:repeatMode="reverse"
android:startOffset="2000"
android:toAlpha="0.2" >
<!--startOffset:延迟时间 -->
</alpha>
anim_rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fillAfter="true"
android:fromDegrees="0"
android:pivotX="50%"
android:repeatCount="3"
android:repeatMode="reverse"
android:pivotY="50%"
android:toDegrees="360" >
</rotate>
anim_scale.xml
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="true"
android:fromXScale="1"
android:fromYScale="1"
android:repeatCount="1"
android:repeatMode="reverse"
android:toXScale="0.5"
android:toYScale="0.5" >
<!-- android:startOffset="1000" 延迟1秒 -->
</scale>
anim_translate.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="true"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:repeatCount="1"
android:repeatMode="reverse"
android:toXDelta="300%"
android:toYDelta="300%" >
<!-- Delta:点 -->
<!-- toXDelta="30%": 加%表示相对于自身,不加%表示像素:移动几个像素的距离-->
</translate>
anim_set.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fillAfter="false"
android:repeatCount="3"
android:repeatMode="reverse"
android:startOffset="2000" >
<rotate
android:fromDegrees="0"
android:pivotX="0.5"
android:pivotY="0.5"
android:toDegrees="360" >
</rotate>
<alpha
android:fromAlpha="1"
android:toAlpha="0.2" >
</alpha>
<scale
android:fromXScale="1"
android:fromYScale="1"
android:toXScale="0.5"
android:toYScale="0.5" >
</scale>
<translate
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="300%"
android:toYDelta="300%" >
</translate>
</set>
2 开启动画
MainActivity.java
package com.android.xmlviewanimation;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
}
// 平移
public void click1(View view) {
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_translate);
imageView.startAnimation(animation);
}
// 缩放(从100%缩放到50%,最后复原)
public void click2(View view) {
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_scale);
imageView.startAnimation(animation);
}
// 透明度
public void click3(View view) {
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_alpha);
imageView.startAnimation(animation);
}
// 旋转
public void click4(View view) {
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_rotate);
imageView.startAnimation(animation);
}
// 一起
public void click5(View view) {
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_set);
imageView.startAnimation(animation);
}
}