自定义一个View继承View类
public class MyVicrClass extends View { Timer timer = null; int outColor = Color.BLACK; float du = 0; Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); invalidate(); if(du>=360){ timer.cancel(); } } }; public MyVicrClass(Context context) { super(context); } public MyVicrClass(Context context, AttributeSet attrs) { super(context, attrs); } public MyVicrClass(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public MyVicrClass(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint pa = new Paint(); Paint p1=new Paint(); Paint p2=new Paint(); float fw=getWidth()/2; float fh=getHeight()/2; Paint p3 = new Paint(); p3.setColor(Color.RED); RectF rectF = new RectF(getWidth()/2-100,getHeight()/2-100,getWidth()/2+100,getHeight()/2+100); canvas.drawArc(rectF,0,du,true,p3); pa.setColor(Color.BLACK); p1.setColor(outColor); p2.setColor(Color.WHITE); pa.setTextSize(20); p1.setAntiAlias(true); p2.setAntiAlias(true); canvas.drawCircle(fw,fh,80,p1); canvas.drawCircle(fw,fh,60,p2); canvas.drawText("100%",fw,fh,pa); } @Override public boolean onTouchEvent(MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ if(event.getX()<getWidth()/2&&event.getY()<250){ timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { du+=12; handler.sendEmptyMessage(1); } },1000,1000); }if(event.getX()>getWidth()/2&&event.getY()<250){ du=0; handler.sendEmptyMessage(1); }if(event.getY()<getHeight()-100){ outColor=Color.BLUE; invalidate(); } } return super.onTouchEvent(event); } }
这是布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.bawei.wangyiming2017109.MainActivity"> <com.bawei.wangyiming2017109.MyVicrClass android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/id1"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/btn_start" android:text="开始"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/btn_reset" android:text="重置"/> </LinearLayout> </RelativeLayout>
这是MainActivity
package com.bawei.wangyiming2017109; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.util.Timer; import java.util.logging.Handler; public class MainActivity extends AppCompatActivity { Button btn_start; Button btn_reset; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_start = (Button) findViewById(R.id.btn_start); btn_reset = (Button) findViewById(R.id.btn_reset); btn_start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this,"开始",Toast.LENGTH_SHORT).show(); } }); btn_reset.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this,"重置",Toast.LENGTH_SHORT).show(); } }); } }