Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> android基本控件學習-----ProgressBar,android基本控件

android基本控件學習-----ProgressBar,android基本控件

編輯:關於android開發

android基本控件學習-----ProgressBar,android基本控件


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"
        />
    <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"
        />
    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:max="100"
        android:progress="20"
        />
    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:indeterminate="true"
        />
</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>

效果圖

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved