简介

某些音乐播放或者视频播放的界面上,资源还在加载时,进度条的原点(thumb)会显示一个转圈的效果。

资源加载完成后,又切换回静态效果。这个效果增强了用户体验。

一般来说有美术人员负责设计和切图。尝试实现时,我们可以使用使用drawable,来模拟实现这个转圈的效果。

示例

dimens.xml

为方便管理,可以添加一些尺寸设置

  1. <dimen name="audio_course_item_seek_bar_progress_height">6dp</dimen>
  2. <dimen name="audio_course_item_seek_bar_radius">2dp</dimen>
  3. <dimen name="audio_seek_bar_thumb_size">20dp</dimen>
  4. <dimen name="audio_seek_bar_thumb_ring_width">4dp</dimen>

drawable

我们一共要添加4个drawable文件。分别是2种thumb,1个动画,1个进度条“底座”。

  1. shape_thumb_round_1.xml # 静态thumb
  2. layers_seek_bar_progress_1.xml
  3. layers_thumb_ring_sweep_1.xml
  4. rotate_thumb_1.xml

shape_thumb_round_1.xml

用solid和stroke做出的圆环效果

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="oval">
  4. <solid android:color="#ffffff" />
  5. <stroke
  6. android:width="@dimen/audio_seek_bar_thumb_ring_width"
  7. android:color="#7095fc" />
  8. <size
  9. android:width="@dimen/audio_seek_bar_thumb_size"
  10. android:height="@dimen/audio_seek_bar_thumb_size" />
  11. </shape>

layers_thumb_ring_sweep_1.xml

这是准备拿来转圈的thumb。使用layer-list,叠加多层效果。

底部是一个半白色的圆(android:shape="oval")。

再叠加上一层圆环(android:shape="ring"),使用了渐变色,增加动感。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item>
  4. <shape android:shape="oval">
  5. <size
  6. android:width="@dimen/audio_seek_bar_thumb_size"
  7. android:height="@dimen/audio_seek_bar_thumb_size" />
  8. <solid android:color="#ffffff" />
  9. </shape>
  10. </item>
  11. <item>
  12. <shape
  13. android:innerRadius="4dp"
  14. android:thicknessRatio="6"
  15. android:shape="ring"
  16. android:useLevel="false">
  17. <gradient
  18. android:endColor="#ffffff"
  19. android:startColor="#7095fc"
  20. android:type="sweep" />
  21. <size
  22. android:width="@dimen/audio_seek_bar_thumb_size"
  23. android:height="@dimen/audio_seek_bar_thumb_size" />
  24. </shape>
  25. </item>
  26. </layer-list>

rotate_thumb_1.xml

定义旋转效果。注意它的drawable使用了上面定义的layers_thumb_ring_sweep_1.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <rotate xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:drawable="@drawable/layers_thumb_ring_sweep_1"
  4. android:duration="100"
  5. android:fromDegrees="0"
  6. android:pivotX="50%"
  7. android:pivotY="50%"
  8. android:toDegrees="-360" />

旋转参数android:toDegrees可以根据需求定义。

layers_seek_bar_progress_1.xml

定义进度条的样式。这个是“底座”。颜色要和上面的匹配,看起来好看一点。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:id="@android:id/background">
  4. <shape>
  5. <size
  6. android:width="5dp"
  7. android:height="@dimen/audio_course_item_seek_bar_progress_height" />
  8. <solid android:color="#e1e5e8" />
  9. </shape>
  10. </item>
  11. <item android:id="@android:id/secondaryProgress">
  12. <clip>
  13. <shape>
  14. <solid android:color="#b7bdc8" />
  15. </shape>
  16. </clip>
  17. </item>
  18. <item android:id="@android:id/progress">
  19. <clip>
  20. <shape>
  21. <gradient
  22. android:angle="0"
  23. android:centerColor="#b8cafd"
  24. android:endColor="#86a4fd"
  25. android:startColor="#eef2ff" />
  26. </shape>
  27. </clip>
  28. </item>
  29. </layer-list>

