Android之属性动画简介

广告也精彩

前言

一直在用,但老是记不住,因此记录于此,方便自己查阅。

纸上得来终觉浅

正文

下面简单记录一下属性动画的旋转、平移、缩放和透明度渐变。

缩放

  1. 对X轴
private void TestScaleX(View view) {
    ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, 1.5f, 1);
    scaleX.setDuration(500);
    scaleX.start();
}
  1. 对Y轴
private void TestScaleY(View view) {
    ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, 1.5f, 1);
    scaleY.setDuration(500);
    scaleY.start();
}
  1. 对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();
}

平移

  1. 对X轴
@SuppressLint("ObjectAnimatorBinding")
private void TestTranslationX(View view) {
    ObjectAnimator translation = ObjectAnimator.ofFloat(view, "translationX", 0, 300, 0);
    translation.setDuration(500);
    translation.start();
}

  1. 对Y轴
@SuppressLint("ObjectAnimatorBinding")
private void TestTranslationY(View view) {
    ObjectAnimator translation = ObjectAnimator.ofFloat(view, "translationY", 0, 300, 0);
    translation.setDuration(500);
    translation.start();
}

  1. 对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();
}

旋转

  1. 对X轴
private void TestRotationX(View view) {
    ObjectAnimator rotationX = ObjectAnimator.ofFloat(view, "rotationX", 0, 180);
    rotationX.setDuration(500);
    rotationX.start();
}

  1. 对Y轴
private void TestRotationY(View view) {
    ObjectAnimator rotationY = ObjectAnimator.ofFloat(view, "rotationY", 0, 180);
    rotationY.setDuration(500);
    rotationY.start();
}
  1. 对Z轴
private void TestRotation(View view) {
    /**
     * rotation 范围值 0-360
     */
    ObjectAnimator rotation = ObjectAnimator.ofFloat(view, "rotation", 0, 180);
    rotation.setDuration(500);
    rotation.start();
}

参考文章

  1. Android 实现属性动画平移,旋转,缩放,渐变
  2. ValueAnimator、objectAnimator的基础用法
  3. 自定义控件三部曲之动画篇(七)——ObjectAnimator基本使用

广告也精彩
版权声明:125la.com站长 发表于 2022年5月5日 下午8:20。
转载请注明:Android之属性动画简介 | 125啦读书导航

相关文章

广告也精彩

暂无评论

暂无评论...