【Android】摇摆动画(84/100)

在这里插入图片描述
自定义动画类:SwingAnimation

package top.lc951.myandroid.views;

import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
 * @author lichong
 * 2022年08月22日15:20:20
 */
public class SwingAnimation extends Animation {
    private float mMiddleDegrees; // 中间的角度
    private float mLeftDegrees; // 左边的角度
    private float mRightDegrees; // 右边的角度
    private int mPivotXType; // 圆心的横坐标类型
    private int mPivotYType; // 圆心的纵坐标类型
    private float mPivotXValue; // 圆心横坐标的数值比例
    private float mPivotYValue; // 圆心纵坐标的数值比例
    private float mPivotX; // 圆心横坐标的数值
    private float mPivotY; // 圆心纵坐标的数值

    public SwingAnimation(float middleDegrees, float leftDegrees, float rightDegrees) {
        this(middleDegrees, leftDegrees, rightDegrees, 0.0f, 0.0f);
    }

    public SwingAnimation(float middleDegrees, float leftDegrees,
                          float rightDegrees, float pivotX, float pivotY) {
        this(middleDegrees, leftDegrees, rightDegrees, ABSOLUTE, pivotX, ABSOLUTE, pivotY);
    }

    public SwingAnimation(float middleDegrees, float leftDegrees, float rightDegrees,
                          int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {
        mMiddleDegrees = middleDegrees;
        mLeftDegrees = leftDegrees;
        mRightDegrees = rightDegrees;
        mPivotXValue = pivotXValue;
        mPivotXType = pivotXType;
        mPivotYValue = pivotYValue;
        mPivotYType = pivotYType;
        initializePivotPoint();
    }

    // 初始化圆心的横纵坐标数值
    private void initializePivotPoint() {
        if (mPivotXType == ABSOLUTE) {
            mPivotX = mPivotXValue;
        }
        if (mPivotYType == ABSOLUTE) {
            mPivotY = mPivotYValue;
        }
    }

    // 在动画变换过程中调用
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        float degrees;
        float leftPos = (float) (1.0 / 4.0); // 摆到左边端点时的时间比例
        float rightPos = (float) (3.0 / 4.0); // 摆到右边端点时的时间比例
        if (interpolatedTime <= leftPos) { // 从中间线往左边端点摆
            degrees = mMiddleDegrees + ((mLeftDegrees - mMiddleDegrees) * interpolatedTime * 4);
        } else if (interpolatedTime > leftPos && interpolatedTime < rightPos) { // 从左边端点往右边端点摆
            degrees = mLeftDegrees + ((mRightDegrees - mLeftDegrees) * (interpolatedTime - leftPos) * 2);
        } else { // 从右边端点往中间线摆
            degrees = mRightDegrees + ((mMiddleDegrees - mRightDegrees) * (interpolatedTime - rightPos) * 4);
        }
        float scale = getScaleFactor(); // 获得缩放比率
        if (mPivotX == 0.0f && mPivotY == 0.0f) {
            t.getMatrix().setRotate(degrees);
        } else {
            t.getMatrix().setRotate(degrees, mPivotX * scale, mPivotY * scale);
        }
    }

    // 在初始化时调用
    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        mPivotX = resolveSize(mPivotXType, mPivotXValue, width, parentWidth);
        mPivotY = resolveSize(mPivotYType, mPivotYValue, height, parentHeight);
    }

}

控制使用:

package top.lc951.myandroid.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.widget.ImageView;

import top.lc951.myandroid.R;
import top.lc951.myandroid.views.SwingAnimation;

/**
 * 播放摇摆动画
 */
public class SwingAnimActivity extends AppCompatActivity {
    private ImageView iv_swing; // 声明一个图像视图对象

    public static void actionActivity(Context context) {
        Intent intent = new Intent(context, SwingAnimActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swing_anim);
        iv_swing = findViewById(R.id.iv_swing);
        findViewById(R.id.layout_root)
                .setOnClickListener(v -> showSwingAnimation());
        showSwingAnimation(); // 开始播放摇摆动画
    }

    private void showSwingAnimation() {
        // 创建一个摇摆动画
        // 参数取值说明:中间度数、摆到左侧的度数、摆到右侧的度数、圆心X坐标类型、圆心X坐标相对比例、圆心Y坐标类型、圆心Y坐标相对比例
        // 坐标类型有三种:ABSOLUTE 绝对坐标,RELATIVE_TO_SELF 相对自身的坐标,RELATIVE_TO_PARENT 相对上级视图的坐标
        // X坐标相对比例,为0时表示左边顶点,为1表示右边顶点,为0.5表示水平中心点
        // Y坐标相对比例,为0时表示上边顶点,为1表示下边顶点,为0.5表示垂直中心点
        SwingAnimation swingAnimation = new SwingAnimation(
                0f, 60f, -60f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.0f);
        swingAnimation.setDuration(4000); // 设置动画的播放时长
        swingAnimation.setRepeatCount(0); // 设置动画的重播次数
        swingAnimation.setFillAfter(false); // 设置维持结束画面
        swingAnimation.setStartOffset(500); // 设置动画的启动延迟
        iv_swing.startAnimation(swingAnimation); // 开始播放摇摆动画
    }
}

这个动画有点古董钟摇摆效果。
突然写这个会比较麻,留着以后直接用蛤!!!

自研产品推荐

历时一年半多开发终于smartApi-v1.0.0版本在2023-09-15晚十点正式上线
smartApi是一款对标国外的postman的api调试开发工具,由于开发人力就作者一个所以人力有限,因此v1.0.0版本功能进行精简,大功能项有:

  • api参数填写
  • api请求响应数据展示
  • PDF形式的分享文档
  • Mock本地化解决方案
  • api列表数据本地化处理
  • 再加上UI方面的打磨

为了更好服务大家把之前的公众号和软件激活结合,如有疑问请大家反馈到公众号即可,下个版本30%以上的更新会来自公众号的反馈。
嗯!先解释不上服务端原因,API调试工具的绝大多数时候就是一个数据模型、数据处理、数据模型理解共识的问题解决工具,所以作者结合自己十多年开发使用的一些痛点来打造的,再加上服务端开发一般是面向企业的,作者目前没有精力和时间去打造企业服务。再加上没有资金投入所以服务端开发会滞后,至于什么时候会进行开发,这个要看募资情况和用户反馈综合考虑。虽然目前国内有些比较知名的api工具了,但作者使用后还是觉得和实际使用场景不符。如果有相关吐槽也可以在作者的公众号里反馈蛤!
下面是一段smartApi使用介绍:
在这里插入图片描述

下载地址:

https://pan.baidu.com/s/1iultkXqeLNG4_eNiefKTjQ?pwd=cnbl

posted @ 2022-08-22 15:51  lichong951  阅读(6)  评论(0编辑  收藏  举报  来源