layout

上面的资源文件准备完毕后。在我们的布局中添加一个SeekBar

  • android:maxHeightandroid:minHeight需要设置
  • android:progressDrawable 用前面定义好的“底座”
  • android:thumb 先使用静态的样式
  1. <SeekBar
  2. android:id="@+id/play_sb"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_marginTop="16dp"
  6. android:background="@null"
  7. android:maxHeight="@dimen/audio_course_item_seek_bar_progress_height"
  8. android:minHeight="@dimen/audio_course_item_seek_bar_progress_height"
  9. android:progress="40"
  10. android:progressDrawable="@drawable/layers_seek_bar_progress_1"
  11. android:thumb="@drawable/shape_thumb_round_1"
  12. app:layout_constraintTop_toTopOf="parent" />

Activity中调用

由Activity来持有Drawable变量和动画。例子中使用了dataBinding。

  1. private RotateDrawable mRotateThumbDrawable; // 加载中的thumb,由Activity来持有这个drawable
  2. private Drawable mSolidThumb;
  3. private ObjectAnimator mThumbAnimator; // 控制动画
  4. // ...
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. mBinding = DataBindingUtil.setContentView(this, R.layout.act_seekbar_1);// ...
  9. mRotateThumbDrawable = (RotateDrawable) AppCompatResources.getDrawable(getApplicationContext(), R.drawable.rotate_thumb_1);
  10. mSolidThumb = AppCompatResources.getDrawable(getApplicationContext(), R.drawable.shape_thumb_round_1);
  11. }

Drawable对象由activity直接持有,操作起来比较方便。

改变seekbar的thumb,使用方法setThumb(Drawable thumb)

使用静态的thumb

  1. mBinding.playSb.setThumb(mSolidThumb);

使用转圈圈的效果,先setThumb,并且需要启动动画

  1. mBinding.playSb.setThumb(mRotateThumbDrawable);
  2. mThumbAnimator = ObjectAnimator.ofInt(mRotateThumbDrawable, "level", 0, 10000);
  3. mThumbAnimator.setDuration(1000);
  4. mThumbAnimator.setRepeatCount(ValueAnimator.INFINITE);
  5. mThumbAnimator.setInterpolator(new LinearInterpolator());
  6. mThumbAnimator.start();

效果如下图

可以在静态和动态之间相互切换。

离开页面时记得关闭动画

  1. @Override
  2. protected void onDestroy() {
  3. if (null != mThumbAnimator) {
  4. mThumbAnimator.cancel();
  5. }
  6. super.onDestroy();
  7. }

小结

要实现转圈的效果。主要还是直接操作drawable对象,把动画加进去。

setThumb(Drawable thumb)方法接受的是Drawable对象,那么我们的思路就是从控制Drawable这点下手。

全部使用drawable可以达到文中的效果。有条件的也可以使用图片资源。做出更丰富的效果。

参考:

更多Android文章可参考 https://an.rustfisher.com/

