Android_自定义控件之喜马拉雅6.6.21.3播放进度条

本文介绍了如何实现喜马拉雅6.6.21.3版本的播放进度条,包括绘制思路、代码实现以及关键的起点和终点计算,提供了完整的Java代码示例和GitHub项目链接。
摘要由CSDN通过智能技术生成

1、前言

补一篇文章,github早已提交,文章迟迟未写,今天就奉上。
前段时间 Android开发正规群(324345614)有个群友在问怎么实现喜马拉雅6.6.21.3版本的播放进度条,网上搜了一圈并没有人写过类似的,群里其他人也没有人想帮他写一个,自告奋勇,开始撸一个。

2、编写代码

java为例子,kotlin代码在github 项目中有,文章后面会给出github地址

2.1、编写思路:

  • 绘制前测量
  • 背景条绘制
  • 缓冲条绘制
  • 播放进度框背景绘制
  • 播放进度文字绘制
  • 进度框计算移动过程的中心点
  • 触摸调整进度

2.2、开始写代码了

在这里插入图片描述
这里涉及到一点,进度框的宽度是比较长的。不能像那些小圆点进度那样可以忽略不计,
所以在播放过程中,这个进度框的中心点会一直往右移,起点的计算就尤其重要了。
起点计算:应该是要根据当前的进度和总进度还有总宽度计算出距左边的距离,这个距离得出来的还不是最终的,因为进度框有很大宽度的,不然会在播放结束后跑出右边。所以还要根据当前进度占比总进度得出的比例算出进度框占比大小,然后再用前面计算得到的距离减去这个占比
终点计算:起点+进度框宽度

float textBgStartX = (float) progress * (progressBarRealWidth) / maxProgress - (float) textBgWidth / maxProgress * progress + dia;
        float textBgEndX = textBgStartX + textBgWidth;
        Log.e(TAG, "width " + width + ", textBgWidth " + textBgWidth + ", textBgStartX " + textBgStartX);
        canvas.drawRoundRect(textBgStartX
                , (height >> 1) - (textBgHeight >> 1)
                , textBgEndX
                , (height >> 1) + (textBgHeight >> 1)
                , textBgWidth >> 1 //x半径
                , textBgWidth >> 1 //y半径
                , textBgPaint);

CustomSeekBar.java完整代码

package com.kincai.customseekbar;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.ColorRes;
import androidx.core.content.ContextCompat;

import java.util.Locale;

/**
 * Created by KINCAI
 * <p>
 * Desc 自定义SeekBar 资源配置属性暂未实现
 * <p>
 * Date 2019-10-28 16:38
 */
public class CustomSeekBar extends View {
   
    private final String TAG = this.getClass().getSimpleName();
    private int seekBarDefaultWidth;
    private int textBgHeight;
    private int textBgWidth;
    private int cacheProgressBarHeight;
    private int progressBarHeight;
    private int cacheProgressBarColor;
    private int progressBarColor;
    private int textColor;
    private int textBgColor;
    private int maxProgress;
    private int progress;
    private int cacheProgress;
    private int minCircleRadius;
    private int textSize;

    private Paint cacheProgressBarPaint = new Paint();
    private Paint progressBarPaint = new Paint();
    private Paint textPaint = new Paint();
    private Paint textBgPaint = new Paint();
    /**小圆点直径*/
    private float dia;
    private int textHeight;

    public CustomSeekBar(Context context) {
   
        this(context, null);
    }

    public CustomSeekBar(Context context, AttributeSet attrs) {
   
        this(context, attrs, 0);
    }

    public CustomSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
   
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
   
        // 默认宽度为屏幕宽
        Resources resources = this.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        seekBarDefaultWidth = dm.widthPixels;
        textBgWidth = dp2px(200);
        textBgHeight = dp2px(16);
        cacheProgressBarHeight = dp2px(1.5f);
        progressBarHeight = dp2px(1f);
        setCircleParam();
        textSize = sp2px(10);

        progressBarColor = Color.parseColor("#d9ead3");
        cacheProgressBarColor = Color.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值