MainActivity.java
public class MainActivity extends AppCompatActivity {
CustomCircleView customCircleView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
customCircleView = findViewById(R.id.custom);
findViewById(R.id.custom_inside).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float degrees = (float)(720 + Math.random() * 1000);
RotateAnimation rotateAnimation = new RotateAnimation(0, -degrees, 350, 350);
rotateAnimation.setDuration(5000);
rotateAnimation.setFillAfter(true);
customCircleView.startAnimation(rotateAnimation);
}
});
//饼图的角度数组
//这里只是模拟数据,通常情况下,我们需要计算出每一个模块的百分比,然后计算出在整圆(360度)中所占的角度
float[] angles = new float[]{100, 60, 100, 50, 20, 30};
CustomBingView customBingView = findViewById(R.id.bing);
customBingView.setData(angles);
customBingView.invalidate();
}
}
CustomCircleView.java
/**
* 这里是画转盘的
* @author hasee
*/
public class CustomCircleView extends View{
Paint paint;
int mCircleCount = 6;
float mStartAngle = 0;
RectF rectF;
RectF re;
private String[] contents = new String[]{"美 女","女 神","热 舞","丰 满","性 感","知 性"};
public int[] colors = new int[]{Color.parseColor("#FF8247"),
Color.parseColor("#00EEEE"),
Color.parseColor("#ABB2FF"),
Color.parseColor("#FF00FF"),
Color.parseColor("#FFFF00"),
Color.parseColor("#00BFFF")};
public CustomCircleView(Context context) {
this(context,null);
}
public CustomCircleView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public CustomCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initData();
}
private void initData() {
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(10);
paint.setTextSize(20);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
rectF = new RectF();
rectF.left=100;
rectF.top=100;
rectF.bottom=600;
rectF.right=600;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int measuredWidth = getMeasuredWidth();
re = new RectF(0,0,measuredWidth,measuredWidth);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i=0;i<mCircleCount;i++){
/*if (i%2==0){
paint.setColor(Color.YELLOW);
}else {
paint.setColor(Color.LTGRAY);
}*/
paint.setColor(colors[i]);
canvas.drawArc(rectF,mStartAngle,60,true,paint);
mStartAngle+=60;
}
for(int i=0;i<6;i++){
paint.setColor(Color.BLACK);
Path path = new Path();
path.addArc(re,(i-1)*60+60,60);
canvas.drawTextOnPath(contents[i],path,40,160,paint);
}
}
}
CustomCircleInsideView.java
/**
* 转盘中间开始按钮和指针
* @author hasee
*/
public class CustomCircleInsideView extends View {
/**
* 画笔
*/
Paint mPaint;
RectF mRectF;
String mStr;
public CustomCircleInsideView(Context context) {
super(context);
init();
}
public CustomCircleInsideView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
//自定义属性,如何添加自定义属性如下(考点)
//第一步:在values文件夹下创建attrs.xml
//第二步:详见attrs.xml文件内部
//第三步:在所在的布局文件的根layout中添加xmlns:app="http://schemas.android.com/apk/res-auto"
//第四步:在布局文件的控件中添加app:"你在attrs中设置的attr name"="你的值"
//第五步:调用下面这句话,最后的为R.styleable.你在attrs中设置的declare-styleable name
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomCircleView);
//第六步:调用下面这句话,根据你在attrs中设置的format,选择getXXX方法,
//入参为 R.styleable. 加上 你在attrs中设置的declare-styleable name 加上 _ 加上 你在attrs中设置的attr name
mStr = typedArray.getString(R.styleable.CustomCircleView_text);
init();
}
private void init() {
//以下注释请看CustomBingView里面
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(10);
mPaint.setTextSize(40);
mPaint.setStyle(Paint.Style.FILL);
mRectF = new RectF();
mRectF.top = 120;
mRectF.bottom = 350;
mRectF.right = 450;
mRectF.left = 250;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//设置画笔颜色为黑色,
mPaint.setColor(Color.parseColor("#9A32CD"));
//画出指针,用一个扇形,然后盖住后面补分来简单表示
canvas.drawArc(mRectF, 60, 60, true, mPaint);
mPaint.setColor(Color.parseColor("#9A32CD"));
//画一个红色的圆形,就是中间的大按钮
canvas.drawCircle(350,350, 70, mPaint);
mPaint.setColor(Color.BLACK);
//添加按钮上的文字
canvas.drawText(mStr, 300, 370, mPaint);
}
}
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- name为想要调用这个属性的类名即可 -->
<declare-styleable name="CustomCircleView">
<!-- name为属性的名字,可以随意起,只要符合规则看得懂 -->
<!-- format为属性内容的类型 -->
<attr name="text" format="string"></attr>
</declare-styleable>
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.bawei.myapplication.view.CustomCircleView
android:id="@+id/custom"
android:layout_width="wrap_content"
android:layout_height="500dp"
android:visibility="gone"
/>
<com.bawei.myapplication.view.CustomCircleInsideView
android:id="@+id/custom_inside"
android:layout_width="wrap_content"
android:layout_height="500dp"
app:text="开始"
android:background="#00000000"
android:visibility="gone"/>
<com.bawei.myapplication.view.CustomBingView
android:id="@+id/bing"
android:layout_width="wrap_content"
android:layout_height="500dp" />
</RelativeLayout>
CustomBingView.java
/**
* 画饼图的view
* @author hasee
*/
public class CustomBingView extends View{
/**
* 记录传入的角度
*/
float[] mAngles;
/**
* 位置控制
*/
RectF mRectF;
/**
* 画笔
*/
Paint mPaint;
/**
* 起始角度
*/
float mStartAngle = 0;
public CustomBingView(Context context) {
super(context);
init();
}
public CustomBingView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
/**
* 设置每个扇形角度
* @param angles 角度数组
*/
public void setData(float[] angles){
mAngles = angles;
}
/**
* 初始化
*/
private void init() {
//初始化画笔
mPaint = new Paint();
//设置画笔颜色为蓝色
mPaint.setColor(Color.BLUE);
//设置宽度
mPaint.setStrokeWidth(10);
//设置填充模式
mPaint.setStyle(Paint.Style.FILL);
//设置上下左右位置
mRectF = new RectF();
mRectF.top = 100;
mRectF.bottom = 400;
mRectF.right = 550;
mRectF.left = 250;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for(int i = 0; i < mAngles.length; i++){
if(i % 2 == 0 ){
//角标为双数时,设置画笔颜色为蓝色
mPaint.setColor(Color.BLUE);
}else{
//角标为单数时,设置画笔颜色为绿色
mPaint.setColor(Color.GREEN);
}
//画扇形
//第一个参数,位置信息
//第二个参数,起始角度
//第三个参数,从起始角度开始,顺时针旋转角度
//第四个参数,是否延申到圆心
//第五个参数,画笔
canvas.drawArc(mRectF, mStartAngle, mAngles[i], true, mPaint);
//下一个扇形的起始角度 = 本次扇形起始角度 + 本次扇形旋转角度
mStartAngle += mAngles[i];
}
}
}
CustomGestureView.java
/**
* 这里是手势拖动
* @author hasee
*/
public class CustomGestureView extends View{
Paint mPaint;
float mTouchX = 300, mTouchY = 300, mCircleRadius = 50;
public CustomGestureView(Context context) {
super(context);
init();
}
public CustomGestureView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(10);
mPaint.setStyle(Paint.Style.FILL);
//监听touch事件
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//获取点击位置
mTouchX = event.getX();
mTouchY = event.getY();
//刷新view
invalidate();
return true;
}
});
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//画圆
//第一个参数,圆心x轴位置
//第二个参数,圆心y轴位置
//第三个参数,圆形半径
//第四个参数,画笔
canvas.drawCircle(mTouchX, mTouchY, mCircleRadius, mPaint);
}
}