Android SeekBar 自定义thumb,thumb旋转动画效果的更多相关文章

  1. android xml实现animation 4种动画效果

    animation有四种动画类型 分别为alpha(透明的渐变).rotate(旋转).scale(尺寸伸缩).translate(移动),二实现的分发有两种,一种是javaCode,另外一种是XML ...

  2. 拒绝IE8-,CSS3 transform rotate旋转动画效果(支持IE9+/chrome/firefox)

    <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta nam ...

  3. Android笔记之为自定义对话框添加移动动画效果

    给底部的对话框添加移动动画效果 可通过Window.setWindowAnimations(int resId)设置 SharingDialog.java package com.bu_ish.sha ...

  4. Android SeekBar自定义使用图片和颜色显示

    案例使用的图片如下:                            1.在res/drawable目录下新增一个xml风格文件,seekbar_define_style.xml ? 1 2 3 ...

  5. Android PullToRrefresh 自定义下拉刷新动画 (listview、scrollview等)

    PullToRefreshScrollView 自定义下拉刷新动画,只需改一处. 以下部分转载自http://blog.csdn.net/superjunjin/article/details/450 ...

  6. AndroidUI 视图动画-旋转动画效果 (RotateAnimation)

    RotateAnimation,能实现Android的视图的旋转效果,废话不多说直接上代码. 新建一个Android 项目,在activity_main.xml中添加一个按钮,然后使用Relative ...

  7. [android] 优酷环形菜单-旋转动画

    获取房子,菜单图标ImageView对象,获取三个圆环RelativeLayout对象 给菜单图标(icon_menu)设置点击事件 定义一个成员变量isLevel3Show来存储第三级菜单是否显示 ...

  8. 一起学android之设置ListView数据显示的动画效果(24)

    效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFpX3FpbmdfeHVfa29uZw==/font/5a6L5L2T/fontsize/40 ...

  9. UI特效--Android利用ViewFlipper实现屏幕切换动画效果

    .屏幕切换指的是在同一个Activity内屏幕见的切换,最长见的情况就是在一个FrameLayout内有多个页面,比如一个系统设置页面:一个个性化设置页面.2.介绍ViewFilpper类ViewFl ...

  10. Mono For Android中简单实现按钮的动画效果

    Android中动画的分Tween Animation和Frame Animation,本节主要讲Tween Animation的实现. 一般是通过XML文件来定义动画的,具体如下: 1.在项目res ...

随机推荐

  1. python paramiko实现ssh上传下载执行命令

    paramiko ssh上传下载执行命令 序言 最近项目经常需要动态在跳板机上登录服务器进行部署环境,且服务器比较多,每次完成所有服务器到环境部署执行耗费大量时间.为了解决这个问题,根据所学的执行实现 ...

  2. docker - compose 部署 Nginx

    主要介绍 docker 中 Nginx 的部署及项目目录挂载券的方法.docker 中部署一个服务,有三种方法,分别是 docker run.Dockerfile.docker-compose . 下 ...

  3. Windows下的程序及热键监视神器——Spy++

    Windows下的程序及热键监视神器--Spy++ 背景 在使用Windows的时候,偶尔会发现某些应用程序的热键不生效了:又或是桌面弹出了弹框却并不知道这个弹框来自何处.例如,本人最近使用Vim的时 ...

  4. B站1024程序员节部分答案

    1.页面的背后是什么? 直接撸页面源码就行啦 2.真正的秘密只有特殊的设备才能看到 修改UA为页面上提示的"bilibili Security Browser" 3.密码是啥? 弱 ...

  5. SpringBoot入门03-转发到Thymeleaf

    前言 Spring Boot不提倡使用jsp和用View层,而是使用Thymeleaf代替jsp,因为性能可以得到提升. 使用Thymeleaf要加入依赖 Thymeleaf不能直接被访问,它严格遵守 ...

  6. Java(17)面向对象之多态

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201621.html 博客主页:https://www.cnblogs.com/testero ...

  7. CentOS 压缩解压

    目录 命令 tar gzip.gunzip bzip2.bunzip2 zip.unzip 命令组合 打包:将多个文件合成一个总的文件,这个总的文件通常称为"归档". 压缩:将一个 ...

  8. 学习笔记-React的简单介绍&工作原理

    一.React简单介绍 1.React起源于Facebook内部项目,与2013年5月 2.是一个用于构建用户界面的JavaScript库 二.React特点 1.声明式设计-React采用声明范式, ...

  9. poi实现生成下拉选

    在我们日常开发中,经常需要使用poi操作excel文件,现在就简单介绍一下在poi中是如何生成下拉选的. 1.创建workbook 2.创建数据约束 3.设置数据的有效性 @Test public v ...

  10. GitHub Universe 2021|MS Reactor 邀你共聚年度盛会

    GitHub Universe 2021 将于2021年10月27-28日(PDT)在线直播,MS Reactor 将与 CSDN 合作进行转播,与你一同观看这场全球开发者盛会. 关于 GitHub ...