【Android】属性动画(83/100)

核心动画代码如下:
private ObjectAnimator alphaAnim, translateAnim, scaleAnim, rotateAnim; // 声明四个属性动画对象
private void initObjectAnim() {
// 构造一个在透明度上变化的属性动画
alphaAnim = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0.1f, 1f);
// 构造一个在横轴上平移的属性动画
translateAnim = ObjectAnimator.ofFloat(imageView, "translationX", 0f, -200f, 0f, 200f, 0f);
// 构造一个在纵轴上缩放的属性动画
scaleAnim = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 0.5f, 1f);
// 构造一个围绕中心点旋转的属性动画
rotateAnim = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f, 0f);
}
效果控制如下:
private String[] objectArray = {"灰度动画", "平移动画", "缩放动画", "旋转动画", "裁剪动画"};
// 播放指定类型的属性动画
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
private void playObjectAnim(int type) {
ObjectAnimator anim = null;
if (type == 0) { // 灰度动画
anim = alphaAnim;
} else if (type == 1) { // 平移动画
anim = translateAnim;
} else if (type == 2) { // 缩放动画
anim = scaleAnim;
} else if (type == 3) { // 旋转动画
anim = rotateAnim;
} else if (type == 4) { // 裁剪动画
int width = imageView.getWidth();
int height = imageView.getHeight();
// 构造一个从四周向中间裁剪的属性动画
ObjectAnimator clipAnim = ObjectAnimator.ofObject(imageView, "clipBounds",
new RectEvaluator(), new Rect(0, 0, width, height),
new Rect(width / 3, height / 3, width / 3 * 2, height / 3 * 2),
new Rect(0, 0, width, height));
anim = clipAnim;
}
if (anim != null) {
anim.setDuration(3000); // 设置动画的播放时长
anim.start(); // 开始播放属性动画
}
}
补充xml布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.ObjectAnimActivity">
<TextView
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/tv_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="请选择:"
android:textColor="@color/black"
android:textSize="17sp"
/>
<Spinner
app:layout_constraintStart_toEndOf="@id/tv_label"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:id="@+id/spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:spinnerMode="dropdown" />
<ImageView
android:id="@+id/iv_origin"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_label"
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="@mipmap/ic_img05"
android:scaleType="fitXY"
android:layout_margin="10dp"
/>
</android.support.constraint.ConstraintLayout>
在最初的android动画框架里有许多的缺陷,后来随着android系统版本的迭代陆陆续续修复了。这些动画框架可能都用不到了。但参考学习还是不错的。
## SmartApi开发工具
全新版本
极小,极快,极限性能的开发调试工具
官网地址:[http://www.smartapi.site/](http://www.smartapi.site/)


## 旧版本不在维护更新

浙公网安备 33010602011771号