import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Timer;
public class MainActivity extends AppCompatActivity {
private Button btnAdd;
private ProgressView pv;
private static final int FLAG = 0x123;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case FLAG:
int progress = pv.getProgress();
progress += 10;
if (progress > 100) {
progress = 0;
}
pv.setProgress(progress);
handler.sendEmptyMessageDelayed(FLAG, 1000);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = (Button) findViewById(R.id.btn_add);
pv = (ProgressView) findViewById(R.id.pv_data);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// int progress = pv.getProgress();
// progress += 30;
// pv.setProgress(progress);
handler.sendEmptyMessageDelayed(FLAG, 1000);
}
});
pv.setListener(new ProgressView.ProgressUpdateListener(){
@Override
public void onProgressUpdated(int progress) {
if (progress >= 100) {
// handler.remove;
startActivity(new Intent(MainActivity.this, ScanActivity.class));
}
}
});
}
@Override
protected void onStop() {
super.onStop();
handler.removeMessages(FLAG);
}
}
-------------------------------------------------------------------------------------------
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by WuXirui
* Create Time: 2017/11/4
* Description:
*/
public class ProgressView extends View {
private ProgressUpdateListener listener;
interface ProgressUpdateListener {
void onProgressUpdated(int progress);
}
public void setListener(ProgressUpdateListener listener) {
this.listener = listener;
}
private int progress = 0;
public ProgressView(Context context) {
super(context);
}
public ProgressView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public int getProgress() {
return progress;
}
public void setProgress(int progress) {
this.progress = progress;
postInvalidate();
if (listener != null) {
listener.onProgressUpdated(progress);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = getWidth();
int height = getHeight();
// 绘图范围
int react = Math.min(width, height);
// 半径
int radius = react / 2;
// 绘制最外层的大圆
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(radius, radius, radius, paint);
paint.reset();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(20);
paint.setColor(Color.RED);
// 画扇形
canvas.drawArc(10, 10, react-10, react-10, -90, progress*360/100, true, paint);
// 画内层的小圆
paint.reset();
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawCircle(radius, radius, radius - 20, paint);
// 画进度文字
paint.reset();
paint.setColor(Color.BLUE);
paint.setTextSize(40);
Rect rect = new Rect();
paint.getTextBounds(progress + "", 0, String.valueOf(progress).length(),
rect);
int textWidth = rect.width();
int textHeight = rect.height();
canvas.drawText(progress + "", radius-textWidth/2, radius-textHeight/2, paint);
}
public class ScanActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
}
}