一起学Android之ProgressBar

贵有恒,何必三更起、五更眠、最无益,只怕一日曝、十日寒。这篇文章主要讲述一起学Android之ProgressBar相关的知识,希望能为你提供帮助。
本文简述在android开发中进度条(ProgressBar)的常见应用,仅供学习分享使用。
概述【一起学Android之ProgressBar】在Android开发中,进度条的使用场景有很多,如播放电影时可拖动的观看进度条,评分时使用的评分条,上传下载时的进度条,网络加载时的圆形进度条等。本文主要讲解三种进度条的常见用法:ProgressBar,SeekBar,RatingBar。
ProgressBar常见用法ProgressBar涉及知识点

  1. android:max 设置进度条的最大进度
  2. android:progress 设置进度条的当前进度(如播放视频的播放进度)
  3. android:secondaryProgress 设置进度条的第二进度(如播放视频时的缓冲进度)
  4. style="?android:attr/progressBarStyleHorizontal" 设置进度条的样式(水平样式)
  5. style="?android:attr/progressBarStyleLarge"  设置进度条的样式(垂直样式),圆形进度条
  6. isIndeterminate() 判断进度条的形状,true:水平 false:圆形
  7. incrementProgressBy(10) 增加当前的进度  incrementSecondaryProgressBy(10) 增加当前的第二进度。
ProgressBar效果图如下图所示:
一起学Android之ProgressBar

文章图片

ProgressBar示例代码:
一起学Android之ProgressBar

文章图片
一起学Android之ProgressBar

文章图片
1 < LinearLayout 2android:id="@+id/ll_progress" 3android:layout_width="match_parent" 4android:layout_height="wrap_content" 5android:layout_below="@id/ll_seeking" 6android:layout_marginTop="8dp" 7android:layout_marginRight="15dp" 8android:orientation="horizontal"> 9< TextView 10android:id="@+id/tv_progress" 11android:layout_width="wrap_content" 12android:layout_height="wrap_content" 13android:text="@string/progress1" 14android:textSize="20dp"/> 15< ProgressBar 16android:id="@+id/pb_01" 17android:max="100" 18android:progress="30" 19android:layout_marginLeft="10dp" 20android:secondaryProgress="40" 21style="?android:attr/progressBarStyleHorizontal" 22android:layout_weight="1" 23android:layout_width="0dp" 24android:layout_height="40dp"/> 25< /LinearLayout> 26< TextView 27android:id="@+id/tv_progress2" 28android:layout_width="wrap_content" 29android:layout_height="wrap_content" 30android:layout_below="@id/ll_progress" 31android:layout_marginTop="8dp" 32android:text="@string/progress2" 33android:textSize="20dp"/> 34< ProgressBar 35android:id="@+id/pb_02" 36android:layout_alignLeft="@id/rbar" 37android:layout_below="@id/ll_progress" 38style="?android:attr/progressBarStyleLarge" 39android:layout_width="wrap_content" 40android:layout_height="wrap_content"/>

View Code SeekBar常见用法SeekBar涉及知识点
  1. android:max ,android:progress ,android:secondaryProgress此三个属性和ProgressBar相同,参考上面。
  2. setOnSeekBarChangeListener 用于设置SeekBar的监听事件,监听用户拖动的状态。
  3. OnSeekBarChangeListener表示一个接口,有三个函数需要实现:
    • onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) //进度改变
    • onStartTrackingTouch(SeekBar seekBar) //开始触摸
    • onStopTrackingTouch(SeekBar seekBar) //停止触摸
SeekBar效果图如下图所示:
一起学Android之ProgressBar

文章图片

SeekBar示例代码
一起学Android之ProgressBar

文章图片
一起学Android之ProgressBar

文章图片
1//监控SeekBar事件 2mSeekBar=(SeekBar)this.findViewById(R.id.sbar); 3mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 4@Override 5public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 6Log.i("DemoBar", "SeekBar--> setOnSeekBarChangeListener: "+String.valueOf(progress)+"--> fromUser:"+String.valueOf(fromUser)); 7mSeekText.setText(String.valueOf(progress)); 8} 9 10@Override 11public void onStartTrackingTouch(SeekBar seekBar) { 12 13} 14 15@Override 16public void onStopTrackingTouch(SeekBar seekBar) { 17 18} 19});

View Code RatingBar常见用法RatingBar涉及知识点
  1. android:numStars 表示星星的个数,默认为5个
  2. android:rating 表示当前的分值
  3. android:stepSize 表示步长,及前进一次多长的跨度,如,0.5表示半颗星星 1表示一颗心
  4. setOnRatingBarChangeListener 设置监听事件,
  5. OnRatingBarChangeListener 表示一个接口,只有一个函数需要实现:
    • onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser)
RatingBar效果图如下图所示:
一起学Android之ProgressBar

文章图片

RatingBar示例代码
一起学Android之ProgressBar

文章图片
一起学Android之ProgressBar

文章图片
1mRatingBar =(RatingBar) this.findViewById(R.id.rbar); 2mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { 3@Override 4public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { 5Log.i("DemoBar", "RatingBar--> onRatingChanged: "+String.valueOf(rating)+"--> fromUser:"+String.valueOf(fromUser)); 6mRatingText.setText(String.valueOf(rating)); 7} 8});

View Code 备注一起学习,一起总结,一起进步。附上整体演示图片
一起学Android之ProgressBar

文章图片


    推荐阅读