短視訊開發app,自定義帶進度條的視訊播放按鈕
短視訊開發app,自定義帶進度條的視訊播放按鈕
具體的程式碼邏輯都有清楚的註釋,可以直接檢視程式碼
public class MusicProgress extends View { private Context mContext; private static final int DEFAULT_SIZE = 40; private static final float HALF = 0.5f; private static final float ONE_TENTH = 0.1f; private static final float DEFAULT_START_ANGLE = -90; private int defaultSize; private int mWidth; private int mHeight; private Paint mBgPaint; private Paint mProgressPaint; // 背景顏色和進度條顏色 private int mBgColor; private int mProgressColor; // 背景圓 private int radius; private int circlePoint; // 播放暫停按鈕 private Bitmap mPlayBm; private Bitmap mPauseBm; private Bitmap mBitmap; private Rect mDstRect; private int mBmOffset; // 進度條 private RectF mProgressRectF; private int mProgressWidth; // 進度條開始結束角度 private float mStartAngle; private float mSweepAngle; public MusicProgress(Context context) { this(context, null); } public MusicProgress(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public MusicProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.mContext = context; init(); } private void init() { defaultSize = dp2px(DEFAULT_SIZE); mProgressWidth = (int) (defaultSize * ONE_TENTH); radius = (int) (defaultSize * HALF - mProgressWidth * HALF); circlePoint = (int) (defaultSize * HALF); mBgColor = ContextCompat.getColor(mContext, R.color.progress_bg_color); mProgressColor = ContextCompat.getColor(mContext, R.color.progress_color); // 暫停、播放按鈕的繪製區域 mBmOffset = (int) (defaultSize * HALF * HALF); mDstRect = new Rect(mBmOffset, mBmOffset, defaultSize - mBmOffset, defaultSize - mBmOffset); mPlayBm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.play); mPauseBm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.pause); // 預設是暫停狀態 mBitmap = mPlayBm; mBgPaint = new Paint(); mBgPaint.setAntiAlias(true); mBgPaint.setColor(mBgColor); mProgressPaint = new Paint(); mProgressPaint.setAntiAlias(true); mProgressPaint.setColor(mProgressColor); mProgressPaint.setStrokeWidth(mProgressWidth); mProgressPaint.setStyle(Paint.Style.STROKE); // 進度條 mProgressRectF = new RectF(0, 0, defaultSize, defaultSize); this.mStartAngle = DEFAULT_START_ANGLE; } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); this.mWidth = w; this.mHeight = h; if (mWidth > 0) { // 修改進度條寬度 mProgressWidth = (int) (mWidth * ONE_TENTH); mProgressPaint.setStrokeWidth(mProgressWidth); // 修改背景圓的半徑 radius = (int) (mWidth * HALF - mProgressWidth * HALF); // 修改圓心 circlePoint = (int) (mWidth * HALF); // 暫停、播放按鈕的繪製區域 mBmOffset = (int) (mWidth * HALF * HALF); mDstRect = new Rect(mBmOffset, mBmOffset, mWidth - mBmOffset, mWidth - mBmOffset); float size = mWidth - mProgressWidth * HALF; // 進度條 mProgressRectF = new RectF(mProgressWidth * HALF, mProgressWidth * HALF, size, size); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(getSize(widthMeasureSpec), getSize(heightMeasureSpec)); } /** * 獲取測量大小 */ private int getSize(int measureSpec) { int result; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { //確切大小,所以將得到的尺寸給view result = specSize; } else if (specMode == MeasureSpec.AT_MOST) { //預設值為40dp, 此處要結合父控制元件給子控制元件的最多大小(要不然會填充父控制元件),所以採用最小值 result = Math.min(defaultSize, specSize); } else { result = defaultSize; } return result; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 繪製背景圓 canvas.drawCircle(circlePoint, circlePoint, radius, mBgPaint); // 繪製播放暫停按鈕 canvas.drawBitmap(mBitmap, null, mDstRect, mBgPaint); // 繪製進度條 canvas.drawArc(mProgressRectF, mStartAngle, mSweepAngle, false, mProgressPaint); } /** * 重新整理播放UI. */ public void play() { mBitmap = mPauseBm; invalidate(); } /** * 重新整理暫停UI. */ public void pause() { mBitmap = mPlayBm; invalidate(); } /** * 更新進度. * * @param startAngle 開始角度 * @param sweepAngle 掃過角度 */ public void updateProgress(@FloatRange(from = -360f, to = 360f) float startAngle, @FloatRange(from = 0f, to = 360f) float sweepAngle) { this.mStartAngle = startAngle; this.mSweepAngle = sweepAngle; invalidate(); } /** * 更新進度. * * @param sweepAngle 掃過角度 */ public void updateProgress(@FloatRange(from = 0f, to = 360f) float sweepAngle) { this.mSweepAngle = sweepAngle; invalidate(); } private int dp2px(final float dp) { final float density = mContext.getResources().getDisplayMetrics().density; return (int) (dp * density + 0.5f); } private int px2dp(final float px) { float scale = mContext.getResources().getDisplayMetrics().scaledDensity; return (int) (px / scale + 0.5f); } }
涉及的資原始檔
<color name="progress_bg_color">#7A7A7A</color> <color name="progress_color">#00F1B7</color>
以上就是短視訊開發app,自定義帶進度條的視訊播放按鈕, 更多內容歡迎關注之後的文章
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2901638/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 短視訊app開發,點選視訊進行全屏播放APP
- 短視訊app開發,更改進度條的滑動速度APP
- 短視訊平臺搭建,Android自定義旋轉進度條Android
- 短視訊app開發,長按將視訊儲存到相簿APP
- 短視訊開發app,webservice自定義加入攔截器APPWeb
- 短視訊直播原始碼,自定義圖片或視訊的迴圈播放原始碼
- 日常總結 --- 視訊播放按鈕
- Android進階:自定義視訊播放器開發(上)Android播放器
- Android進階:自定義視訊播放器開發(下)Android播放器
- 短視訊商城系統,Android進度條,自定義進度條,顯示百分比Android
- 短視訊app開發,內部標題進行滾動播放APP
- 自定義視訊播放器播放器
- 短視訊程式開發,RecyclerView自帶的滾動條View
- android短視訊開發,自定義下拉選單Android
- 短視訊系統原始碼,實現按鈕開啟關閉,顏色可自定義原始碼
- 直播短視訊原始碼短視訊APP短視訊帶貨系統多商戶直播原始碼APP
- 短視訊APP開發帶飛你的創業夢APP創業
- 短視訊開發,點選按鈕Button,更換背景顏色
- 短視訊app開發之ios小視訊開發經驗共享APPiOS
- 短視訊平臺搭建,自定義滾動條的樣式
- 短視訊app原始碼,提示以按鈕彈窗的形式實現APP原始碼
- Cordova開發app——短視訊拍攝APP
- 短視訊直播系統開發直播短視訊程式搭建短視訊互動直播
- Android進階:十、自定義視訊播放器 1Android播放器
- 短視訊軟體開發,按鈕側滑顯示各個選項
- android短視訊開發,點選分享按鈕生成分享連結Android
- iOS開發:音訊播放、錄音、視訊播放、拍照、視訊錄製iOS音訊
- Android開發 海康威視 多路視訊播放(同時播放視訊)Android
- video自定義實現視訊播放功能IDE
- 短視訊平臺原始碼,長按視訊、對話方塊彈出操作可選按鈕原始碼
- 短視訊系統,長按側滑實現刪除的按鈕
- 短視訊商城在ios短視訊開發上的應用效果iOS
- 短視訊帶貨原始碼,android 自定義常駐通知欄原始碼Android
- 短視訊程式開發,簡易的自定義確認彈框AlertDialog
- 自定義相機採集及視訊編輯(1)-短視訊錄製
- iOS開發 AVFoundation 自定義視訊錄製iOS
- iOS開發系列--音訊播放、錄音、視訊播放、拍照、視訊錄製(轉)iOS音訊
- iOS 基於AVPlayer自定義視訊播放器iOS播放器