简单的自定义水平进度条

本文详细介绍了如何在Android平台上实现视频进度条的功能,并通过实例展示了其在视频播放中的应用。主要内容包括进度条的样式定制、最大进度设置、当前进度更新、颜色与字体配置等关键属性的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * 
 */
package com.example.horview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/*
 * 类名      HorProgressBar.java
 * 说明   description of the class
 * 创建日期 2015-2-8
 * 作者 ou
 */

public class HorProgressBar extends View {
	  /** 
     * 画笔对象的引用 
     */  
    private Paint paint;  
    /** 
     * 画笔对象的引用 
     */  
    private Paint paint2;  
      
    /** 
     * 圆环的颜色 
     */  
    private int roundColor;  
      
    /** 
     * 圆环进度的颜色 
     */  
    private int roundProgressColor;  
      
    /** 
     * 中间进度百分比的字符串的颜色 
     */  
    private int textColor;  
      
    /** 
     * 中间进度百分比的字符串的字体 
     */  
    private float textSize;  
      
    /** 
     * 圆环的宽度 
     */  
    private float roundWidth;  
      
    /** 
     * 最大进度 
     */  
    private int max;  
      
    /** 
     * 当前进度 
     */  
    private int progress;  
    /** 
     * 是否显示中间的进度 
     */  
    private boolean textIsDisplayable;  
      
    /** 
     * 进度的风格,实心或者空心 
     */  
    private int style;  
      
    public static final int STROKE = 0;  
    public static final int FILL = 1;  
      
    public HorProgressBar(Context context) {  
        this(context, null);  
    }  
  
    public HorProgressBar(Context context, AttributeSet attrs) {  
        this(context, attrs, 0);  
    }  
      
    public HorProgressBar(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);  
          
        paint = new Paint();  
        paint2 = new Paint();  
  
          
    }  
      
  
    @Override  
    protected void onDraw(Canvas canvas) {  
        super.onDraw(canvas);  
          
        /** 
         * 画底下的线!
         */  
        paint.setColor(Color.BLUE); //设置线的颜色!
        paint.setStyle(Style.STROKE);//设置非填充
        paint.setStrokeWidth(5);//笔宽5像素
        paint.setAntiAlias(true);  //消除锯齿   
        canvas.drawLine(0, 0, getWidth(), 0, paint);
          
        /** 
         * 画第2条线
         */  
        paint2.setColor(Color.RED); //设置线的颜色!
        paint2.setStyle(Style.STROKE);//设置非填充
        paint2.setStrokeWidth(5);//笔宽5像素
        paint2.setAntiAlias(true);  //消除锯齿   
        if(progress !=0) {
        	 canvas.drawLine(0, 0,progress*getWidth()/getWidth(), 0, paint2);
        }
          
        switch (style) {  
        case STROKE:{  
            paint.setStyle(Paint.Style.STROKE);  
            break;  
        }  
        case FILL:{  
            paint.setStyle(Paint.Style.FILL_AND_STROKE);  
            if(progress !=0)  
            break;  
        }  
        }  
          
    }  
      
      
    public synchronized int getMax() {  
        return max;  
    }  
  
    /** 
     * 设置进度的最大值 
     * @param max 
     */  
    public synchronized void setMax(int max) {  
        if(max < 0){  
            throw new IllegalArgumentException("max not less than 0");  
        }  
        this.max = max;  
    }  
  
    /** 
     * 获取进度.需要同步 
     * @return 
     */  
    public synchronized int getProgress() {  
        return progress;  
    }  
  
    /** 
     * 设置进度,此为线程安全控件,由于考虑多线的问题,需要同步 
     * 刷新界面调用postInvalidate()能在非UI线程刷新 
     * @param progress 
     */  
    public synchronized void setProgress(int progress) {  
        if(progress < 0){  
            throw new IllegalArgumentException("progress not less than 0");  
        }  
            this.progress = progress;  
            postInvalidate();  //这个是可以异步调用ondraw的方法的!
          
    }  
      
      
    public int getCricleColor() {  
        return roundColor;  
    }  
  
    public void setCricleColor(int cricleColor) {  
        this.roundColor = cricleColor;  
    }  
  
    public int getCricleProgressColor() {  
        return roundProgressColor;  
    }  
  
    public void setCricleProgressColor(int cricleProgressColor) {  
        this.roundProgressColor = cricleProgressColor;  
    }  
  
    public int getTextColor() {  
        return textColor;  
    }  
  
    public void setTextColor(int textColor) {  
        this.textColor = textColor;  
    }  
  
    public float getTextSize() {  
        return textSize;  
    }  
  
    public void setTextSize(float textSize) {  
        this.textSize = textSize;  
    }  
  
    public float getRoundWidth() {  
        return roundWidth;  
    }  
  
    public void setRoundWidth(float roundWidth) {  
        this.roundWidth = roundWidth;  
    }  
  
  
}主要是在做视频进度条的时候用到了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值