现在的app下载的时候是下载的时候使用一个圆然后根据下载的进度进行显示:
自定义个atterx.xml文件:
然后进行layout.xml中调用:
xmlns:android_progress="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="START" />
android:id="@+id/round_progress_bar"
android:layout_width="60dp"
android:layout_height="60dp"
android_progress:roundColor="@color/skyblue"
android_progress:roundWidth="10dip"
android_progress:roundProgressColor="@color/red" />
然后在自定义一个view的实现类:public class RoundProgressBar extends View {
private int roundColor;
private float roundWidth;
private int roundProgressColour;
private Paint paint;
private int progress;
public RoundProgressBar(Context context) {
this(context, null);
}
public RoundProgressBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RoundProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint = new Paint();
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);
roundColor = typedArray.getColor(R.styleable.RoundProgressBar_roundColor, R.color.skyblue);
roundWidth = typedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);
roundProgressColour = typedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, R.color.red);
typedArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int centre = getWidth() / 2;
int radius = (int) (centre - roundWidth / 2);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(roundWidth);
paint.setColor(roundColor);
canvas.drawCircle(centre, centre, radius, paint);
paint.setStrokeWidth(roundWidth);
paint.setColor(roundProgressColour);
RectF rectF = new RectF(centre - radius, centre - radius, centre + radius, centre + radius);
paint.setStyle(Paint.Style.STROKE);
canvas.drawArc(rectF, 270, 360 * progress / 100, false, paint);
}
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 > 100) {
progress = 100;
}
if (progress <= 100) {
this.progress = progress;
postInvalidate();//这句很重要,他表示了每一次进来后都需要进行重绘
}
}
}
在mainactivity中进行数据的调用:
public class MainActivity extends ActionBarActivity {
RoundProgressBar progressBar;
int progress = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.start);
progressBar = (RoundProgressBar) findViewById(R.id.round_progress_bar);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
while (progress <= 100) {
Log.e("test", progress + "");
progress += 3;
progressBar.setProgress(progress);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
});
}
http://download.csdn.net/detail/xushuangshuang1/8573645这是源代码