状态进度条android

前端之家收集整理的这篇文章主要介绍了状态进度条android前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找一种在 Android中实现“状态”进度条的流畅方式,如下面三个示例所示.
由于我不是重新发明轮子的粉丝,我想问一下我是否有一些我不知道的库.

我查了一下,找不到任何lib,所以我想我需要自己实现它.这样做最简单的解决方案是什么?我应该扩展ProgressBar还是应该从头开始?你有任何我可以建立的建议或教程吗?

解决方法

更新

请参阅this sample project,附上GIF以提供概述.

您可以根据您的要求自定义以下内容.

  1. import android.content.Context;
  2. import android.content.res.TypedArray;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.graphics.RectF;
  7. import android.os.Bundle;
  8. import android.os.Parcelable;
  9. import android.util.AttributeSet;
  10. import android.view.View;
  11.  
  12. public class NumberProgressBar extends View {
  13.  
  14. private Context mContext;
  15.  
  16. /**
  17. * The max progress,default is 100
  18. */
  19. private int mMax = 100;
  20.  
  21. /**
  22. * current progress,can not exceed the max progress.
  23. */
  24. private int mProgress = 0;
  25.  
  26. /**
  27. * the progress area bar color
  28. */
  29. private int mReachedBarColor;
  30.  
  31. /**
  32. * the bar unreached area color.
  33. */
  34. private int mUnreachedBarColor;
  35.  
  36. /**
  37. * the progress text color.
  38. */
  39. private int mTextColor;
  40.  
  41. /**
  42. * the progress text size
  43. */
  44. private float mTextSize;
  45.  
  46. /**
  47. * the height of the reached area
  48. */
  49. private float mReachedBarHeight;
  50.  
  51. /**
  52. * the height of the unreached area
  53. */
  54. private float mUnreachedBarHeight;
  55.  
  56. /**
  57. * the suffix of the number.
  58. */
  59. private String mSuffix = "%";
  60.  
  61. /**
  62. * the prefix.
  63. */
  64. private String mPrefix = "";
  65.  
  66.  
  67. private final int default_text_color = Color.rgb(66,145,241);
  68. private final int default_reached_color = Color.rgb(66,241);
  69. private final int default_unreached_color = Color.rgb(204,204,204);
  70. private final float default_progress_text_offset;
  71. private final float default_text_size;
  72. private final float default_reached_bar_height;
  73. private final float default_unreached_bar_height;
  74.  
  75. /**
  76. * for save and restore instance of progressbar.
  77. */
  78. private static final String INSTANCE_STATE = "saved_instance";
  79. private static final String INSTANCE_TEXT_COLOR = "text_color";
  80. private static final String INSTANCE_TEXT_SIZE = "text_size";
  81. private static final String INSTANCE_REACHED_BAR_HEIGHT = "reached_bar_height";
  82. private static final String INSTANCE_REACHED_BAR_COLOR = "reached_bar_color";
  83. private static final String INSTANCE_UNREACHED_BAR_HEIGHT = "unreached_bar_height";
  84. private static final String INSTANCE_UNREACHED_BAR_COLOR = "unreached_bar_color";
  85. private static final String INSTANCE_MAX = "max";
  86. private static final String INSTANCE_PROGRESS = "progress";
  87. private static final String INSTANCE_SUFFIX = "suffix";
  88. private static final String INSTANCE_PREFIX = "prefix";
  89. private static final String INSTANCE_TEXT_VISBILITY = "text_visibility";
  90.  
  91. private static final int PROGRESS_TEXT_VISIBLE = 0;
  92. private static final int PROGRESS_TEXT_INVISIBLE = 1;
  93.  
  94.  
  95.  
  96. /**
  97. * the width of the text that to be drawn
  98. */
  99. private float mDrawTextWidth;
  100.  
  101. /**
  102. * the drawn text start
  103. */
  104. private float mDrawTextStart;
  105.  
  106. /**
  107. *the drawn text end
  108. */
  109. private float mDrawTextEnd;
  110.  
  111. /**
  112. * the text that to be drawn in onDraw()
  113. */
  114. private String mCurrentDrawText;
  115.  
  116. /**
  117. * the Paint of the reached area.
  118. */
  119. private Paint mReachedBarPaint;
  120. /**
  121. * the Painter of the unreached area.
  122. */
  123. private Paint mUnreachedBarPaint;
  124. /**
  125. * the Painter of the progress text.
  126. */
  127. private Paint mTextPaint;
  128.  
  129. /**
  130. * Unreached Bar area to draw rect.
  131. */
  132. private RectF mUnreachedRectF = new RectF(0,0);
  133. /**
  134. * reached bar area rect.
  135. */
  136. private RectF mReachedRectF = new RectF(0,0);
  137.  
  138. /**
  139. * the progress text offset.
  140. */
  141. private float mOffset;
  142.  
  143. /**
  144. * determine if need to draw unreached area
  145. */
  146. private boolean mDrawUnreachedBar = true;
  147.  
  148. private boolean mDrawReachedBar = true;
  149.  
  150. private boolean mIfDrawText = true;
  151.  
  152. public enum ProgressTextVisibility{
  153. Visible,Invisible
  154. };
  155.  
  156.  
  157.  
  158. public NumberProgressBar(Context context) {
  159. this(context,null);
  160. }
  161.  
  162. public NumberProgressBar(Context context,AttributeSet attrs) {
  163. this(context,attrs,R.attr.numberProgressBarStyle);
  164. }
  165.  
  166. public NumberProgressBar(Context context,AttributeSet attrs,int defStyleAttr) {
  167. super(context,defStyleAttr);
  168.  
  169. mContext = context;
  170.  
  171. default_reached_bar_height = dp2px(1.5f);
  172. default_unreached_bar_height = dp2px(1.0f);
  173. default_text_size = sp2px(10);
  174. default_progress_text_offset = dp2px(3.0f);
  175.  
  176. //load styled attributes.
  177. final TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs,R.styleable.NumberProgressBar,defStyleAttr,0);
  178.  
  179. mReachedBarColor = attributes.getColor(R.styleable.NumberProgressBar_progress_reached_color,default_reached_color);
  180. mUnreachedBarColor = attributes.getColor(R.styleable.NumberProgressBar_progress_unreached_color,default_unreached_color);
  181. mTextColor = attributes.getColor(R.styleable.NumberProgressBar_progress_text_color,default_text_color);
  182. mTextSize = attributes.getDimension(R.styleable.NumberProgressBar_progress_text_size,default_text_size);
  183.  
  184. mReachedBarHeight = attributes.getDimension(R.styleable.NumberProgressBar_progress_reached_bar_height,default_reached_bar_height);
  185. mUnreachedBarHeight = attributes.getDimension(R.styleable.NumberProgressBar_progress_unreached_bar_height,default_unreached_bar_height);
  186. mOffset = attributes.getDimension(R.styleable.NumberProgressBar_progress_text_offset,default_progress_text_offset);
  187.  
  188. int textVisible = attributes.getInt(R.styleable.NumberProgressBar_progress_text_visibility,PROGRESS_TEXT_VISIBLE);
  189. if(textVisible != PROGRESS_TEXT_VISIBLE){
  190. mIfDrawText = false;
  191. }
  192.  
  193. setProgress(attributes.getInt(R.styleable.NumberProgressBar_progress,0));
  194. setMax(attributes.getInt(R.styleable.NumberProgressBar_max,100));
  195. //
  196. attributes.recycle();
  197.  
  198. initializePainters();
  199.  
  200. }
  201.  
  202. @Override
  203. protected int getSuggestedMinimumWidth() {
  204. return (int)mTextSize;
  205. }
  206.  
  207. @Override
  208. protected int getSuggestedMinimumHeight() {
  209. return Math.max((int)mTextSize,Math.max((int)mReachedBarHeight,(int)mUnreachedBarHeight));
  210. }
  211.  
  212. @Override
  213. protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
  214. setMeasuredDimension(measure(widthMeasureSpec,true),measure(heightMeasureSpec,false));
  215. }
  216.  
  217. private int measure(int measureSpec,boolean isWidth){
  218. int result;
  219. int mode = MeasureSpec.getMode(measureSpec);
  220. int size = MeasureSpec.getSize(measureSpec);
  221. int padding = isWidth?getPaddingLeft()+getPaddingRight():getPaddingTop()+getPaddingBottom();
  222. if(mode == MeasureSpec.EXACTLY){
  223. result = size;
  224. }else{
  225. result = isWidth ? getSuggestedMinimumWidth() : getSuggestedMinimumHeight();
  226. result += padding;
  227. if(mode == MeasureSpec.AT_MOST){
  228. if(isWidth) {
  229. result = Math.max(result,size);
  230. }
  231. else{
  232. result = Math.min(result,size);
  233. }
  234. }
  235. }
  236. return result;
  237. }
  238.  
  239. @Override
  240. protected void onDraw(Canvas canvas) {
  241. if(mIfDrawText){
  242. calculateDrawRectF();
  243. }else{
  244. calculateDrawRectFWithoutProgressText();
  245. }
  246.  
  247. if(mDrawReachedBar){
  248. canvas.drawRect(mReachedRectF,mReachedBarPaint);
  249. }
  250.  
  251. if(mDrawUnreachedBar) {
  252. canvas.drawRect(mUnreachedRectF,mUnreachedBarPaint);
  253. }
  254.  
  255. if(mIfDrawText)
  256. canvas.drawText(mCurrentDrawText,mDrawTextStart,mDrawTextEnd,mTextPaint);
  257. }
  258.  
  259. private void initializePainters(){
  260. mReachedBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  261. mReachedBarPaint.setColor(mReachedBarColor);
  262.  
  263. mUnreachedBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  264. mUnreachedBarPaint.setColor(mUnreachedBarColor);
  265.  
  266. mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  267. mTextPaint.setColor(mTextColor);
  268. mTextPaint.setTextSize(mTextSize);
  269. }
  270.  
  271.  
  272. private void calculateDrawRectFWithoutProgressText(){
  273. mReachedRectF.left = getPaddingLeft();
  274. mReachedRectF.top = getHeight()/2.0f - mReachedBarHeight / 2.0f;
  275. mReachedRectF.right = (getWidth() - getPaddingLeft() - getPaddingRight() )/(getMax()*1.0f) * getProgress() + getPaddingLeft();
  276. mReachedRectF.bottom = getHeight()/2.0f + mReachedBarHeight / 2.0f;
  277.  
  278. mUnreachedRectF.left = mReachedRectF.right;
  279. mUnreachedRectF.right = getWidth() - getPaddingRight();
  280. mUnreachedRectF.top = getHeight()/2.0f + - mUnreachedBarHeight / 2.0f;
  281. mUnreachedRectF.bottom = getHeight()/2.0f + mUnreachedBarHeight / 2.0f;
  282. }
  283.  
  284. private void calculateDrawRectF(){
  285.  
  286. mCurrentDrawText = String.format("%d",getProgress()*100/getMax());
  287. mCurrentDrawText = mPrefix + mCurrentDrawText + mSuffix;
  288. mDrawTextWidth = mTextPaint.measureText(mCurrentDrawText);
  289.  
  290. if(getProgress() == 0){
  291. mDrawReachedBar = false;
  292. mDrawTextStart = getPaddingLeft();
  293. }else{
  294. mDrawReachedBar = true;
  295. mReachedRectF.left = getPaddingLeft();
  296. mReachedRectF.top = getHeight()/2.0f - mReachedBarHeight / 2.0f;
  297. mReachedRectF.right = (getWidth() - getPaddingLeft() - getPaddingRight() )/(getMax()*1.0f) * getProgress() - mOffset + getPaddingLeft();
  298. mReachedRectF.bottom = getHeight()/2.0f + mReachedBarHeight / 2.0f;
  299. mDrawTextStart = (mReachedRectF.right + mOffset);
  300. }
  301.  
  302. mDrawTextEnd = (int) ((getHeight() / 2.0f) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2.0f)) ;
  303.  
  304. if((mDrawTextStart + mDrawTextWidth )>= getWidth() - getPaddingRight()){
  305. mDrawTextStart = getWidth() - getPaddingRight() - mDrawTextWidth;
  306. mReachedRectF.right = mDrawTextStart - mOffset;
  307. }
  308.  
  309. float unreachedBarStart = mDrawTextStart + mDrawTextWidth + mOffset;
  310. if(unreachedBarStart >= getWidth() - getPaddingRight()){
  311. mDrawUnreachedBar = false;
  312. }else{
  313. mDrawUnreachedBar = true;
  314. mUnreachedRectF.left = unreachedBarStart;
  315. mUnreachedRectF.right = getWidth() - getPaddingRight();
  316. mUnreachedRectF.top = getHeight()/2.0f + - mUnreachedBarHeight / 2.0f;
  317. mUnreachedRectF.bottom = getHeight()/2.0f + mUnreachedBarHeight / 2.0f;
  318. }
  319. }
  320. /**
  321. * get progress text color
  322. * @return progress text color
  323. */
  324. public int getTextColor() {
  325. return mTextColor;
  326. }
  327.  
  328. /**
  329. * get progress text size
  330. * @return progress text size
  331. */
  332. public float getProgressTextSize() {
  333. return mTextSize;
  334. }
  335.  
  336. public int getUnreachedBarColor() {
  337. return mUnreachedBarColor;
  338. }
  339.  
  340. public int getReachedBarColor() {
  341. return mReachedBarColor;
  342. }
  343.  
  344. public int getProgress() {
  345. return mProgress;
  346. }
  347.  
  348. public int getMax() {
  349. return mMax;
  350. }
  351.  
  352. public float getReachedBarHeight(){
  353. return mReachedBarHeight;
  354. }
  355.  
  356. public float getUnreachedBarHeight(){
  357. return mUnreachedBarHeight;
  358. }
  359.  
  360. public void setProgressTextSize(float TextSize) {
  361. this.mTextSize = TextSize;
  362. mTextPaint.setTextSize(mTextSize);
  363. invalidate();
  364. }
  365.  
  366. public void setProgressTextColor(int TextColor) {
  367. this.mTextColor = TextColor;
  368. mTextPaint.setColor(mTextColor);
  369. invalidate();
  370. }
  371.  
  372. public void setUnreachedBarColor(int BarColor) {
  373. this.mUnreachedBarColor = BarColor;
  374. mUnreachedBarPaint.setColor(mReachedBarColor);
  375. invalidate();
  376. }
  377.  
  378. public void setReachedBarColor(int ProgressColor) {
  379. this.mReachedBarColor = ProgressColor;
  380. mReachedBarPaint.setColor(mReachedBarColor);
  381. invalidate();
  382. }
  383.  
  384. public void setReachedBarHeight(float height){
  385. mReachedBarHeight = height;
  386. }
  387.  
  388. public void setUnreachedBarHeight(float height){
  389. mUnreachedBarHeight = height;
  390. }
  391.  
  392. public void setMax(int Max) {
  393. if(Max > 0){
  394. this.mMax = Max;
  395. invalidate();
  396. }
  397. }
  398.  
  399. public void setSuffix(String suffix){
  400. if(suffix == null){
  401. mSuffix = "";
  402. }else{
  403. mSuffix = suffix;
  404. }
  405. }
  406.  
  407. public String getSuffix(){
  408. return mSuffix;
  409. }
  410.  
  411. public void setPrefix(String prefix){
  412. if(prefix == null)
  413. mPrefix = "";
  414. else{
  415. mPrefix = prefix;
  416. }
  417. }
  418.  
  419. public String getPrefix(){
  420. return mPrefix;
  421. }
  422.  
  423. public void incrementProgressBy(int by){
  424. if(by > 0){
  425. setProgress(getProgress() + by);
  426. }
  427. }
  428.  
  429. public void setProgress(int Progress) {
  430. if(Progress <= getMax() && Progress >= 0){
  431. this.mProgress = Progress;
  432. invalidate();
  433. }
  434. }
  435.  
  436. @Override
  437. protected Parcelable onSaveInstanceState() {
  438. final Bundle bundle = new Bundle();
  439. bundle.putParcelable(INSTANCE_STATE,super.onSaveInstanceState());
  440. bundle.putInt(INSTANCE_TEXT_COLOR,getTextColor());
  441. bundle.putFloat(INSTANCE_TEXT_SIZE,getProgressTextSize());
  442. bundle.putFloat(INSTANCE_REACHED_BAR_HEIGHT,getReachedBarHeight());
  443. bundle.putFloat(INSTANCE_UNREACHED_BAR_HEIGHT,getUnreachedBarHeight());
  444. bundle.putInt(INSTANCE_REACHED_BAR_COLOR,getReachedBarColor());
  445. bundle.putInt(INSTANCE_UNREACHED_BAR_COLOR,getUnreachedBarColor());
  446. bundle.putInt(INSTANCE_MAX,getMax());
  447. bundle.putInt(INSTANCE_PROGRESS,getProgress());
  448. bundle.putString(INSTANCE_SUFFIX,getSuffix());
  449. bundle.putString(INSTANCE_PREFIX,getPrefix());
  450. bundle.putBoolean(INSTANCE_TEXT_VISBILITY,getProgressTextVisibility());
  451. return bundle;
  452. }
  453.  
  454. @Override
  455. protected void onRestoreInstanceState(Parcelable state) {
  456. if(state instanceof Bundle){
  457. final Bundle bundle = (Bundle)state;
  458. mTextColor = bundle.getInt(INSTANCE_TEXT_COLOR);
  459. mTextSize = bundle.getFloat(INSTANCE_TEXT_SIZE);
  460. mReachedBarHeight = bundle.getFloat(INSTANCE_REACHED_BAR_HEIGHT);
  461. mUnreachedBarHeight = bundle.getFloat(INSTANCE_UNREACHED_BAR_HEIGHT);
  462. mReachedBarColor = bundle.getInt(INSTANCE_REACHED_BAR_COLOR);
  463. mUnreachedBarColor = bundle.getInt(INSTANCE_UNREACHED_BAR_COLOR);
  464. initializePainters();
  465. setMax(bundle.getInt(INSTANCE_MAX));
  466. setProgress(bundle.getInt(INSTANCE_PROGRESS));
  467. setPrefix(bundle.getString(INSTANCE_PREFIX));
  468. setSuffix(bundle.getString(INSTANCE_SUFFIX));
  469. setProgressTextVisibility(bundle.getBoolean(INSTANCE_TEXT_VISBILITY) ? ProgressTextVisibility.Visible : ProgressTextVisibility.Invisible);
  470. super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATE));
  471. return;
  472. }
  473. super.onRestoreInstanceState(state);
  474. }
  475.  
  476. public float dp2px(float dp) {
  477. final float scale = getResources().getDisplayMetrics().density;
  478. return dp * scale + 0.5f;
  479. }
  480.  
  481. public float sp2px(float sp){
  482. final float scale = getResources().getDisplayMetrics().scaledDensity;
  483. return sp * scale;
  484. }
  485.  
  486. public void setProgressTextVisibility(ProgressTextVisibility visibility){
  487. mIfDrawText = visibility == ProgressTextVisibility.Visible;
  488. invalidate();
  489. }
  490.  
  491. public boolean getProgressTextVisibility() {
  492. return mIfDrawText;
  493. }
  494.  
  495. }

