UI控件之ProgressBar(进度条)

(一)概述

(二)常用属性与基础实例

从官方的API我们可以看到这样一个类的关系图:

常用属性详解 :

对应在java我们可以调用下述方法:

先看看系统给我们提供的进度条吧

运行效果如下:

实现代码:

xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.android_progressbar.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="小圆形进度条" />

    <ProgressBar
        style="?android:attr/progressBarStyleSmallTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ProgressBar>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中型圆形进度条" />

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ProgressBar>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="大型圆形进度条" />

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ProgressBar>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="水平进度条" />

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30" >
    </ProgressBar>

    <ProgressBar
        android:id="@+id/progressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:max="100"
        android:progress="30"
        android:secondaryProgress="60" >
    </ProgressBar>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="增加进度" >
        </Button>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="减小进度" >
        </Button>
    </LinearLayout>

</LinearLayout>

java文件

public class MainActivity extends Activity implements OnClickListener{

    private ProgressBar progressBar;
    private Button button1 ,button2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       //如何设置窗口有刻度的效果
        requestWindowFeature(Window.FEATURE_PROGRESS);//设置手机屏幕最上面的进度条
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);//设置ActionBar中最右边的进度条

        setContentView(R.layout.activity_main);
        progressBar = (ProgressBar) findViewById(R.id.progressbar);
        setProgressBarVisibility(true); //激活进度条
        setProgressBarIndeterminate(true);//设置进度条为不确定模式下
        setProgress(3500);

        button1 = (Button) this.findViewById(R.id.button1);
        button2 = (Button) this.findViewById(R.id.button2);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.button1:
            progressBar.setProgress((int)(progressBar.getProgress() * 1.2));
            progressBar.setSecondaryProgress((int)(progressBar.getSecondaryProgress()*1.2));

            break;

        case R.id.button2:

            progressBar.setProgress((int)(progressBar.getProgress() * 0.8));
            progressBar.setSecondaryProgress((int)(progressBar.getSecondaryProgress()*0.8));
            break;
        }
    }
}

代码比较简单,已经加入注释了额 ,我们实际开发中使用的进度条都不会这么用,因为—丑!那么问题来了又没有好的办法呢?有!

(三)使用动画来替代原始进度条

要写一个“菊花”进度条的话,首先,我们先定义一个AnimationDrawable文件:

图片资源戳这里:图片素材

运行效果图:

xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.android_progressbar.MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_marginTop="200dp"
        android:src="@drawable/anim_pgbar" />

</LinearLayout>

java代码:

public class MainActivity2 extends Activity {

    private ImageView img_pgbar;

    private AnimationDrawable ad;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main2);
        img_pgbar = (ImageView) findViewById(R.id.imageView1);
        ad = (AnimationDrawable) img_pgbar.getDrawable();
        img_pgbar.postDelayed( new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                ad.start();
            }
        }, 100);

    }

}

(四)自定义圆形进度条

其实,人家官方的api是这么说的:

 public class MyActivity extends Activity {
     private static final int PROGRESS = 0x1;

     private ProgressBar mProgress;
     private int mProgressStatus = 0;

     private Handler mHandler = new Handler();

     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);

         setContentView(R.layout.progressbar_activity);

         mProgress = (ProgressBar) findViewById(R.id.progress_bar);

         // Start lengthy operation in a background thread
         new Thread(new Runnable() {
             public void run() {
                 while (mProgressStatus < 100) {
                     mProgressStatus = doWork();

                     // Update the progress bar
                     mHandler.post(new Runnable() {
                         public void run() {
                             mProgress.setProgress(mProgressStatus);
                         }
                     });
                 }
             }
         }).start();
     }
 }

人家是建议你开一个线程,把耗时的pgBar丢进去后,然后使用handle机制来更行pgBar进度条的显示的~~~哇哈哈(偷懒ing)!

运行结果:

实现代码:

package com.example.android_progressbar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

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 = 100;
    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()
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getRealSize(widthMeasureSpec);
        mHeight = getRealSize(heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);

    }

    @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();
        }

    }

    public 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;
    }

    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);
        }
    }

}

其实,就是一个自定义View的实现;

xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android_progressbar.MainActivity" >

   <com.example.android_progressbar.CirclePgBar
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>

</RelativeLayout>

喜欢就点个赞吧~~~O(∩_∩)O谢谢 ~<完>

时间: 2024-12-11 23:30:20

UI控件之ProgressBar(进度条)的相关文章

【React Native开发】React Native控件之ProgressBarAndroid进度条讲解(12)

