当前位置: 代码迷 >> Android >> android根本控件学习-ProgressBar
  详细解决方案

android根本控件学习-ProgressBar

热度:502   发布时间:2016-04-24 11:54:43.0
android基本控件学习-----ProgressBar

ProgressBar(进度条)讲解

一、常用属性和基础使用实例

(1)常用属性:

android:max:进度条的最大值

android:progress:进度条已完成进度值

android:progressDrawable:设置轨道对应的Drawable对象

android:indeterminate如果设置为true,则精度条不精确的显示

android:indeterminateDrawable:设置不显示精度条的Drawable对象

android:indeterminateDuration:设置不精确显示进度条的持续时间

android:secondaryProgress:二级进度条,类似视频播放的一条是当前的播放进度,一条是缓存条,前者通过progress属性进行设置。

上面的属性同样在Java中也有相对应得方法:

getMax():返回这个进度条的范围的最大值

getProgress():返回当前的进度

getSecondaryProgress():返回次要的进度

incrementProgressBy(int diff):指定增加的进度

isIndeterminate():指示进度条是否在不确定的模式下

setIndeterminate(boolean indeterminate):设置不确定模式下

(2)系统默认提供的进度条实例

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <ProgressBar        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        style="@android:style/Widget.ProgressBar.Small"/>    <ProgressBar        android:layout_width="match_parent"        android:layout_marginTop="20dp"        android:layout_height="wrap_content"/>    <ProgressBar        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        style="@android:style/Widget.ProgressBar.Large"/>    <ProgressBar        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        android:max="100"        android:progress="20"        style="@android:style/Widget.ProgressBar.Horizontal"/>    <ProgressBar        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        android:indeterminate="true"        style="@android:style/Widget.ProgressBar.Horizontal"/></LinearLayout>

二、自定义ProgressBar

package com.example.test3;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.util.AttributeSet;import android.view.View;/** * Created by coder-tu on 2016/1/11. * 自定义圆形进度条 */public class CirclePgBar extends View {    private Paint mBackPaint;    private Paint mFrontPaint;    private Paint mTextPaint;    private float mStrokeWidth = 50;    private float mHalfStrokeWidth = mStrokeWidth /2;    private float mRadius = 200;    private RectF mRect;    private int mProgress = 0;    private int mTargetProgress = 90;    private int mMax = 100;    private int mWidth;    private int mHeight;    public CirclePgBar(Context context) {        super(context);        init();    }    public CirclePgBar(Context context, AttributeSet attrs) {        super(context, attrs);        init();    }    public CirclePgBar(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }    /**     * 初始化相关参数     */    private void init() {//        背景白色画笔        mBackPaint = new Paint();        mBackPaint.setColor(Color.WHITE);        mBackPaint.setAntiAlias(true);        mBackPaint.setStyle(Paint.Style.STROKE);        mBackPaint.setStrokeWidth(mStrokeWidth);//        绿色画笔        mFrontPaint = new Paint();        mFrontPaint.setColor(Color.GREEN);        mFrontPaint.setAntiAlias(true);        mFrontPaint.setStyle(Paint.Style.STROKE);        mFrontPaint.setStrokeWidth(mStrokeWidth);//        圆圈里面的数字        mTextPaint = new Paint();        mTextPaint.setColor(Color.GREEN);        mTextPaint.setAntiAlias(true);        mTextPaint.setTextSize(80);        mTextPaint.setTextAlign(Paint.Align.CENTER);    }    /**     * 重写测量大小的onMeasure方法和绘制View的核心方法onDraw()     * @param widthMeasureSpec     * @param heightMeasureSpec     */    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        mWidth = getRealSize(widthMeasureSpec);        mHeight = getRealSize(heightMeasureSpec);        setMeasuredDimension(mWidth,mHeight);    }    private int getRealSize(int measureSpec) {        int result = 1;        int mode = MeasureSpec.getMode(measureSpec);        int size = MeasureSpec.getSize(measureSpec);//        后面会对三种测量模式进行详细讲解        if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.UNSPECIFIED){            result = (int)(mRadius * 2 + mStrokeWidth);        }else{            result = size;        }        return result;    }    @Override    protected void onDraw(Canvas canvas) {        initRect();        float angle = mProgress / (float) mMax * 360;        canvas.drawCircle(mWidth/2,mHeight/2,mRadius,mBackPaint);        canvas.drawArc(mRect,-90,angle,false,mFrontPaint);        canvas.drawText(mProgress + "%",mWidth/2 + mHalfStrokeWidth,mHeight/2 +mHalfStrokeWidth,mTextPaint);        if(mProgress < mTargetProgress){            mProgress += 1;            invalidate();        }    }    private void initRect() {        if (mRect == null){            mRect = new RectF();            int viewSize = (int)(mRadius * 2);            int left = (mWidth - viewSize)/2;            int top = (mHeight - viewSize)/2;            int right = left + viewSize;            int bottom = top + viewSize;            mRect.set(left,top,right,bottom);        }    }}

在布局文件中使用

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <com.example.test3.CirclePgBar        android:layout_width="match_parent"        android:layout_height="match_parent"/></LinearLayout>

效果图

 

  相关解决方案