xml布局
<com.example.administrator.exam.MyCustomView
android:id="@+id/myview"
app:out_circle_width="200"
app:out_circle_color="#888888"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>
attrs
<attr name="out_circle_width" format="integer" />
<attr name="out_circle_color" format="color" />
<declare-styleable name="MyCustomView">
<attr name="out_circle_width" />
<attr name="out_circle_color" />
</declare-styleable>
MyCustomView
public class MyCustomView extends View{
private Paint out_mPaint,in_mPaint,text_mPaint;
private int out_circle_color;
private float out_circle_width;
private float into_circle_width=0;
private String schedule="0";
public float bitmapX;
public float bitmapY;
public void setSchedule(String schedule) {
this.schedule = schedule;
}
public void setInto_circle_width(float into_circle_width) {
this.into_circle_width = into_circle_width;
}
public MyCustomView(Context context) {
this(context,null);
}
public MyCustomView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
init(attrs);
}
public MyCustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyleAttr, 0);
int n = a.getIndexCount();
for (int i = 0; i < n; i++)
{
int attr = a.getIndex(i);
switch (attr)
{
case R.styleable.MyCustomView_out_circle_color:
out_circle_color = a.getColor(attr,Color.BLACK);
break;
case R.styleable.MyCustomView_out_circle_width:
// 默认颜色设置为黑色
out_circle_width = a.getInteger(attr, 0);
break;
}
}
a.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.e("1323", MeasureSpec.getSize(widthMeasureSpec) / 2+"" );
bitmapX=MeasureSpec.getSize(widthMeasureSpec) / 2 ;
bitmapY=MeasureSpec.getSize(heightMeasureSpec) / 2;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.translate(bitmapX, bitmapY);
canvas.drawCircle(0,0,out_circle_width,out_mPaint); // 绘制两个圆形
canvas.drawCircle(0,0,into_circle_width,in_mPaint);
canvas.drawText(schedule,0,50,text_mPaint);
}
private void setoutPaint() {
out_mPaint = new Paint();
out_mPaint.setAntiAlias(true); //消除锯齿
out_mPaint.setStrokeWidth(30);
out_mPaint.setColor(out_circle_color);
out_mPaint.setStyle(Paint.Style.STROKE); //绘制空心圆
}
private void setinPaint() {
in_mPaint = new Paint();
in_mPaint.setAntiAlias(true); //消除锯齿
in_mPaint.setStrokeWidth(30);
in_mPaint.setColor(Color.GREEN);
}
private void setTextPaint() {
text_mPaint = new Paint();
text_mPaint.setTextAlign(Paint.Align.CENTER);
text_mPaint.setAntiAlias(true); //消除锯齿
text_mPaint.setStrokeWidth(5);
text_mPaint.setTextSize(100);
text_mPaint.setColor(Color.GRAY);
}
public void init(AttributeSet attrs){
setoutPaint();
setinPaint();
setTextPaint();
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
public float i = 0;
private MyCustomView view;
private Handler h = new Handler() {
@Override
public void handleMessage(Message msg) {
int aaaa = (int) i;
if(aaaa==100){
t.cancel();
setViewtouch(view);
play.setEnabled(true);
}
view.setSchedule(aaaa+"%");
view.setInto_circle_width(aaaa*2);
view.invalidate();
Log.e("sssss", "handleMessage: ");
}
};
private Timer t;
private Button play;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (MyCustomView) findViewById(R.id.myview);
play = (Button) findViewById(R.id.play);
}
public void play(View view){
i=0;
t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
i += 0.2;
h.sendEmptyMessage(0);
}
},10,10);
play.setEnabled(false);
}
private void setViewtouch(final MyCustomView view) {
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
view.bitmapX = event.getX();
view.bitmapY = event.getY();
view.invalidate();
return true;
}
});
}
@Override
protected void onDestroy() {
if(t!=null){
t.cancel();
}
super.onDestroy();
}
}