Basic android control learning ----- ProgressBar, basic android Control

Source: Internet
Author: User

Basic android control learning ----- ProgressBar, basic android Control

ProgressBar

I. common attributes and basic use instances

(1) common attributes:

Android: max: maximum value of the progress bar

Android: progress bar completed

Android: progressDrawable: Set the Drawable object corresponding to the track.

Android: indeterminate if set to true, the precision bar is not displayed accurately.

Android: indeterminateDrawable: sets the Drawable object that does not display the precision bar.

Android: indeterminateDuration: sets the duration of the inaccurate display progress bar.

Android: secondaryProgress: second-level progress bar. For example, the current playback progress and cache are used for video playback. The former is set through the progress attribute.

The above attributes also have corresponding methods in Java:

GetMax (): returns the maximum value of the range of the progress bar.

GetProgress (): returns the current progress

GetSecondaryProgress (): returns the secondary progress

IncrementProgressBy (int diff): Specifies the increase progress.

IsIndeterminate (): indicates whether the progress bar is in an uncertain mode.

SetIndeterminate (boolean indeterminate): sets the uncertain mode.

(2) progress bar instance provided by the system by default

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

Ii. Custom 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. * Custom circular progress bar */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 ();} /*** initialize related parameters */private void init () {// Background white brush mBackPaint = new Paint (); mBackPaint. setColor (Color. WHITE); mBackPaint. setAntiAlias (true); mBackPaint. setStyle (Paint. style. STROKE); mBackPaint. setStrokeWidth (mStrokeWidth); // green brush mFrontPaint = new Paint (); mFrontPaint. setColor (Color. GREEN); mFrontPaint. setAntiAlias (true); mFrontPaint. setStyle (Paint. style. STROKE); mFrontPaint. setStrokeWidth (mStrokeWidth); // number in the circle mTextPaint = new Paint (); mTextPaint. setColor (Color. GREEN); mTextPaint. setAntiAlias (true); mTextPaint. setTextSize (80); mTextPaint. setTextAlign (Paint. align. CENTER);}/*** override the onMeasure method of the measurement size and the onDraw () Core Method for drawing the View () * @ param widthMeasureSpec * @ param heightMeasureSpec */@ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec); mWidth = getRealSize (widthMeasureSpec); mHeight = getRealSize (heightMeasureSpec); average (mWidth, mHeight);} private int getRealSize (int measureSpec) {int result = 1; int mode = MeasureSpec. getMode (measureSpec); int size = MeasureSpec. getSize (measureSpec); // The following describes the three measurement modes if (mode = MeasureSpec. AT_MOST | mode = MeasureSpec. UNSPECIFIED) {result = (int) (mRadius * 2 + mStrokeWidth);} else {result = size;} return result ;}@ Override protected void onDraw (Canvas 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 );}}}

Use in layout files

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.