当前位置:网站首页>【微信小程序开发(六)】绘制音乐播放器环形进度条
【微信小程序开发(六)】绘制音乐播放器环形进度条
2022-07-28 01:39:00 【袜子是一只狗】
需求背景
需要开发类似于网易云播放器的功能,这里主要讲解环形进度条的问题
下图是最终上线的效果图
一、微信小程序不支持svg
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
二、使用canvas
1.这里主要放绘制环形进度条的代码
代码如下(示例):
drawCanvasCircleByStep() {
// 背景播放器暂停进度结束就不画了
if (isNaN(wx.$globalData.backgroundAudioManager.duration)) {
return;
}
// 画满是 this.data.step 2 其中 this.data.innerAudioContextStatus 内存控制音频播放状态 1 正在播放
if(this.data.step >= 2 || this.data.innerAudioContextStatus != '1') {
return;
}
// 这里同时控制中间图片的旋转,这样可以在同一个方法中控制进度条和图片的旋转暂停
this.data.ratateDeg = this.data.ratateDeg + 0.2;
this.setData({
ratateDeg: this.data.ratateDeg
})
// 2 乘以 音频播放进度比例
this.data.step = 2 * ((wx.$globalData.backgroundAudioManager.currentTime)/(wx.$globalData.backgroundAudioManager.duration));
// console.log(this.data.step)
if (!this.data.secondCanvas) {
// 部分机型不支持多次创建 所以只有第一次getContext
const query = wx.createSelectorQuery()
query.select('#canvasProgressbg2')
.fields({
node: true, size: true })
.exec((res) => {
this.data.secondCanvas = res[0].node
const ctx = this.data.secondCanvas.getContext('2d')
const dpr = wx.getSystemInfoSync().pixelRatio
this.data.secondCanvas.width = 500 * dpr
this.data.secondCanvas.height = 500 * dpr
ctx.scale(dpr, dpr)
// 绘制前清空画布
ctx.clearRect(0, 0, this.data.secondCanvas.width, this.data.secondCanvas.height)
ctx.lineWidth = 6;// 设置圆环的宽度
var gradient = ctx.createLinearGradient(500, 250, 250, 500);
gradient.addColorStop("0", "rgba(191, 204, 222, 1)");
gradient.addColorStop("1", "rgba(104, 129, 183, 1)");
ctx.strokeStyle = gradient; // 设置圆环的颜色
ctx.lineCap = '#A9B9D4' // 设置圆环端点的形状
ctx.beginPath();//开始一个新的路径
ctx.arc(250, 250, 484/2, -Math.PI / 2, this.data.step * Math.PI - Math.PI / 2);
ctx.stroke();
ctx.closePath();
this.data.secondCtx = ctx;
// 使用 requestAnimationFrame 递归绘制
this.data.requestAnimationFrame = this.data.secondCanvas.requestAnimationFrame(this.drawCanvasCircleByStep)
})
} else {
const ctx = this.data.secondCtx
// 绘制前清空画布
ctx.clearRect(0, 0, this.data.secondCanvas.width, this.data.secondCanvas.height)
ctx.lineWidth = 6;// 设置圆环的宽度
var gradient = ctx.createLinearGradient(500, 250, 250, 500);
gradient.addColorStop("0", "rgba(191, 204, 222, 1)");
gradient.addColorStop("1", "rgba(104, 129, 183, 1)");
ctx.strokeStyle = gradient; // 设置圆环的颜色
ctx.lineCap = 'round' // 设置圆环端点的形状
ctx.beginPath();//开始一个新的路径
ctx.arc(250, 250, 484/2, -Math.PI / 2, this.data.step * Math.PI - Math.PI / 2);
ctx.stroke();
ctx.closePath();
this.data.requestAnimationFrame = this.data.secondCanvas.requestAnimationFrame(this.drawCanvasCircleByStep)
}
},
2. 部分机型安卓不支持渐变
代码如下(示例):
gradient.addColorStop("0", "rgba(191, 204, 222, 1)");
gradient.addColorStop("1", "rgba(104, 129, 183, 1)");
ctx.strokeStyle = gradient; // 设置圆环的颜色 这里直接改为纯色值就好了
3. 处理好页面中的定时器 在onUnload中清除,微信小程序有内存泄漏
onUnload:function(){
if (this.timeupdateOnBackgourndAudio) {
clearInterval(this.timeupdateOnBackgourndAudio)
}
// 同理setTimeout 清除使用 clearTimeout(timer);
},
边栏推荐
- 【英雄哥七月集训】第 27天:图
- MySQL锁系列之锁算法详解(荣耀典藏版)
- what‘s the meaning of “rc“ in release name
- 程序里随处可见的interface,真的有用吗?真的用对了吗?
- Wechat campus bathroom reservation applet graduation design finished product (2) applet function
- With elephant & nbsp; Eplato created by swap, analysis of the high premium behind it
- 【TA-霜狼_may-《百人计划》】图形3.5 Early-z 和 Z-prepass
- Compile and use Qwt in qt|vs2017
- 新基建助力智能化道路交通领域的转型发展
- Usage of delegate
猜你喜欢

selenium+pytest+allure综合练习

Cesium3Dtilesets 使用customShader的解读以及泛光效果示例

pytest最好的测试框架

1313_ Pyserial installation and document generation

retainface使用报错:ModuleNotFoundError: No module named 'rcnn.cython.bbox'

MySQL锁系列之锁算法详解(荣耀典藏版)

Today in history: the father of database passed away; Apple buys cups code; IBM chip Alliance

Compile and use Qwt in qt|vs2017

ps 简单使用

Is the interface that can be seen everywhere in the program really useful? Is it really right?
随机推荐
特征值和特征向量
QT implementation disable shortcut key
【图像隐藏】基于DCT、DWT、LHA、LSB的数字图像信息隐藏系统含各类攻击和性能参数附matlab代码
树的孩子兄弟表示法
初识C语言 -- 结构体,分支和循环语句
MySQL数据库InnoDB存储引擎中的锁机制(荣耀典藏版)
【OpenGL】GLES20.glClear
Special network technology virtual host PHP version setting
Red hat official announced the new president and CEO! Paul Cormier, a key figure in transformation, is "retiring"
【自我成长网站收集】
Sword finger offer special assault edition day 12
Use try-with-resources or close this
关于Sqli-labs单引号不报错的问题
【HCIP】BGP 基础
MySQL's way to solve deadlock - lock analysis of common SQL statements
Should programmers choose outsourcing companies
MySQL blocking monitoring script
Email security report in the second quarter: email attacks have soared fourfold, and well-known brands have been used to gain trust
基于FPGA的64位8级流水线加法器
How MySQL uses indexes (glory Collection Edition)