最简单的自定义视频播放进度条

前言:之前有个需求是做一个播放的进度条,于是我在网上查找了一些文章,发现最终还是出入较大,没办法,还是自己写吧。

先贴2张效果图:
这里写图片描述

这里写图片描述

当然了,我们产品的要求可不是这么简单,得是进度条的左上角显示播放视频的名称,右上角显示播放的总时长。但实现到这一步了,其他那2个需求就太简单了。

难点嘛,你们看了设计图可能觉得没难点,但是当时对我来说就有一个难点,那就是绘制的字体在提示框的中间,查询了一些文章才做成最终效果。

绘制文字的代码如下:

 private void drawProgressText2(Canvas canvas){
        textRect.left = (int) moveDistance;
        textRect.top = paintWidth+progressMarginTop+triangleHeight;
        textRect.right = (int) (tipWidth + moveDistance);
        textRect.bottom = tipHeight+paintWidth+progressMarginTop+triangleHeight;
        Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
        int baseline = (textRect.bottom + textRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
        //文字绘制到提示框的中心位置
        canvas.drawText(progressText, textRect.centerX(), baseline, textPaint);
    }

其中:moveDistance 表示提示框的最左边的值;paintWidth表示绘制进度条的宽度,progressMarginTop 表示 整个提示框距离进度条的距离,triangleHeight表示绘制小三角指示的高度;tipWidth 表示提示框的宽度啦。还有一点设置绘制文字的piant d的setTextAlign(Paint.Align.CENTER),这样文字就居中显示啦。

然后就是绘制小三角,代码如下:

 private void drawTriangle2(Canvas canvas) {
        path.moveTo(moveDistance + tipWidth / 2, paintWidth+progressMarginTop);
        path.lineTo(moveDistance + tipWidth / 2-triangleHeight/2, paintWidth+progressMarginTop+triangleHeight);
        path.lineTo(moveDistance + tipWidth / 2+triangleHeight/2, paintWidth+progressMarginTop+triangleHeight);
        canvas.drawPath(path, tipPaint);
        path.reset();
    }

这个就没啥好说的,就是先设定三角顶尖的位置,然后分别向左、右绘制一条直线,设置paint 的 style 为 Paint.Style.FILL就填满了小三角。

然后是绘制圆角矩形,有现成的api调用:

private void drawRoundRect2(Canvas canvas) {
        rectF.set(moveDistance, paintWidth+progressMarginTop+triangleHeight, tipWidth + moveDistance, paintWidth+progressMarginTop+triangleHeight+tipHeight);
        canvas.drawRoundRect(rectF, roundRectRadius, roundRectRadius, tipPaint);
    }

这个很简单就不BB了,最后是画进度条,先画一个背景,然后画进度。

 private void drawProgress2(Canvas canvas){
        canvas.drawLine(getPaddingLeft(), 0, getWidth(), 0, bgPaint);
        canvas.drawLine(getPaddingLeft(), 0, currentProgress, 0, progressPaint);
    }

然后就是模拟动画了:

private void  initAnimation(){
        progressAnimator = ValueAnimator.ofFloat(0, mProgress);
        progressAnimator.setDuration(10*1000);
        progressAnimator.setStartDelay(500);
        progressAnimator.setInterpolator(new LinearInterpolator());

        progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float value = (float) valueAnimator.getAnimatedValue();
//                progressText = String.valueOf((int)value);
                progressText = getTime((int)value);

                //把当前百分比进度转化成view宽度对应的比例
                currentProgress = value * mViewWidth / mProgress;
                //移动进度提示框,只有当前进度到提示框中间位置之后开始移动,
                //当进度框移动到最右边的时候停止移动,但是进度条还可以继续移动
                //moveDis是tip框移动的距离
                if (currentProgress >= (tipWidth / 2) &&
                        currentProgress <= (mViewWidth - tipWidth / 2)) {
                    moveDistance = currentProgress - tipWidth / 2;
                }
                invalidate();
            }
        });

        progressAnimator.start();
    }

这里面都解释清楚了,最后记得设置 mProgress 这个变量额值:

