android 自定义进度条

自定义view,显示进度标点及进度数值,外进度框和内进度条。android 自定义进度条

自定义view类

package com.sample.util;
import
android.annotation.SuppressLint; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; import android.util.AttributeSet; import android.util.Log; import android.view.View; import com.gj.gateway.R; public class HProgressBar extends View { private final String TAG = "HProgressBar"; private int mProgress_outline_color = 0xFFFFFFFF;//外边框颜色 private int mProgress_color = 0xFFFFFFFF; //进度条颜色 private int mProgress_circle_color = 0xFFD1F4DE; //圆圈颜色 private int mProgress_text_color = 0xFFFFFFFF; //进度字体颜色 private int mProgress_text_bg_color = 0x50FFFFFF; //进度背景颜色 private float mProgress_circle_height = 56;// 56; //圆圈高度 private float mProgress_height = 40;// 25; //progress高度 private float mProgress_bar_height = 30; //progress进度条高度 private float mProgress_text_height = 35; //进度文字高度 private float mProgress_text_paddingH = 25; //进度文字左右padding private float mProgress_text_paddingV = 0; //进度文字上下pading private float mProgress_text_size = 32; //进度文字字体大小 private float mMaxProgress = 100; private volatile float mProgress_progress_bar = 0; private int mProgressWidth; private Paint mOutLinePaint; private Paint mProgressPaint; private Paint mCirClePaint; private Paint mTextPain; private Paint mTextBgPain; public HProgressBar(Context context) { super(context); Log.d(TAG, "HProgressBar: 1"); } public HProgressBar(Context context, AttributeSet attrs) { super(context, attrs); Log.d(TAG, "HProgressBar: 2"); initDefStyleAttr(attrs); mOutLinePaint = new Paint(); mOutLinePaint.setColor(mProgress_outline_color); mOutLinePaint.setAntiAlias(true); mOutLinePaint.setStrokeWidth(2); mOutLinePaint.setStyle(Paint.Style.STROKE); //设置空心 mProgressPaint = new Paint(); mProgressPaint.setColor(mProgress_color); mProgressPaint.setAntiAlias(true); mCirClePaint = new Paint(); mCirClePaint.setColor(mProgress_circle_color); mCirClePaint.setAntiAlias(true); mTextBgPain = new Paint(); mTextBgPain.setColor(mProgress_text_bg_color); mTextBgPain.setAntiAlias(true); mTextPain = new Paint(); mTextPain.setColor(mProgress_text_color); mTextPain.setAntiAlias(true); mTextPain.setTextSize(mProgress_text_size); mTextPain.setTextAlign(Paint.Align.CENTER); mTextPain.setTextSize(mProgress_text_size); } public HProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); Log.d(TAG, "HProgressBar: 3"); } public HProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); Log.d(TAG, "HProgressBar: 4"); } private void initDefStyleAttr(AttributeSet attrs) { final TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.HProgress); mProgress_outline_color = attributes.getColor(R.styleable.HProgress_hProgress_outline_color, mProgress_outline_color); mProgress_color = attributes.getColor(R.styleable.HProgress_hProgress_color, mProgress_color); mProgress_circle_color = attributes.getColor(R.styleable.HProgress_hProgress_circle_color, mProgress_circle_color); mProgress_text_color = attributes.getColor(R.styleable.HProgress_hProgress_text_color, mProgress_text_color); mProgress_text_bg_color = attributes.getColor(R.styleable.HProgress_hProgress_text_bg_color, mProgress_text_bg_color); mProgress_circle_height = (int) attributes.getDimension(R.styleable.HProgress_hProgress_circle_height, mProgress_circle_height); mProgress_height = (int) attributes.getDimension(R.styleable.HProgress_hProgress_height, mProgress_height); mProgress_bar_height = (int) attributes.getDimension(R.styleable.HProgress_hProgress_bar_height, mProgress_bar_height); mProgress_text_height = (int) attributes.getDimension(R.styleable.HProgress_hProgress_text_height, mProgress_text_height); mProgress_text_size = (int) attributes.getDimension(R.styleable.HProgress_hProgress_text_size, mProgress_text_size); mProgress_text_paddingH = (int) attributes.getDimension(R.styleable.HProgress_hProgress_text_paddingH, mProgress_text_paddingH); mProgress_text_paddingV = (int) attributes.getDimension(R.styleable.HProgress_hProgress_text_paddingV, mProgress_text_paddingV); mProgress_progress_bar = attributes.getInteger(R.styleable.HProgress_hProgress_progress_bar, (int)mProgress_progress_bar); mMaxProgress = attributes.getInteger(R.styleable.HProgress_hProgress_maxProgress, (int) mMaxProgress); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mProgressWidth = MeasureSpec.getSize(widthMeasureSpec); setMeasuredDimension(mProgressWidth, (int )(mProgress_text_height + mProgress_circle_height)); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Rect rect = new Rect(); mTextPain.getTextBounds(String.valueOf(getProgress()), 0, String.valueOf(getProgress()) .length(),rect); int w = rect.width(); int h = rect.height(); float spaceA = (mProgress_circle_height - mProgress_height) / 2; float spaceB = (mProgress_height - mProgress_bar_height) / 2; float progressMargin = mProgress_circle_height / 2; //长度变化范围等于控件长度减去两边的边距和空隙,由于是圆角进度条,还要减去内进度条的高度,即内进度条两边半圆的长度 float changeRange = mProgressWidth - (progressMargin + spaceB) * 2 - mProgress_bar_height; float startPoint = progressMargin + spaceB; float circleX = startPoint + mProgress_bar_height / 2 + changeRange * getProgress() / mMaxProgress; float textStartX; float textEndX; if (circleX - w / 2 - mProgress_text_paddingH < 0) { textStartX = 0; textEndX = textStartX + w + mProgress_text_paddingH * 2; } else if (circleX + w / 2 + mProgress_text_paddingH >= mProgressWidth) { textStartX = mProgressWidth - mProgress_text_paddingH * 2 - w; textEndX = mProgressWidth; } else { textStartX = circleX - w / 2 - mProgress_text_paddingH; textEndX = circleX + w / 2 + mProgress_text_paddingH; } //画数字背景框 RectF rectF2 = new RectF(textStartX, mProgress_circle_height, textEndX, mProgress_circle_height + mProgress_text_height); canvas.drawRoundRect(rectF2, mProgress_text_height / 2, mProgress_text_height / 2, mTextBgPain); //画数字进度 Paint.FontMetrics fontMetrics = mTextPain.getFontMetrics(); canvas.drawText((int)getProgress() + "", (textStartX + textEndX) / 2, mProgress_circle_height + mProgress_text_height / 2 + (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent, mTextPain); //画progressBar 外边 RectF rectF = new RectF(progressMargin, spaceA,mProgressWidth - progressMargin, mProgress_height + spaceA); canvas.drawRoundRect(rectF, mProgress_height / 2, mProgress_height / 2, mOutLinePaint); //画progressBar 内进度条 RectF rectF1 = new RectF(startPoint, spaceA + spaceB, startPoint + mProgress_bar_height + changeRange * getProgress()/mMaxProgress, mProgress_bar_height + spaceB + spaceA); canvas.drawRoundRect(rectF1, mProgress_bar_height / 2, mProgress_bar_height / 2, mProgressPaint); //画圆 canvas.drawCircle(circleX, mProgress_circle_height / 2, mProgress_circle_height / 2, mCirClePaint); } public int getProgress() { return (int)mProgress_progress_bar; } public void setProgress(int progress) { if (progress > mMaxProgress) { throw new RuntimeException("progress mast less than mMaxProgress"); } mProgress_progress_bar = progress; postInvalidate(); } public void setmMaxProgress(int maxProgress) { this.mMaxProgress = maxProgress; } }

资源文件中attrs.xml对应定义

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="HBLevelView">
        <attr name="startProgress" format="float|reference" />
        <attr name="scurrProgress" format="float|reference" />
        <attr name="endProgress" format="float|reference" />
        <attr name="progressTextSize" format="reference|dimension" />
        <attr name="backColor" format="color|reference" />
        <attr name="foreColor" format="color|reference" />
        <attr name="textColor" format="color|reference" />
        <attr name="rectCorn" format="dimension|reference" />
    </declare-styleable>
    <declare-styleable name="HProgress">
        <!--外边框颜色-->
        <attr name="hProgress_outline_color" format="color" />
        <!--进度条颜色-->
        <attr name="hProgress_color" format="color" />
        <!--圆圈颜色-->
        <attr name="hProgress_circle_color" format="color" />
        <!--进度字体颜色-->
        <attr name="hProgress_text_color" format="color" />
        <!--进度背景颜色-->
        <attr name="hProgress_text_bg_color" format="color" />
        <!--圆圈高度-->
        <attr name="hProgress_circle_height" format="dimension" />
        <!--progress高度-->
        <attr name="hProgress_height" format="dimension" />
        <!--progress进度条高度-->
        <attr name="hProgress_bar_height" format="dimension" />
        <!--进度文字高度-->
        <attr name="hProgress_text_height" format="dimension" />
        <!--进度文字字体大小-->
        <attr name="hProgress_text_size" format="dimension" />
        <!--进度文字横向padding-->
        <attr name="hProgress_text_paddingH" format="dimension" />
        <!--进度文字纵向padding-->
        <attr name="hProgress_text_paddingV" format="dimension" />
        <!--进度值-->
        <attr name="hProgress_progress_bar" format="integer" />
        <attr name="hProgress_maxProgress" format="integer" />
    </declare-styleable>
</resources>

如果想使用该进度条,可以将这两个文件移入到工程目录中,在布局文件中使用自定义类:

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal">
    <com.sample.util.HProgressBar
      android:id="@+id/hprogress_bar_register"
      android:layout_width="@dimen/d_885dp"
      android:layout_height="@dimen/d_40dp"
      android:layout_marginTop="@dimen/d_100dp"
          />
 </LinearLayout>

 操作控件

private HProgressBar mHProgressBar;
.... 
mHProgressBar = (HProgressBar)findViewById(R.id.hprogress_bar_register);
....
mHProgressBar.setProgress(100);

 

android 自定义进度条

上一篇:eclipse或Android studio项目逆向工程(难以实现时序图)


下一篇:迷宫问题的C语言求解