Android 旋转动画简单实现

背景

产品优化,需要在启动页添加 loading 提示,通过一个图片 360 度旋转实现。如下图:

在这里插入图片描述


实现

步骤

  • 获取 View 对象(这里示例使用 ImageView)
  • 根据 anim resource 加载一个 动画对象
  • 调用 View 对象的 startAnimation 方法开始动画

动画 set xml

在这里插入图片描述

rotate.xml 如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="600"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:interpolator="@android:anim/linear_interpolator"
        android:toDegrees="350" />
</set>

相关属性就不再介绍了,大家自行阅读这里,有详细的属性介绍


代码

private void rotate() {
        // 根据 anim resource 加载一个 动画对象
        Animation rotateAnim = AnimationUtils.loadAnimation(this, R.anim.rotate);
        // 创建 dialog 并指定 style
        Dialog dialog = new Dialog(this, R.style.dialog);
        // LayoutInflater 构建 View
        View loadingView = LayoutInflater.from(this).inflate(R.layout.splash_loading, null);
        // 获取 View 对象(这里是 ImageView)
        ImageView view = loadingView.findViewById(R.id.iv_loading);
        // 开始动画
        view.startAnimation(rotateAnim);
        // 返回不可取消 dialog
        dialog.setCancelable(false);
        // 设置显示内容 view
        dialog.setContentView(loadingView);
        // 显示 dialog
        dialog.show();
    }

关键代码其实就三句,不过我是用的 dialog ,所以会多一些跟 dialog 相关的代码

至于为啥我这里用 dialog ,是因为我的启动页是没有 setContentView 的,所以使用的是 dialog


演示

请添加图片描述


技术永不眠!我们下期见!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值