转载请标明出处: http://blog.csdn.net/developer_jiangqq/article/details/50596367 本文出自:[江清清的博客] (一)前言 [好消息]个人网站已经上线运行,后面博客以及技术干货等精彩文章会同步更新,请大家关注收藏:http://www.lcode.org 今天我们一起来看一下进度加载条ProgressBarAndroid控件的讲解与基本使用. 刚创建的React Native技术交流群(282693535),欢迎各位大牛,React

【React Native开发】React Native控件之ProgressBarAndroid进度条解说(12)

转载请标明出处: http://blog.csdn.net/developer_jiangqq/article/details/50596367 本文出自:[江清清的博客] (一)前言 [好消息]个人站点已经上线执行,后面博客以及技术干货等精彩文章会同步更新.请大家关注收藏:http://www.lcode.org 今天我们一起来看一下进度载入条ProgressBarAndroid控件的解说与基本使用. 刚创建的React Native技术交流3群(496508742),React Native

C# 时间控件 竖直进度条 饼图显示 按钮基础控件库

Prepare 本文将使用一个NuGet公开的组件来实现一些特殊的控件显示,方便大家进行快速的开发系统. 在Visual Studio 中的NuGet管理器中可以下载安装,也可以直接在NuGet控制台输入下面的指令安装: Install-Package HslCommunication NuGet安装教程  http://www.cnblogs.com/dathlin/p/7705014.html 技术支持QQ群:592132877 (组件的版本更新细节也将第一时间在群里发布) Summary

6控件(开关/滑块/进度条......)

#pragma mark  1开关 - (void)createSwitch{ UISwitch *sw = [[UISwitch alloc]initWithFrame:CGRectMake(0, 70, 200, 40)]; sw.tintColor = [UIColor redColor]; sw.thumbTintColor = [UIColor orangeColor]; [sw setOn:YES animated:YES]; [sw addTarget: self action:@

[转载]ExtJs4 笔记(8) Ext.slider 滚轴控件、 Ext.ProgressBar 进度条控件、 Ext.Editor 编辑控件

作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律责任. 本篇要登场的有三个控件,分别是滚轴控件.进度条控件和编辑控件. 一.滚轴控件 Ext.slider 1.滚轴控件的定义 下面我们定义三个具有代表意义滚轴控件,分别展示滚轴横向.纵向,以及单值.多值选择的特性: [html] <h1>滚轴控件</h1> <div class

Android基础入门教程——2.3.7 ProgressBar(进度条)

Android基础入门教程--2.3.7 ProgressBar(进度条) 标签(空格分隔): Android基础入门教程 本节引言: 本节给大家带来的是Android基本UI控件中的ProgressBar(进度条),ProgressBar的应用场景很多,比如 用户登录时,后台在发请求,以及等待服务器返回信息,这个时候会用到进度条:或者当在进行一些比较 耗时的操作,需要等待一段较长的时间,这个时候如果没有提示,用户可能会以为程序Carsh或者手机死机 了,这样会大大降低用户体验,所以在需要进行耗

Android02.常用布局及基本UI控件

一.Android学习API指南:[了解] 1. 应用的组成部分   App Components 1.1. 应用的基本原理    App Fundamentals 1.2. Activity      Activities 1.2.1. 片段    Fragments 1.2.2. 加载器     Loaders 1.2.3. 任务和返回堆    Tasks and Back Stack 1.3. Service服务   Services 1.3.1. 绑定服务     Bound Servi

《深入理解Windows Phone 8.1 UI控件编程》

<深入理解Windows Phone 8.1 UI控件编程>本书基于最新的Windows Phone 8.1 Runtime SDK编写,全面深入地论述了最酷的UI编程技术:实现复杂炫酷的动画.掌握布局原理.列表虚拟化原理.高性能列表实现.图表编程.控件原理等. 目录如下: <深入理解Windows Phone 8 .1 UI控件编程>目录 第1章 深入解析程序界面 1.1 XAML的原理 1.1.1 XAML的概念 1.1.2 XAML页面的编译 1.1.3 动态加载XAML 1

【Unity】8.1 Unity内置的UI控件

分类:Unity.C#.VS2015 创建日期:2016-04-27 一.简介 Unity 5.x内置了-套完整的GUI系统,提供了从布局.控件到皮肤的-整套GUI解决方案,因此可直接利用它做出各种风格和样式的GUI界面,并且扩展性很强(程序员可以基于已有的控件创建出适合自己需求的控件). 有两种使用GUI的办法,一种是直接将UI添加到层次视图或者场景视图中,然后通过GUI脚本去控制它:另一种是直接通过GUI脚本去创建. 二.直接添加UI控件到场景中 下图是Unity 5.3.4内置的UI控件,