android 圆角播放暂停按钮 及 动画效果

圆角播放暂停按钮 及 动画效果

按钮效果图

在这里插入图片描述在这里插入图片描述

绘制代码
  • 通过vector绘制图形(vector不了解的同学,请百度搜索相关内容进行补课 或者 点击链接查看详细讲解)
<!-- 暂停图形 -->
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="23dp"
    android:height="23dp"
    android:viewportWidth="23"
    android:viewportHeight="23">
    <path
        android:name="play"
        android:fillColor="@color/black"
        android:strokeAlpha="1"
        android:strokeColor="@color/black"
        android:strokeWidth="2"
        android:strokeLineCap="round"
        android:pathData="M7,5 L7,18 M16,5 L16,18 M7,18 L7,18 M16,5 L16,5 L16,5 z" />
</vector>
<!-- 播放图形 -->
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="23dp"
    android:height="23dp"
    android:viewportWidth="23"
    android:viewportHeight="23">
    <path
        android:name="play"
        android:fillColor="@color/black"
        android:strokeAlpha="1"
        android:strokeColor="@color/black"
        android:strokeWidth="2"
        android:strokeLineCap="round"
        android:pathData="M6,5 L17,12 M6,18 L17,12 M6,18 L6,5 M7,6 L7,17 L15,12 z" />
</vector>

接下来就是让它动起来

请添加图片描述

代码实现
  • 利用set 添加动画效果(知识点 set,objectanimator,pathData 需要同学自行搜索补课 )
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="sequentially">

    <objectAnimator
        android:duration="200"
        android:propertyName="pathData"
        android:valueFrom="@string/path_play"
        android:valueTo="@string/path_pause"
        android:valueType="pathType"
        android:repeatMode="restart"
        android:repeatCount="0"/>
    <objectAnimator
        android:duration="100"
        android:propertyName="pathData"
        android:valueFrom="@string/path_pause"
        android:valueTo="@string/path_pause2"
        android:valueType="pathType"
        android:repeatMode="restart"
        android:repeatCount="0"/>
</set>

<!-- 播放和暂停动画 -->

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="sequentially">

    <objectAnimator
        android:duration="100"
        android:propertyName="pathData"
        android:valueFrom="@string/path_pause2"
        android:valueTo="@string/path_pause"
        android:valueType="pathType"
        android:repeatMode="restart"
        android:repeatCount="0"/>
    <objectAnimator
        android:duration="300"
        android:propertyName="pathData"
        android:valueFrom="@string/path_pause"
        android:valueTo="@string/path_play"
        android:valueType="pathType"
        android:repeatMode="restart"
        android:repeatCount="0"/>
</set>
  • 我是添加了两个动作,如果为了简便不需要流畅效果可去掉一个
把动画和图形关联起来
  • 通过 animated-vector 关联图形和动画
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/media_pause">

    <target
        android:name="play"
        android:animation="@anim/media_pause" />
</animated-vector>

<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/media_play">

    <target
        android:name="play"
        android:animation="@anim/media_play" />
</animated-vector>
  • java 代码调用

        findViewById(R.id.image).setOnClickListener(v -> {
            ((ImageView)v).setImageResource(!isPause ?R.drawable.media_pause_animate:R.drawable.media_play_animate);
            AnimatedVectorDrawable drawable = (AnimatedVectorDrawable) ((ImageView)v).getDrawable();
            drawable.start();
            isPause = !isPause;

        });

最后推荐一个我自己写的MVVM开源项目《Open MVVM》

有问题请私信,留言,或者发送邮件到我扣扣邮箱 qingingrunt2010, 再或者加入扣扣群721765299

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值