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">
<jx.com.day2_customview.CustomDiskView
android:id="@+id/main_disk"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<jx.com.day2_customview.CustomCircleView
android:id="@+id/main_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
Main 主页面
package jx.com.day2_customview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.RotateAnimation;
public class MainActivity extends AppCompatActivity {
CustomCircleView customCircleView;
CustomDiskView diskView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取资源id
customCircleView = findViewById(R.id.main_custom);
diskView = findViewById(R.id.main_disk);
//点击事件(旋转)
customCircleView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//设置一个随机数
int degress = (int) (720 + Math.random()*1000);
RotateAnimation rotateAnimation = new RotateAnimation(0, degress, 400, 600);
//动画时间
rotateAnimation.setDuration(5000);
//设置动画停止时不在原始地方
rotateAnimation.setFillAfter(true);
//设置动画
diskView.startAnimation(rotateAnimation);
}
});
}
}
CustomDiskView 自定义View
package jx.com.day2_customview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
public class CustomDiskView extends View {
Paint paint;
int circlecount = 6;
int mStartAngle = 0;
int text = 10;
private RectF rectF;
public CustomDiskView(Context context) {
super(context);
init();
}
public CustomDiskView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
private void init(){
//声明
paint = new Paint();
rectF = new RectF();
rectF.left = 100;
rectF.top = 300;
rectF.right = 700;
rectF.bottom = 900;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
//定义字符串
private String[] contents = new String[]{"美 女", "女 神", "热 舞", "丰 满", "性 感", "知 性"};
public int[] colors = new int[]{Color.parseColor("#8EE5EE"), Color.parseColor("#FFD700"), Color.parseColor("#FFD39B"), Color.parseColor("#FF8247"), Color.parseColor("#FF34B3"), Color.parseColor("#F0E68C")};
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//循环添加颜色,圆弧,圆弧上的字体
for (int i = 0; i < circlecount; i++) {
//设置颜色
paint.setColor(colors[i]);
//设置圆弧
canvas.drawArc(rectF,mStartAngle,60,true,paint);
//设置字体大小
paint.setTextSize(30);
//设置宽度
paint.setStrokeWidth(10);
//设置颜色
paint.setColor(Color.BLACK);
Path path = new Path();
path.addArc(rectF,text,60);
canvas.drawTextOnPath(contents[i],path,60,60,paint);
mStartAngle+=60;
text+=60;
}
}
}
CustomCircleView 自定义View
package jx.com.day2_customview;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
public class CustomCircleView extends View {
Paint paint;
public CustomCircleView(Context context) {
super(context);
init();
}
public CustomCircleView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
private void init(){
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(60);
paint.setStrokeWidth(10);
paint.setColor(Color.BLUE);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RectF rectF = new RectF();
rectF.top = 450;
rectF.left = 375;
rectF.right = 425;
rectF.bottom = 500;
//设置颜色
paint.setColor(Color.BLACK);
//设置一个小指针(小的圆弧)
canvas.drawArc(rectF,60,60,true,paint);
paint.setColor(Color.RED);
//设置一个圆(中间的小圆)
canvas.drawCircle(400, 600, 100, paint);
paint.setColor(Color.BLACK);
//设置字体(按钮字体)
canvas.drawText("start",340,620,paint);
}
}