android加载视频进度条,android自定义进度条,类似微信视频加载

要实现的效果如下

00ae0e875171?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

效果.png

00ae0e875171?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

效果.gif

简单的步骤利用canvas画三个不同大小的圆,相互叠加,显示成为进度条,然后利用Animation重写applyTransformation来控制进度条。

下面的代码

public class LodingCircleView extends View {

private Paint paintBgCircle;

private Paint paintCircle;

private Paint paintProgressCircle;

private float startAngle = -90f;//开始角度

private float sweepAngle = 0;//结束

private int progressCirclePadding = 0;//进度圆与背景圆的间距

private boolean fillIn = false;//进度圆是否填充

private int animDuration = 2000;

private LodingCircleViewAnim mLodingCircleViewAnim;//动画效果

public LodingCircleView(Context context) {

super(context);

init();

}

public LodingCircleView(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

public LodingCircleView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init();

}

public int dip2px(Context context, float dpValue) {

final float scale = context.getResources().getDisplayMetrics().density;

return (int) (dpValue * scale + 0.5f);

}

private void init() {

mLodingCircleViewAnim = new LodingCircleViewAnim();

mLodingCircleViewAnim.setDuration(animDuration);

progressCirclePadding = dip2px(getContext(), 3);

paintBgCircle = new Paint();

paintBgCircle.setAntiAlias(true);

paintBgCircle.setStyle(Paint.Style.FILL);

paintBgCircle.setColor(Color.WHITE);

paintCircle = new Paint();

paintCircle.setAntiAlias(true);

paintCircle.setStyle(Paint.Style.FILL);

paintCircle.setColor(Color.BLACK);

paintProgressCircle = new Paint();

paintProgressCircle.setAntiAlias(true);

paintProgressCircle.setStyle(Paint.Style.FILL);

paintProgressCircle.setColor(Color.WHITE);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredWidth() / 2, getMeasuredWidth() / 2, paintBgCircle);

canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredWidth() / 2, getMeasuredWidth() / 2 - progressCirclePadding / 2, paintCircle);

RectF f = new RectF(progressCirclePadding, progressCirclePadding, getMeasuredWidth() - progressCirclePadding, getMeasuredWidth() - progressCirclePadding);

canvas.drawArc(f, startAngle, sweepAngle, true, paintProgressCircle);

if (!fillIn)

canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredWidth() / 2, getMeasuredWidth() / 2 - progressCirclePadding * 2, paintCircle);

}

public void startAnimAutomatic(boolean fillIn) {

this.fillIn = fillIn;

if (mLodingCircleViewAnim != null)

clearAnimation();

startAnimation(mLodingCircleViewAnim);

}

public void stopAnimAutomatic() {

if (mLodingCircleViewAnim != null)

clearAnimation();

}

public void setProgerss(int progerss, boolean fillIn) {

this.fillIn = fillIn;

sweepAngle = (float) (360 / 100.0 * progerss);

invalidate();

}

private class LodingCircleViewAnim extends Animation {

@Override

protected void applyTransformation(float interpolatedTime, Transformation t) {

super.applyTransformation(interpolatedTime, t);

if (interpolatedTime < 1.0f) {

sweepAngle = 360 * interpolatedTime;

invalidate();

} else {

startAnimAutomatic(fillIn);

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想要实现自定义视频播放进度条和暂停/播放按钮,可以按照以下步骤进行: 1. 在布局文件中添加 VideoView、SeekBar 和暂停/播放按钮: ``` <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> <SeekBar android:id="@+id/seek_bar" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/play_pause_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pause" /> ``` 2. 在 Activity 中获取 VideoView、SeekBar 和暂停/播放按钮的引用: ``` VideoView videoView = findViewById(R.id.video_view); SeekBar seekBar = findViewById(R.id.seek_bar); Button playPauseButton = findViewById(R.id.play_pause_button); ``` 3. 设置 VideoView 的路径并开始播放: ``` videoView.setVideoPath("path/to/video.mp4"); videoView.start(); ``` 4. 为 VideoView 添加一个 OnPreparedListener,当视频准备好时,设置 SeekBar 的最大值和添加一个定时器来更新 SeekBar 的进度: ``` videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { seekBar.setMax(videoView.getDuration()); final Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { seekBar.setProgress(videoView.getCurrentPosition()); handler.postDelayed(this, 1000); } }; handler.postDelayed(runnable, 1000); } }); ``` 5. 为 SeekBar 添加一个 OnSeekBarChangeListener,当拖动 SeekBar 时,改变视频播放的位置: ``` seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (fromUser) { videoView.seekTo(progress); } } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }); ``` 6. 为暂停/播放按钮添加一个 OnClickListener,当点击按钮时,暂停或继续播放视频: ``` playPauseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (videoView.isPlaying()) { videoView.pause(); playPauseButton.setText("Play"); } else { videoView.start(); playPauseButton.setText("Pause"); } } }); ``` 7. 为 VideoView 添加一个 OnCompletionListener,当视频播放完成时,将暂停/播放按钮的文本设置为“Play”: ``` videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { playPauseButton.setText("Play"); } }); ``` 以上就是实现自定义视频播放进度条和暂停/播放按钮的方法。您可以根据您的需求自定义 SeekBar 的样式和暂停/播放按钮的图标。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值