android基本控制項學習-----ProgressBar,android基本控制項

來源:互聯網
上載者:User

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"        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>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.