本文教你如何定制属于自己的progressbar,效果图如下所视
代码如下:
//注意自定义View的宽和高如果指定了具体的宽高那么宽高就是指定的那个值,如果没用指定具体的宽或者高即用的wrap_content那么自定义View的宽高即为该父容器的大小
//注意获取View的宽和高:getMeasuredWidth()在setMearsuredDemision()中执行onMeasured()会调用该方法
//getWidth()在onLayout中执行而在执行onLayout之后onDraw()才会执行所以可以在onDraw中通过getWidth()获取view的宽和高
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class CostomProgressbar extends View {
private int firstColor=Color.parseColor("#00FFFF"),secondColor=Color.parseColor("#E0E0E0");
private int speed=90;
private int progress=0;
private boolean isChange=false;
private int width=20;
public CostomProgressbar(Context context) {
this(context,null);
}
public CostomProgressbar(Context context, AttributeSet attrs) {
super(context, attrs);
new Thread(){
public void run() {
while(true){
progress++;
if(progress>360){
progress=0;
isChange=!isChange;
}
postInvalidate();
try {
Thread.sleep(100-speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}.start();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(!isChange){
drawArc(firstColor,360,canvas);
drawArc(secondColor, progress,canvas);
}else {
drawArc(secondColor, 360,canvas);
drawArc(firstColor, progress,canvas);
}
}
private void drawArc(int color,int sweepAngle,Canvas canvas){
Paint paint=new Paint();
paint.setAntiAlias(true);
paint.setColor(color);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(width);
int cx=getMeasuredWidth()/2;
int cy=getMeasuredHeight()/2;
int radius=Math.min(getMeasuredHeight(), getMeasuredWidth())/2-width/2;
RectF rectf=new RectF(cx-radius, cy-radius, cx+radius, cy+radius);
canvas.drawArc(rectf, -90, sweepAngle, false, paint);
}
public void setColor(int firstColor,int secondColor){
this.firstColor=firstColor;
this.secondColor=secondColor;
}
public void setWitdh(int width){
this.width=width;
}
public void setSpeed(int seepd){
this.speed=speed;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical">
<com.solo.costomprograssbar.view.CostomProgressbar
android:id="@+id/id_cp"
android:layout_width="300dp"
android:layout_height="200dp"/>
</LinearLayout>
import com.solo.costomprograssbar.view.CostomProgressbar;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CostomProgressbar cp=new CostomProgressbar(this);
cp.setColor(Color.parseColor("#FFD306"), Color.parseColor("#FF2D2D"));
cp.setWitdh(15);
<span style="white-space:pre"> </span>cp.setSpeed(20);//这里没有起作用,难道是线程的缘故
addContentView(cp,new LayoutParams(100, 100)) ;
}
}