前言
一直在用,但老是记不住,因此记录于此,方便自己查阅。
纸上得来终觉浅
正文
下面简单记录一下属性动画的旋转、平移、缩放和透明度渐变。
缩放
- 对X轴
private void TestScaleX(View view) {
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, 1.5f, 1);
scaleX.setDuration(500);
scaleX.start();
}
- 对Y轴
private void TestScaleY(View view) {
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, 1.5f, 1);
scaleY.setDuration(500);
scaleY.start();
}
- 对X和Y轴
private void TestScaleXY(View view) {
AnimatorSet animatorSet = new AnimatorSet();
/**
* 组合动画
* view : 缩放的View
* propertyName : 动画类型: scaleX 对x轴进行缩放
* value1 : 起始值,缩放起始值,1.0f就是默认大小
* value2 : 结束值,缩放结束值,1.5f意思在原来基础上放大0.5f
*
* PS: value 可以有N个,中间的是过度值,最后的是结束状态值
*/
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, 1.5f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, 1.5f);
animatorSet.setDuration(500);
animatorSet.setInterpolator(new LinearInterpolator());
animatorSet.play(scaleX).with(scaleY);
animatorSet.start();
}
透明度渐变
@SuppressLint("ObjectAnimatorBinding")
private void TestAlpha(View view) {
/**
* alpha 范围值 0.0f-1.0f
*/
ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", 1.0f, 0.5f);
alpha.setDuration(500);
alpha.start();
}
平移
- 对X轴
@SuppressLint("ObjectAnimatorBinding")
private void TestTranslationX(View view) {
ObjectAnimator translation = ObjectAnimator.ofFloat(view, "translationX", 0, 300, 0);
translation.setDuration(500);
translation.start();
}
- 对Y轴
@SuppressLint("ObjectAnimatorBinding")
private void TestTranslationY(View view) {
ObjectAnimator translation = ObjectAnimator.ofFloat(view, "translationY", 0, 300, 0);
translation.setDuration(500);
translation.start();
}
- 对XY轴
private void TestTranslation(View view) {
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator translationX = ObjectAnimator.ofFloat(view, "translationX", 0, 300, 0);
ObjectAnimator translationY = ObjectAnimator.ofFloat(view, "translationY", 0, 300, 0);
animatorSet.setDuration(500);
animatorSet.setInterpolator(new LinearInterpolator());
animatorSet.play(translationX).with(translationY);
animatorSet.start();
}
旋转
- 对X轴
private void TestRotationX(View view) {
ObjectAnimator rotationX = ObjectAnimator.ofFloat(view, "rotationX", 0, 180);
rotationX.setDuration(500);
rotationX.start();
}
- 对Y轴
private void TestRotationY(View view) {
ObjectAnimator rotationY = ObjectAnimator.ofFloat(view, "rotationY", 0, 180);
rotationY.setDuration(500);
rotationY.start();
}
- 对Z轴
private void TestRotation(View view) {
/**
* rotation 范围值 0-360
*/
ObjectAnimator rotation = ObjectAnimator.ofFloat(view, "rotation", 0, 180);
rotation.setDuration(500);
rotation.start();
}
参考文章
© 版权声明