干這行這么久了還是第一次寫文章爬立,試試水万哪。有什么不足還請多多指教,
主要代碼(繪制兩個圓弧奕巍,和文字)
/**
*調(diào)用onDraw繪制邊框
*/
@Override
protected void onDraw(Canvascanvas) {
//TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(Color.green(R.color.green));//設(shè)置背景顏色
//創(chuàng)建一個RectF,用來限定繪制圓弧的范圍
RectFrectf= newRectF();
//設(shè)置畫筆的顏色
mPaint.setColor(Color.WHITE);
//設(shè)置進度是實心還是空心
mPaint.setStrokeWidth(2);//設(shè)置圓環(huán)的寬度
//? ? ? ? //設(shè)置畫筆的樣式的止,空心
mPaint.setStyle(Paint.Style.STROKE);
//設(shè)置抗鋸齒
mPaint.setAntiAlias(true);
//設(shè)置畫得一個半徑,然后比較長和寬诅福,以最大的值來確定長方形的長寬,確定半徑
intr=getMeasuredWidth()>getMeasuredHeight()?getMeasuredWidth():getMeasuredHeight();
//如果設(shè)置的padding不一樣繪制出來的是橢圓形氓润。繪制的時候考慮padding
//Log.i("邊界", "寬度"+getMeasuredWidth()+"高度"+getMeasuredHeight()+"getPaddingLeft()"+getPaddingLeft()+"getPaddingTop"+getPaddingTop()+"getPaddingRight(): "+getPaddingRight()+"getPaddingBottom()"+getPaddingBottom());
//當padding都為0的時候,繪制出來的就是RectF限定的區(qū)域就是一個正方形
intp=dip2px(context,2);
rectf.set(getPaddingLeft()+p,getPaddingTop()+p,r-getPaddingRight()-p,r-getPaddingBottom()-p);
//繪制圓弧
canvas.drawArc(rectf,-25,-130,false,mPaint);
rectf.set(getPaddingLeft()+p,getPaddingTop()+p,r-getPaddingRight()-p,r-getPaddingBottom()-p);
//繪制圓弧
canvas.drawArc(rectf,25,130,false,mPaint);
//繪制文字
mPaint.setColor(Color.WHITE);
mPaint.setStrokeWidth(0);
mPaint.setTextSize(12);
mPaint.setTextAlign(Paint.Align.CENTER);
mPaint.setStyle(Paint.Style.STROKE);//畫空心矩形
canvas.drawText(time,r/2,r/2+dip2px(context,3),mPaint);
}
設(shè)置倒計時要更新Draw
public voidsetTime(Stringtime){
this.time=time;
invalidate();//更新
}
在線程中設(shè)置倒計時
private voidcalculationTime(){
//? ? ? ? time = ((min == 0?"00":min)&&(second == 0?"00" :second));
if(min==0){//讀秒
//倒計時結(jié)束
if(second==0){
time="00:00";
//倒計時停止
if(timer!= null) {
timer.cancel();
timer= null;
}
if(timerTask!= null) {
timerTask= null;
}
}else{
second--;
if(second>=10) {//10秒以上
time="0"+min+":"+second;
}else{//10秒以內(nèi)
time="0"+min+":0"+second;
}
}
}else{//讀分鐘和秒
if(second==0){//分鐘數(shù)-1咖气,秒遞減
second=59;
min--;
if(min>=10) {
time=min+":"+second;
}else{
time="0"+min+":"+second;
}
}else{
second--;
if(second>=10){
if(min>=10) {
time=min+":"+second;
}else{
time="0"+min+":"+second;
}
}else{
if(min>=10) {
time=min+":0"+second;
}else{
time="0"+min+":0"+second;
}
}
}
}
}
在xml中的調(diào)用
android:orientation="vertical"android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green">
android:id="@+id/countView"
android:layout_width="25dp"
android:layout_height="25dp"
android:gravity="center"
android:textColor="@android:color/darker_gray"
android:layout_margin="10dp"
android:padding="2dp"
/>
呼。浅役。。完了悯舟,其實還有另一種方法,直接畫一個環(huán)形的圓抵怎,然后在覆蓋一個矩形岭参,最后在矩形上繪制文字。
有什么問題演侯,給我留言吧。一起學(xué)習(xí)秒际,一起進步!=坪骸!