/**
     * 设置最大进度
     * @param progress
     */
    public void setProgress(int progress){
        mProgress = progress;
        initAnimation();
    }

好了,就说到这里了,有问题欢迎指正。

以下是一个简单视频播放网页的HTML、CSS和JavaScript代码: HTML代码: ``` <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>视频播放网页</title> <style> video { width: 800px; height: 450px; } </style> </head> <body> <video id="myVideo" controls> <source src="myVideo.mp4" type="video/mp4"> <source src="myVideo.ogg" type="video/ogg"> <source src="myVideo.webm" type="video/webm"> Your browser does not support HTML5 video. </video> <br> <button onclick="playPause()">播放/暂停</button> <button onclick="makeBig()">放大</button> <button onclick="makeSmall()">缩小</button> <button onclick="makeNormal()">恢复</button> <br> <input type="range" id="volumeBar" min="0" max="1" step="0.1" value="1" onchange="setVolume()"> <button onclick="setSpeed(0.5)">0.5x</button> <button onclick="setSpeed(1)">1x</button> <button onclick="setSpeed(1.5)">1.5x</button> <button onclick="setSpeed(2)">2x</button> <br> <button onclick="fullScreen()">全屏</button> <button onclick="exitFullScreen()">退出全屏</button> <script> var video = document.getElementById("myVideo"); function playPause() { if (video.paused) { video.play(); } else { video.pause(); } } function makeBig() { video.width = 1200; video.height = 675; } function makeSmall() { video.width = 400; video.height = 225; } function makeNormal() { video.width = 800; video.height = 450; } function setVolume() { video.volume = document.getElementById("volumeBar").value; } function setSpeed(speed) { video.playbackRate = speed; } function fullScreen() { if (video.requestFullscreen) { video.requestFullscreen(); } else if (video.mozRequestFullScreen) { video.mozRequestFullScreen(); } else if (video.webkitRequestFullscreen) { video.webkitRequestFullscreen(); } else if (video.msRequestFullscreen) { video.msRequestFullscreen(); } } function exitFullScreen() { if (document.exitFullscreen) { document.exitFullscreen(); } else if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if (document.webkitExitFullscreen) { document.webkitExitFullscreen(); } else if (document.msExitFullscreen) { document.msExitFullscreen(); } } </script> </body> </html> ``` CSS代码: ``` video { width: 800px; height: 450px; } ``` JavaScript代码: ``` var video = document.getElementById("myVideo"); function playPause() { if (video.paused) { video.play(); } else { video.pause(); } } function makeBig() { video.width = 1200; video.height = 675; } function makeSmall() { video.width = 400; video.height = 225; } function makeNormal() { video.width = 800; video.height = 450; } function setVolume() { video.volume = document.getElementById("volumeBar").value; } function setSpeed(speed) { video.playbackRate = speed; } function fullScreen() { if (video.requestFullscreen) { video.requestFullscreen(); } else if (video.mozRequestFullScreen) { video.mozRequestFullScreen(); } else if (video.webkitRequestFullscreen) { video.webkitRequestFullscreen(); } else if (video.msRequestFullscreen) { video.msRequestFullscreen(); } } function exitFullScreen() { if (document.exitFullscreen) { document.exitFullscreen(); } else if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if (document.webkitExitFullscreen) { document.webkitExitFullscreen(); } else if (document.msExitFullscreen) { document.msExitFullscreen(); } } ``` 该网页包括一个video标签,用于显示视频。该标签带有controls属性,以便用户可以对视频进行控制。 视频的源文件在source标签中指定,可以指定多个源,以便确保浏览器可以在不同的格式中选择最合适的一个。 网页中的按钮和输入框可以让用户对视频进行控制。JavaScript代码包括了一些函数,用于在用户点击按钮或滑动输入框时执行相应的操作,如播放、暂停、调整音量、调整播放速度、全屏等。 注意,全屏功能需要使用不同的API调用,具体取决于浏览器。在这个例子中,我们使用了四种不同的API调用,以便兼容不同的浏览器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值