Property animation
文件位置/res/anitor/filename.xml
In Java: R.animator.filename
In XML: @[package:]animator/filename
语法
<set
<!--选择是一起执行动画还是按照顺序来-->
android:ordering=["together" | "sequentially"]>
<objectAnimator
<!--图像的动画名称来规定属性比如backgroudcolor-->
android:propertyName="string"
<!--指定播放时间-->
android:duration="int"
<!--指定从哪里开始具体的要根据你指定的propertyName-->
android:valueFrom="float | int | color"
<!--制定从哪里结束具体的要根据你指定的propertyName-->
android:valueTo="float | int | color"
<!--比开始偏移多少也可以理解为延时-->
android:startOffset="int"
<!--重复次数比如1是重复两次-->
android:repeatCount="int"
<!--重复模式-->
android:repeatMode=["repeat" | "reverse"]
<!--默认使用floatType建议不要更改-->
android:valueType=["intType" | "floatType"]/>
<animator
android:duration="int"
android:valueFrom="float | int | color"
android:valueTo="float | int | color"
android:startOffset="int"
android:repeatCount="int"
android:repeatMode=["repeat" | "reverse"]
android:valueType=["intType" | "floatType"]/>
<set>
...
</set>
</set>
一个简单的例子
新建一个res/anitor/test.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially">
<!--dd-->
<objectAnimator
android:propertyName="rotation"
android:duration="2000"
android:repeatCount="1"
android:repeatMode="reverse"
android:valueFrom="0"
android:valueTo="720"/>
<objectAnimator
android:propertyName="rotation"
android:duration="2000"
android:repeatCount="1"
android:startOffset="1000"
android:repeatMode="reverse"
android:valueFrom="0"
android:valueTo="720"/>
</set>
在MainActivity.class中
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AnimatorSet animatorSet = (AnimatorSet)AnimatorInflater.loadAnimator(MainActivity.this, R.animator.test);
animatorSet.setTarget(imageView);
animatorSet.start();
}
});
Tween animation
文件存储位置
res/anim/filename.xml
In Java: R.anim.filename
In XML: @[package:]anim/filename
语法
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@[package:]anim/interpolator_resource"
android:shareInterpolator=["true" | "false"] >
<!--渐变-->
<alpha
android:fromAlpha="float"
android:toAlpha="float" />
<!--缩放-->
<scale
android:fromXScale="float"
android:toXScale="float"
android:fromYScale="float"
android:toYScale="float"
android:pivotX="float"
android:pivotY="float" />
<!--位置移动-->
<translate
android:fromXDelta="float"
android:toXDelta="float"
android:fromYDelta="float"
android:toYDelta="float" />
<!--翻转-->
<rotate
android:fromDegrees="float"
android:toDegrees="float"
android:pivotX="float"
android:pivotY="float" />
<set>
...
</set>
</set>
一个简单的例子
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="10000"
android:shareInterpolator="true">
<alpha
android:fromAlpha="0"
android:toAlpha="100"/>
<translate
android:fromXDelta="100"
android:toXDelta="200"/>
<translate
android:fromXDelta="0"
android:toXDelta="100"
android:fromYDelta="0"
android:toYDelta="100"/>
<rotate
android:fromDegrees="0"
android:pivotY="50%"
android:pivotX="50%"
android:toDegrees="360"/>
</set>
MainActivity.class
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.test);
imageView.startAnimation(animation);
}
});
Frame animation
文件位置
res/drawable/filename.xml
In Java: R.drawable.filename
In XML: @[package:]drawable.filename
语法
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
<!---true代表一次false代表一直-->
android:oneshot=["true" | "false"] >
<item
android:drawable="@[package:]drawable/drawable_resource_name"
android:duration="integer" />
</animation-list>
一个简单的例子
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_launcher_background" android:duration="1000"/>
<item android:drawable="@drawable/ic_launcher_foreground" android:duration="1000"/>
</animation-list>
MainActivity.class
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageView.setImageResource(R.drawable.test);
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getDrawable();
animationDrawable.start();
}
});
网友评论