Link 1

Link 2

Link 3

Link 4

Link 5

Link 6

您也可以使用线性布局.我做了一个简单的例子,只是一个简单的例子.

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5.  
  6. <LinearLayout
  7. android:id="@+id/ll_progress"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_centerHorizontal="true"
  11. android:background="@drawable/layout" >
  12.  
  13. <ImageView
  14. android:id="@+id/ImageView01"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_marginLeft="10dp"
  18. android:layout_marginTop="14dp"
  19. android:src="@drawable/bg_1" />
  20.  
  21. <ImageView
  22. android:id="@+id/imageView1"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_marginLeft="38dp"
  26. android:layout_marginTop="14dp"
  27. android:src="@drawable/bg_1" />
  28.  
  29.  
  30. <ImageView
  31. android:id="@+id/imageView12"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:layout_marginLeft="35dp"
  35. android:layout_marginTop="14dp"
  36. android:src="@drawable/bg_1" />
  37.  
  38. </LinearLayout>
  39.  
  40. </RelativeLayout>

MainActivity.java

  1. import java.util.concurrent.TimeUnit;
  2.  
  3. import android.app.Activity;
  4. import android.graphics.BitmapFactory;
  5. import android.graphics.drawable.BitmapDrawable;
  6. import android.graphics.drawable.Drawable;
  7. import android.graphics.drawable.TransitionDrawable;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.widget.ImageView;
  11. import android.widget.LinearLayout;
  12.  
  13. public class MainActivity extends Activity {
  14.  
  15. LinearLayout llProgress;
  16. Handler handler;
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. llProgress = (LinearLayout) findViewById(R.id.ll_progress);
  23. handler = new Handler(getMainLooper());
  24.  
  25. startTransition();
  26. }
  27.  
  28. int prevChild = 0;
  29.  
  30. private void startTransition() {
  31. // create the transition layers
  32. Drawable[] layers = new Drawable[2];
  33. layers[0] = new BitmapDrawable(getResources(),BitmapFactory.decodeResource(getResources(),R.drawable.bg));
  34. layers[1] = new BitmapDrawable(getResources(),R.drawable.bg_1));
  35.  
  36. final TransitionDrawable[] transitionDrawable = new TransitionDrawable[llProgress.getChildCount()];
  37. for (int i = 0; i < llProgress.getChildCount(); i++) {
  38. transitionDrawable[i] = new TransitionDrawable(layers);
  39. ((ImageView) llProgress.getChildAt(i)).setImageDrawable(transitionDrawable[i]);
  40. }
  41.  
  42. handler.postDelayed(new Runnable() {
  43.  
  44. @Override
  45. public void run() {
  46. transitionDrawable[prevChild++].startTransition(1000);
  47. if(prevChild > llProgress.getChildCount() - 1){
  48. prevChild = 0;
  49. }
  50. handler.postDelayed(this,1000 * 3);
  51. }
  52. },1000 * 3);
  53.  
  54. }

猜你在找的Android相关文章