Android 自定义NumProgressBar
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android 自定义NumProgressBar,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含18753字,纯文字阅读大概需要27分钟。
内容图文
![Android 自定义NumProgressBar](/upload/InfoBanner/zyjiaocheng/1054/85cf427624894008bd361f24154c02a3.jpg)
这是GitHub上的一个开源控件,由于作者是用Android Studio开发,直接导入Eclipse不能使用,这边抠出来这个功能,做成一个小Demo,供Eclipse平台使用。
style配置文件中有可以选择颜色
< resources > <!-- Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devices. --> < style name ="AppBaseTheme" parent ="android:Theme.Light" > <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. --> </ style > <!-- Application theme. --> < style name ="AppTheme" parent ="AppBaseTheme" > <!-- All customizations that are NOT specific to a particular API-level can go here. --> </ style > < style name ="NumberProgressBar_Default" > < item name ="android:layout_height" >wrap_content</item><item name="android:layout_width">match_parent</item><item name="max">100</item><item name="progress">0</item><item name="progress_unreached_color">#CCCCCC</item><item name="progress_reached_color">#3498DB</item><item name="progress_text_size">10sp</item><item name="progress_text_color">#3498DB</item><item name="progress_reached_bar_height">1.5dp</item><item name="progress_unreached_bar_height">0.75dp</item></style><style name="NumberProgressBar_Passing_Green"><item name="android:layout_height">wrap_content</item><item name="android:layout_width">match_parent</item><item name="max">100</item><item name="progress">0</item><item name="progress_unreached_color">#CCCCCC</item><item name="progress_reached_color">#70A800</item><item name="progress_text_size">10sp</item><item name="progress_text_color">#70A800</item><item name="progress_reached_bar_height">8.5dp</item><item name="progress_unreached_bar_height">0.75dp</item></style><style name="NumberProgressBar_Beauty_Red"><item name="android:layout_height">wrap_content</item><item name="android:layout_width">match_parent</item><item name="max">100</item><item name="progress">0</item><item name="progress_unreached_color">#CCCCCC</item><item name="progress_reached_color">#FF3D7F</item><item name="progress_text_size">10sp</item><item name="progress_text_color">#FF3D7F</item><item name="progress_reached_bar_height">1.5dp</item><item name="progress_unreached_bar_height">0.75dp</item></style><style name="NumberProgressBar_Warning_Red"><item name="android:layout_height">wrap_content</item><item name="android:layout_width">match_parent</item><item name="max">100</item><item name="progress">0</item><item name="progress_unreached_color">#CCCCCC</item><item name="progress_reached_color">#E74C3C</item><item name="progress_text_size">10sp</item><item name="progress_text_color">#E74C3C</item><item name="progress_reached_bar_height">1.5dp</item><item name="progress_unreached_bar_height">0.75dp</item></style><style name="NumberProgressBar_Relax_Blue"><item name="android:layout_height">wrap_content</item><item name="android:layout_width">match_parent</item><item name="max">100</item><item name="progress">0</item><item name="progress_unreached_color">#CCCCCC</item><item name="progress_reached_color">#6DBCDB</item><item name="progress_text_size">10sp</item><item name="progress_text_color">#6DBCDB</item><item name="progress_reached_bar_height">10dp</item><item name="progress_unreached_bar_height">0.75dp</item></style><style name="NumberProgressBar_Grace_Yellow"><item name="android:layout_height">wrap_content</item><item name="android:layout_width">match_parent</item><item name="max">100</item><item name="progress">0</item><item name="progress_unreached_color">#CCCCCC</item><item name="progress_reached_color">#FFC73B</item><item name="progress_text_size">10sp</item><item name="progress_text_color">#FFC73B</item><item name="progress_reached_bar_height">1.5dp</item><item name="progress_unreached_bar_height">0.75dp</item></style><style name="NumberProgressBar_Funny_Orange"><item name="android:layout_height">wrap_content</item><item name="android:layout_width">match_parent</item><item name="max">100</item><item name="progress">0</item><item name="progress_unreached_color">#CCCCCC</item><item name="progress_reached_color">#FF530D</item><item name="progress_text_size">10sp</item><item name="progress_text_color">#FF530D</item><item name="progress_reached_bar_height">1.5dp</item><item name="progress_unreached_bar_height">0.75dp</item></style><style name="NumberProgressBar_Twinkle_Night"><item name="android:layout_height">wrap_content</item><item name="android:layout_width">match_parent</item><item name="max">100</item><item name="progress">0</item><item name="progress_unreached_color">#CCCCCC</item><item name="progress_reached_color">#ECF0F1</item><item name="progress_text_size">10sp</item><item name="progress_text_color">#ECF0F1</item><item name="progress_reached_bar_height">1.5dp</item><item name="progress_unreached_bar_height">0.75dp</item></style></resources>
主要实现进度的类
package com.example.mynumbertest; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.os.Bundle; import android.os.Parcelable; import android.util.AttributeSet; import android.view.View; public class NumberProgressBar extends View { private Context mContext; /** * The max progress, default is 100 */privateint mMax = 100; /** * current progress, can not exceed the max progress. */privateint mProgress = 0; /** * the progress area bar color */privateint mReachedBarColor; /** * the bar unreached area color. */privateint mUnreachedBarColor; /** * the progress text color. */privateint mTextColor; /** * the progress text size */privatefloat mTextSize; /** * the height of the reached area */privatefloat mReachedBarHeight; /** * the height of the unreached area */privatefloat mUnreachedBarHeight; privatefinalint default_text_color = Color.rgb(66, 145, 241); privatefinalint default_reached_color = Color.rgb(66,145,241); privatefinalint default_unreached_color = Color.rgb(204, 204, 204); privatefinalfloat default_progress_text_offset; privatefinalfloat default_text_size; privatefinalfloat default_reached_bar_height; privatefinalfloat default_unreached_bar_height; /** * for save and restore instance of progressbar. */privatestaticfinal String INSTANCE_STATE = "saved_instance"; privatestaticfinal String INSTANCE_TEXT_COLOR = "text_color"; privatestaticfinal String INSTANCE_TEXT_SIZE = "text_size"; privatestaticfinal String INSTANCE_REACHED_BAR_HEIGHT = "reached_bar_height"; privatestaticfinal String INSTANCE_REACHED_BAR_COLOR = "reached_bar_color"; privatestaticfinal String INSTANCE_UNREACHED_BAR_HEIGHT = "unreached_bar_height"; privatestaticfinal String INSTANCE_UNREACHED_BAR_COLOR = "unreached_bar_color"; privatestaticfinal String INSTANCE_MAX = "max"; privatestaticfinal String INSTANCE_PROGRESS = "progress"; privatestaticfinalint PROGRESS_TEXT_VISIBLE = 0; privatestaticfinalint PROGRESS_TEXT_INVISIBLE = 1; /** * the width of the text that to be drawn */privatefloat mDrawTextWidth; /** * the drawn text start */privatefloat mDrawTextStart; /** *the drawn text end */privatefloat mDrawTextEnd; /** * the text that to be drawn in onDraw() */private String mCurrentDrawText; /** * the Paint of the reached area. */private Paint mReachedBarPaint; /** * the Painter of the unreached area. */private Paint mUnreachedBarPaint; /** * the Painter of the progress text. */private Paint mTextPaint; /** * Unreached Bar area to draw rect. */private RectF mUnreachedRectF = new RectF(0,0,0,0); /** * reached bar area rect. */private RectF mReachedRectF = new RectF(0,0,0,0); /** * the progress text offset. */privatefloat mOffset; /** * determine if need to draw unreached area */privateboolean mDrawUnreachedBar = true; privateboolean mDrawReachedBar = true; privateboolean mIfDrawText = true; publicenum ProgressTextVisibility{ Visible,Invisible }; public NumberProgressBar(Context context) { this(context, null); } public NumberProgressBar(Context context, AttributeSet attrs) { this(context, attrs, R.attr.numberProgressBarStyle); } public NumberProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mContext = context; default_reached_bar_height = dp2px(1.5f); default_unreached_bar_height = dp2px(1.0f); default_text_size = sp2px(10); default_progress_text_offset = dp2px(3.0f); //load styled attributes.final TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.NumberProgressBar, defStyleAttr, 0); mReachedBarColor = attributes.getColor(R.styleable.NumberProgressBar_progress_reached_color, default_reached_color); mUnreachedBarColor = attributes.getColor(R.styleable.NumberProgressBar_progress_unreached_color,default_unreached_color); mTextColor = attributes.getColor(R.styleable.NumberProgressBar_progress_text_color,default_text_color); mTextSize = attributes.getDimension(R.styleable.NumberProgressBar_progress_text_size, default_text_size); mReachedBarHeight = attributes.getDimension(R.styleable.NumberProgressBar_progress_reached_bar_height,default_reached_bar_height); mUnreachedBarHeight = attributes.getDimension(R.styleable.NumberProgressBar_progress_unreached_bar_height,default_unreached_bar_height); mOffset = attributes.getDimension(R.styleable.NumberProgressBar_progress_text_offset,default_progress_text_offset); int textVisible = attributes.getInt(R.styleable.NumberProgressBar_progress_text_visibility,PROGRESS_TEXT_VISIBLE); if(textVisible != PROGRESS_TEXT_VISIBLE){ mIfDrawText = false; } setProgress(attributes.getInt(R.styleable.NumberProgressBar_progress,0)); setMax(attributes.getInt(R.styleable.NumberProgressBar_max, 100)); // attributes.recycle(); initializePainters(); } @Override protectedint getSuggestedMinimumWidth() { return (int)mTextSize; } @Override protectedint getSuggestedMinimumHeight() { return Math.max((int)mTextSize,Math.max((int)mReachedBarHeight,(int)mUnreachedBarHeight)); } @Override protectedvoid onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(measure(widthMeasureSpec,true), measure(heightMeasureSpec,false)); } privateint measure(int measureSpec,boolean isWidth){ int result; int mode = MeasureSpec.getMode(measureSpec); int size = MeasureSpec.getSize(measureSpec); int padding = isWidth?getPaddingLeft()+getPaddingRight():getPaddingTop()+getPaddingBottom(); if(mode == MeasureSpec.EXACTLY){ result = size; }else{ result = isWidth ? getSuggestedMinimumWidth() : getSuggestedMinimumHeight(); result += padding; if(mode == MeasureSpec.AT_MOST){ if(isWidth) { result = Math.max(result, size); } else{ result = Math.min(result, size); } } } return result; } @Override protectedvoid onDraw(Canvas canvas) { if(mIfDrawText){ calculateDrawRectF(); }else{ calculateDrawRectFWithoutProgressText(); } if(mDrawReachedBar){ canvas.drawRect(mReachedRectF,mReachedBarPaint); } if(mDrawUnreachedBar) { canvas.drawRect(mUnreachedRectF, mUnreachedBarPaint); } if(mIfDrawText) canvas.drawText(mCurrentDrawText,mDrawTextStart,mDrawTextEnd,mTextPaint); } privatevoid initializePainters(){ mReachedBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mReachedBarPaint.setColor(mReachedBarColor); mUnreachedBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mUnreachedBarPaint.setColor(mUnreachedBarColor); mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mTextPaint.setColor(mTextColor); mTextPaint.setTextSize(mTextSize); } privatevoid calculateDrawRectFWithoutProgressText(){ mReachedRectF.left = getPaddingLeft(); mReachedRectF.top = getHeight()/2.0f - mReachedBarHeight / 2.0f; mReachedRectF.right = (getWidth() - getPaddingLeft() - getPaddingRight() )/(getMax()*1.0f) * getProgress() + getPaddingLeft(); mReachedRectF.bottom = getHeight()/2.0f + mReachedBarHeight / 2.0f; mUnreachedRectF.left = mReachedRectF.right; mUnreachedRectF.right = getWidth() - getPaddingRight(); mUnreachedRectF.top = getHeight()/2.0f + - mUnreachedBarHeight / 2.0f; mUnreachedRectF.bottom = getHeight()/2.0f + mUnreachedBarHeight / 2.0f; } privatevoid calculateDrawRectF(){ mCurrentDrawText = String.format("%d%%",getProgress()*100/getMax()); mDrawTextWidth = mTextPaint.measureText(mCurrentDrawText); if(getProgress() == 0){ mDrawReachedBar = false; mDrawTextStart = getPaddingLeft(); }else{ mDrawReachedBar = true; mReachedRectF.left = getPaddingLeft(); mReachedRectF.top = getHeight()/2.0f - mReachedBarHeight / 2.0f; mReachedRectF.right = (getWidth() - getPaddingLeft() - getPaddingRight() )/(getMax()*1.0f) * getProgress() - mOffset + getPaddingLeft(); mReachedRectF.bottom = getHeight()/2.0f + mReachedBarHeight / 2.0f; mDrawTextStart = (mReachedRectF.right + mOffset); } mDrawTextEnd = (int) ((getHeight() / 2.0f) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2.0f)) ; if((mDrawTextStart + mDrawTextWidth )>= getWidth() - getPaddingRight()){ mDrawTextStart = getWidth() - getPaddingRight() - mDrawTextWidth; mReachedRectF.right = mDrawTextStart - mOffset; } float unreachedBarStart = mDrawTextStart + mDrawTextWidth + mOffset; if(unreachedBarStart >= getWidth() - getPaddingRight()){ mDrawUnreachedBar = false; }else{ mDrawUnreachedBar = true; mUnreachedRectF.left = unreachedBarStart; mUnreachedRectF.right = getWidth() - getPaddingRight(); mUnreachedRectF.top = getHeight()/2.0f + - mUnreachedBarHeight / 2.0f; mUnreachedRectF.bottom = getHeight()/2.0f + mUnreachedBarHeight / 2.0f; } } /** * get progress text color * @return progress text color */publicint getTextColor() { return mTextColor; } /** * get progress text size * @return progress text size */publicfloat getProgressTextSize() { return mTextSize; } publicint getUnreachedBarColor() { return mUnreachedBarColor; } publicint getReachedBarColor() { return mReachedBarColor; } publicint getProgress() { return mProgress; } publicint getMax() { return mMax; } publicfloat getReachedBarHeight(){ return mReachedBarHeight; } publicfloat getUnreachedBarHeight(){ return mUnreachedBarHeight; } publicvoid setProgressTextSize(float TextSize) { this.mTextSize = TextSize; mTextPaint.setTextSize(mTextSize); invalidate(); } publicvoid setProgressTextColor(int TextColor) { this.mTextColor = TextColor; mTextPaint.setColor(mTextColor); invalidate(); } publicvoid setUnreachedBarColor(int BarColor) { this.mUnreachedBarColor = BarColor; mUnreachedBarPaint.setColor(mReachedBarColor); invalidate(); } publicvoid setReachedBarColor(int ProgressColor) { this.mReachedBarColor = ProgressColor; mReachedBarPaint.setColor(mReachedBarColor); invalidate(); } publicvoid setMax(int Max) { if(Max > 0){ this.mMax = Max; invalidate(); } } publicvoid incrementProgressBy(int by){ if(by > 0){ setProgress(getProgress() + by); } } publicvoid setProgress(int Progress) { if(Progress <= getMax() && Progress >= 0){ this.mProgress = Progress; invalidate(); } } @Override protected Parcelable onSaveInstanceState() { final Bundle bundle = new Bundle(); bundle.putParcelable(INSTANCE_STATE,super.onSaveInstanceState()); bundle.putInt(INSTANCE_TEXT_COLOR,getTextColor()); bundle.putFloat(INSTANCE_TEXT_SIZE, getProgressTextSize()); bundle.putFloat(INSTANCE_REACHED_BAR_HEIGHT,getReachedBarHeight()); bundle.putFloat(INSTANCE_UNREACHED_BAR_HEIGHT,getUnreachedBarHeight()); bundle.putInt(INSTANCE_REACHED_BAR_COLOR,getReachedBarColor()); bundle.putInt(INSTANCE_UNREACHED_BAR_COLOR,getUnreachedBarColor()); bundle.putInt(INSTANCE_MAX,getMax()); bundle.putInt(INSTANCE_PROGRESS,getProgress()); return bundle; } @Override protectedvoid onRestoreInstanceState(Parcelable state) { if(state instanceof Bundle){ final Bundle bundle = (Bundle)state; mTextColor = bundle.getInt(INSTANCE_TEXT_COLOR); mTextSize = bundle.getFloat(INSTANCE_TEXT_SIZE); mReachedBarHeight = bundle.getFloat(INSTANCE_REACHED_BAR_HEIGHT); mUnreachedBarHeight = bundle.getFloat(INSTANCE_UNREACHED_BAR_HEIGHT); mReachedBarColor = bundle.getInt(INSTANCE_REACHED_BAR_COLOR); mUnreachedBarColor = bundle.getInt(INSTANCE_UNREACHED_BAR_COLOR); initializePainters(); setMax(bundle.getInt(INSTANCE_MAX)); setProgress(bundle.getInt(INSTANCE_PROGRESS)); super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATE)); return; } super.onRestoreInstanceState(state); } publicfloat dp2px(float dp) { finalfloat scale = getResources().getDisplayMetrics().density; return dp * scale + 0.5f; } publicfloat sp2px(float sp){ finalfloat scale = getResources().getDisplayMetrics().scaledDensity; return sp * scale; } publicvoid setProgressTextVisibility(ProgressTextVisibility visibility){ if(visibility == ProgressTextVisibility.Visible){ mIfDrawText = true; }else{ mIfDrawText = false; } invalidate(); } }
实现:
package com.example.mynumbertest; import java.util.Timer; import java.util.TimerTask; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { privateint counter = 0; private Timer timer; @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final NumberProgressBar bnp = (NumberProgressBar)findViewById(R.id.numberbar2); counter = 0; timer = new Timer(); timer.schedule(new TimerTask() { @Override publicvoid run() { runOnUiThread(new Runnable() { @Override publicvoid run() { bnp.incrementProgressBy(1); counter ++; if (counter == 110) { bnp.setProgress(0); counter=0; } } }); } }, 1000, 100); } @Override publicboolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); returntrue; } }
下载地址
http://download.csdn.net/detail/lj419855402/8255245
原文:http://www.cnblogs.com/LIANQQ/p/4161217.html
内容总结
以上是互联网集市为您收集整理的Android 自定义NumProgressBar全部内容,希望文章能够帮你解决Android 自定义NumProgressBar所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。