android 水平progressBar动态加载
2015-10-26 14:17
435 查看
当你需要动态显示一个progressBar时,动态显示进度,并在右侧显示加载进度,请参考下面代码:
重写写布局
用到的其他代码:
使用方式:xml
使用方式:代码
重写写布局
public class LineProgressBar extends View { private Paint paint; private int lineColor; private int lineProgressColor; private int textColor; private float textSize; private float textWidth; private float textMargin; private float lineHeight; private int max; private int progress = 0; private RectF pLine, bLine; private int progressOne; public LineProgressBar(Context context) { this(context, null); } public LineProgressBar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public LineProgressBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); progressOne = 0; paint = new Paint(); TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.LineProgressBar); // 获取自定义属性和默认值 lineColor = mTypedArray.getColor( R.styleable.RoundProgressBar_lineColor, Color.RED); lineHeight = mTypedArray.getDimension( R.styleable.RoundProgressBar_lineHeight, 10); lineProgressColor = mTypedArray.getColor( R.styleable.RoundProgressBar_lineProgressColor, Color.GREEN); textColor = mTypedArray.getColor( R.styleable.RoundProgressBar_textColor, Color.GREEN); textSize = mTypedArray.getDimension( R.styleable.RoundProgressBar_textSize, 15); textWidth = mTypedArray.getDimension( R.styleable.RoundProgressBar_textWidth, 30); textMargin = mTypedArray.getDimension( R.styleable.RoundProgressBar_textMargin, 5); max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100); mTypedArray.recycle(); pLine = new RectF(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); float width = getWidth(); float height = getHeight(); float margin = (height - lineHeight)/2; float drawHeight = lineHeight + margin; if(bLine == null) bLine = new RectF(0, margin, width-textWidth-textMargin, drawHeight); paint.setColor(lineColor); // 设置背景颜色 paint.setStyle(Paint.Style.FILL); // 设置填充 paint.setAntiAlias(true); // 消除锯齿 canvas.drawRoundRect(bLine, 5, 5, paint); //画进度背景 paint.setStrokeWidth(0); paint.setColor(textColor);//设置字体颜色 paint.setTextSize(textSize);//设置字体大小 paint.setTypeface(Typeface.DEFAULT); // 设置字体 int percent = (int) (((float) progressOne / (float) max) * 100); canvas.drawText(percent + "%", width - textWidth - textMargin, height, paint); // 画出进度百分比 paint.setColor(lineProgressColor);//设置进度颜色 pLine.set(0, margin, progressOne*(width-textWidth-textMargin)/max, drawHeight); canvas.drawRoundRect(pLine, 5, 5, paint);//画进度 if (progressOne < progress) {//循环画 invalidate(); progressOne++; } } public synchronized int getMax() { return max; } public synchronized void setMax(int max) { if (max < 0) { throw new IllegalArgumentException("max not less than 0"); } this.max = max; } public synchronized int getProgress() { return progress; } public synchronized void setProgress(int progress) { if (progress < 0) { throw new IllegalArgumentException("progress not less than 0"); } if (progress > max) { progress = max; } if (progress <= max) { this.progress = progress; progressOne = 0; postInvalidate(); } } public int getTextColor() { return textColor; } public void setTextColor(int textColor) { this.textColor = textColor; } public float getTextSize() { return textSize; } public void setTextSize(float textSize) { this.textSize = textSize; } }
用到的其他代码:
<declare-styleable name="LineProgressBar"> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> <attr name="max" format="integer"></attr> <attr name="textIsDisplayable" format="boolean"></attr> <attr name="lineColor" format="color" /> <attr name="lineHeight" format="dimension" /> <attr name="lineProgressColor" format="color" /> <attr name="textWidth" format="dimension" /> <attr name="textMargin" format="dimension" /> </declare-styleable>
使用方式:xml
<....LineProgressBar android:id="@+id/details_line_progress" android:layout_width="match_parent" android:layout_height="10dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android_custom:lineColor="@color/whitebl" android_custom:lineHeight="5dp" android_custom:lineProgressColor="@color/title_color" android_custom:max="100" android_custom:textColor="@color/title_color" android_custom:textMargin="5dp" android_custom:textSize="14sp" android_custom:textWidth="30dp" > </....LineProgressBar>
使用方式:代码
LineProgressBar progress; ....//初始化 progress.setProgress(80);
相关文章推荐
- android-java与C++互调/对接SDK
- Android HandlerThread和IntentService用法和源码解析
- 【android】去掉标题栏
- listview 顶部添加和底部添加。
- android开发游记:酷炫的启动页面之如何实现两个ViewPager的联动
- 【转】macos下取消Android File Transfer自动启动
- android 自定义 View
- Android NDK 开发 第一步的几个坑
- Android上玩玩Hook?
- Android 开发常用代码片段
- android支付之银联支付
- Android 对话框【Dialog】去除白色边框代码
- Android开发常用工具类
- Android--动画Animation
- Android四个基本组件(2)之Service 服务与Content Provider内容提供商
- NativeActivity通过jni调用java层的Android API
- Android studio集合
- android 随机数Random
- Android屏幕适配全攻略(最权威的官方适配指导)
- Android实现带动画的下拉刷新RecyclerView