热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

自定义progressbar控件

先来看一下图片效果:github地址:https:github.com1181631922OrliteDemotreemaster里面还有一些其他的

先来看一下图片效果:

github地址:https://github.com/1181631922/OrliteDemo/tree/master

里面还有一些其他的demo


花了半天时间吧,大体实现了一下,发现自己对自定义view还是不熟,有待提高,下面来上一下实现代码,总体实现还是比较简单的:

先看自定义view类:

package com.fanyafeng.orlitedemo.myview;import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;/*** Created by 365rili on 16/4/20.*/
public class PercentProgressBarView extends View {private Paint reachPaint;private Paint unReachPaint;private Paint textPaint;private int reachPaintColor;private int unReachPaintColor;private int textPaintColor;private int percent;private String progressText;private String initText;private String finalText;public int getReachPaintColor() {return reachPaintColor;}public void setReachPaintColor(int reachPaintColor) {this.reachPaintColor &#61; reachPaintColor;}public int getUnReachPaintColor() {return unReachPaintColor;}public void setUnReachPaintColor(int unReachPaintColor) {this.unReachPaintColor &#61; unReachPaintColor;}public int getTextPaintColor() {return textPaintColor;}public void setTextPaintColor(int textPaintColor) {this.textPaintColor &#61; textPaintColor;}public String getInitText() {return initText;}public void setInitText(String initText) {this.initText &#61; initText;setPercent(0, initText);}public String getFinalText() {return finalText;}public void setFinalText(String finalText) {this.finalText &#61; finalText;setPercent(100, finalText);}public int getPercent() {return percent;}public void setPercent(int percent, String progressText) {this.percent &#61; percent;this.progressText &#61; progressText;invalidate();}public String getProgressText() {return progressText;}public PercentProgressBarView(Context context) {super(context);}public PercentProgressBarView(Context context, AttributeSet attrs) {super(context, attrs);reachPaint &#61; new Paint(Paint.ANTI_ALIAS_FLAG);unReachPaint &#61; new Paint(Paint.ANTI_ALIAS_FLAG);textPaint &#61; new Paint();}public PercentProgressBarView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}&#64;Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);reachPaint.setAntiAlias(true);if (reachPaintColor !&#61; 0) {reachPaint.setColor(reachPaintColor);} else {reachPaint.setColor(Color.parseColor("#3498DB"));}reachPaint.setStyle(Paint.Style.STROKE);unReachPaint.setAntiAlias(true);if (unReachPaintColor !&#61; 0) {unReachPaint.setColor(unReachPaintColor);} else {unReachPaint.setColor(Color.parseColor("#CCCCCC"));}unReachPaint.setStyle(Paint.Style.STROKE);textPaint.setAntiAlias(true);if (textPaintColor !&#61; 0) {textPaint.setColor(textPaintColor);} else {textPaint.setColor(Color.BLUE);}float endX &#61; getRight();float startY &#61; getTop();float endY &#61; getBottom();float height &#61; getHeight();// 画笔所画的高度reachPaint.setStrokeWidth(height);unReachPaint.setStrokeWidth(height);// Log.d("TAG", "startx:" &#43; startX &#43; "endx:" &#43; endX &#43; "starty:" &#43; startY &#43; "endy:" &#43; endY &#43; "height:" &#43; height &#43; "width:" &#43; width);// 画完成进度textPaint.setTextSize(40);textPaint.setTextAlign(Paint.Align.LEFT);textPaint.setStrokeWidth(10);if (progressText !&#61; null) {if (percent >&#61; 0 && percent <&#61; 100) {Rect textBounds &#61; new Rect();textPaint.getTextBounds(progressText, 0, progressText.length(), textBounds);Paint.FontMetricsInt fontMetricsInt &#61; textPaint.getFontMetricsInt();float baseLine &#61; (getMeasuredHeight() - fontMetricsInt.bottom &#43; fontMetricsInt.top) / 2 - fontMetricsInt.top;float tempEndX &#61; percent * (endX - textBounds.width()) / 100;Log.d("TAG", "percent数值:" &#43; percent);Log.d("TAG", "进度条整体长度:" &#43; endX);Log.d("TAG", "进度条显示:" &#43; tempEndX);// 进度canvas.drawText(progressText, tempEndX - textBounds.width(), baseLine, textPaint);
// 画左侧已完成canvas.drawLine(0, (endY - startY) / 2, tempEndX - (textBounds.width() &#43; 30), (endY - startY) / 2, reachPaint);
// 画右侧未完成canvas.drawLine(tempEndX&#43;textBounds.width()/2, (endY - startY) / 2, endX, (endY - startY) / 2, unReachPaint);/*
// 进度canvas.drawText(progressText, endX / 2 - textBounds.width() / 2, baseLine, textPaint);
// 画左侧已完成canvas.drawLine(0, (endY - startY) / 2, endX / 2 - textBounds.width() / 2 - 10, (endY - startY) / 2, reachPaint);
// 画右侧未完成canvas.drawLine(endX / 2 &#43; textBounds.width() / 2 &#43; 10, (endY - startY) / 2, endX, (endY - startY) / 2, unReachPaint);
**/}}}}
再看一下xml&#xff1a;





ok&#xff0c;最后看一下activity&#xff1a;

package com.fanyafeng.orlitedemo.activity;import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;import com.fanyafeng.orlitedemo.R;
import com.fanyafeng.orlitedemo.myview.PercentProgressBarView;import org.w3c.dom.Text;public class ProgressBarActivity extends AppCompatActivity {private PercentProgressBarView my_progress;private Handler handler;private int count &#61; 0;private Runnable runnable;&#64;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_progress_bar);Toolbar toolbar &#61; (Toolbar) findViewById(R.id.toolbar);setSupportActionBar(toolbar);FloatingActionButton fab &#61; (FloatingActionButton) findViewById(R.id.fab);fab.setOnClickListener(new View.OnClickListener() {&#64;Overridepublic void onClick(View view) {Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();}});initView();initData();}private void initView() {my_progress &#61; (PercentProgressBarView) findViewById(R.id.my_progress);my_progress.setInitText("开始下载");my_progress.setTextPaintColor(Color.BLACK);}private void initData() {handler &#61; new Handler(Looper.getMainLooper());runnable &#61; new Runnable() {&#64;Overridepublic void run() {if (count <100) {count&#43;&#43;;my_progress.setPercent(count,count&#43;"%");handler.postDelayed(this,100);Log.d("TAG","百分比:"&#43;count);
// if (count&#61;&#61;100){
// my_progress.setFinalText("下载完毕");
// }}}};handler.postDelayed(runnable, 1000);}&#64;Overrideprotected void onStop() {super.onStop();handler.removeCallbacks(runnable);}
}
后期还会慢慢完善&#xff0c;自己想加入控制类的方法自己可以定义get和set方法&#xff0c;第一次写自定义view还有好多问题吧&#xff0c;只是简陋的实现了功能&#xff0c;还有很多需要优化&#xff0c;细化

写给自己&#xff1a;贵在自知



推荐阅读
author-avatar
mobiledu2502931077
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有