自定义view
public class Jindutiao extends View {
private Context context;
private Paint paint;
public Jindutiao(Context context) {
super(context);
this.context = context;
init();
}
public Jindutiao(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
public Jindutiao(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
init();
}
private void init() {
paint = new Paint();
paint.setColor(Color.parseColor("#cccfff"));
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(20);
}
private int index = 1;
public void diaoyong(int i) {
index = i;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RectF rectF = new RectF();
rectF.top = 0;
rectF.bottom = 100;
rectF.left = 0;
rectF.right = 100;
canvas.drawArc(rectF, 0, 72 * index, false, paint);
}
}
主页面
public class MainActivity extends AppCompatActivity {
private int i=1;
private Jindutiao jindutiao;
@SuppressLint("HandlerLeak")
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
jindutiao.diaoyong(i);
sendEmptyMessageDelayed(0,1000);
i++;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jindutiao = findViewById(R.id.jindu);
handler.sendEmptyMessage(0);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="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">
<com.example.yu.yuekao_demo07.Jindutiao
android:id="@+id/jindu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>