android自定义环形进度条怎么自由控制进度条的消失与隐藏?怎样封装可以实现在代码中动态添加这个自定义View?
从网上找了Demo可以实现环形进度条的效果,但是环形进度条的显示时间是我们自己定义的,在acitivity中我通过Timer实现了这个方法:具体思路是通过Timer每隔100ms执行一次mSearchHander.setEmptyMessage(0);然后同过mCnt计数,mCent=100之后让环形进度条消失。
不知道这样的思路对不对,希望各位指点
但是现在要求把这个自定义环形进度条封装一下,可以在代码中随时控制进度条的出现/消失/和出现的时间?如何完成这样的封装,求给位大神给个思路
mainActivity中
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetoothmeter);
mhandler = new Handler();
//通过mSearcheHandler更新UI
mSearchHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==0){
//改变环形进度条的进度
mCircleProcessBar.setProgress(mcnt);
mcnt+=1;
if(MCNT_MAX<=mcnt){
mCircleProcessBar.setVisibility(View.GONE);
}
}
}
};
mBlueService = BluetoothLeService.getInstance();
initView();
initEvent();
}
private void initEvent()
{
mImgBtnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
//搜索
mbtnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Prepare list view and initiate scanning
if (m_devicelistadapter != null) {
m_devicelistadapter.clear();
m_devicelistadapter.notifyDataSetChanged();
}
startScan();
//设置圆环进度条的出现时间(秒)
new TimerDelay(mSearchHandler).setDelayTime(CIRCLE_APPARE_TIME,mCircleProcessBar);
mcnt=1;
}
});
}
TimeDelay.java代码如下:
public class TimerDelay {
public static Handler mHandler;
public TimerDelay(Handler mHandler) {
this.mHandler=mHandler;
}
public static void setDelayTime(int delayTime,
MyCircleProcessBar mprobar){
int time =delayTime*10;
mprobar.setVisibility(View.VISIBLE);
Timer timer =new Timer();
timer.schedule(new MyTask(),0,time);
}
static class MyTask extends java.util.TimerTask{
public void run(){
mHandler.sendEmptyMessage(0);
}
}
}
最后附上自定义View的代码:
public class MyCircleProcessBar extends View{
private static final String TAG = "CircleProgressBar";
private int mMaxProgress = 100;
private int mProgress = 0;
private final int mCircleLineStrokeWidth = 8;
private final int mTxtStrokeWidth = 2;
// 画圆所在的距形区域
private final RectF mRectF;
private final Paint mPaint;
private final Context mContext;
private String mTxtHint1;
private String mTxtHint2;
public MyCircleProcessBar(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mRectF = new RectF();
mPaint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = this.getWidth();
int height = this.getHeight();
if (width != height) {
int min = Math.min(width, height);
width = min;
height = min;
}
// 设置画笔相关属性
mPaint.setAntiAlias(true);
mPaint.setColor(Color.rgb(0xe9, 0xe9, 0xe9));
canvas.drawColor(Color.TRANSPARENT);
mPaint.setStrokeWidth(mCircleLineStrokeWidth);
// mPaint.setStyle(Style.STROKE);
mPaint.setStyle(Paint.Style.STROKE);
// 位置
mRectF.left = mCircleLineStrokeWidth / 2; // 左上角x
mRectF.top = mCircleLineStrokeWidth / 2; // 左上角y
mRectF.right = width - mCircleLineStrokeWidth / 2; // 左下角x
mRectF.bottom = height - mCircleLineStrokeWidth / 2; // 右下角y
// 绘制圆圈,进度条背景
canvas.drawArc(mRectF, -90, 360, false, mPaint);
mPaint.setColor(Color.rgb(0xf8, 0x60, 0x30));
canvas.drawArc(mRectF, -90, ((float) mProgress / mMaxProgress) * 360, false, mPaint);
// 绘制进度文案显示
mPaint.setStrokeWidth(mTxtStrokeWidth);
String text = mProgress + "%";
int textHeight = height / 4;
mPaint.setTextSize(textHeight);
int textWidth = (int) mPaint.measureText(text, 0, text.length());
// mPaint.setStyle(Style.FILL);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawText(text, width / 2 - textWidth / 2, height / 2 + textHeight / 2, mPaint);
if (!TextUtils.isEmpty(mTxtHint1)) {
mPaint.setStrokeWidth(mTxtStrokeWidth);
text = mTxtHint1;
textHeight = height / 8;
mPaint.setTextSize(textHeight);
mPaint.setColor(Color.rgb(0x99, 0x99, 0x99));
textWidth = (int) mPaint.measureText(text, 0, text.length());
// mPaint.setStyle(Style.FILL);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawText(text, width / 2 - textWidth / 2, height / 4 + textHeight / 2, mPaint);
}
if (!TextUtils.isEmpty(mTxtHint2)) {
mPaint.setStrokeWidth(mTxtStrokeWidth);
text = mTxtHint2;
textHeight = height / 8;
mPaint.setTextSize(textHeight);
textWidth = (int) mPaint.measureText(text, 0, text.length());
// mPaint.setStyle(Style.FILL);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawText(text, width / 2 - textWidth / 2, 3 * height / 4 + textHeight / 2, mPaint);
}
}
public int getMaxProgress() {
return mMaxProgress;
}
public void setMaxProgress(int maxProgress) {
this.mMaxProgress = maxProgress;
}
public void setProgress(int progress) {
this.mProgress = progress;
this.invalidate();
}
public void setProgressNotInUiThread(int progress) {
this.mProgress = progress;
this.postInvalidate();
}
public String getmTxtHint1() {
return mTxtHint1;
}
public void setmTxtHint1(String mTxtHint1) {
this.mTxtHint1 = mTxtHint1;
}
public String getmTxtHint2() {
return mTxtHint2;
}
public void setmTxtHint2(String mTxtHint2) {
this.mTxtHint2 = mTxtHint2;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论