//自定义一个类
public class MyView extends View {
private Paint paint;
private int mWidth;
private int mHeight;
private int RX;
private int RY;
private int one;
private int two;
private int three;
private int rect;
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
//获取控件的宽度
mWidth = w;
//获取控件的高度
mHeight = h; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas);
paint = new Paint();
//距离控件有一定的距离
rect = mWidth * 3 / 4;
int R = rect * 8 / 25;
int width = R * 3 / 8;
//小圆的半径
int r = rect / 2 - width * 2;
//圆点坐标
RX = mWidth / 2;
RY = mHeight / 2;
one = r;
two = r + width;
three = r + width * 2;
int left = mWidth / 2 - rect / 2;
int right = left + rect;
int top = mHeight / 2 - rect / 2;
int bottom = mHeight - top;
paint.setStrokeWidth(width);
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
//矩形
canvas.drawRect(left, top, right, bottom, paint);
//圆
paint.setColor(Color.WHITE);
canvas.drawCircle(RX, RY, r, paint);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(RX, RY, R, paint);
paint.setColor(Color.BLUE);
canvas.drawCircle(RX, RY, R + width, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) {
if (x + getLeft() < getRight() && y + getTop() < getBottom()) {
int type = 0;
int distance = getDistance(x, y);
if (distance <= one * one) {
type = 0;
} else if (distance > one && distance <= two * two) {
type = 1;
} else if (distance > two && distance <= three * three) {
type = 2;
} else if (x >= RX - rect / 2 && x <= RX + rect / 2 && y >= RY - rect / 2 && y <= RY + rect / 2) {
type = 3;
} else {
type = 4;
}
if (onWhatListener != null) {
onWhatListener.onWhat(type);
}
}
}
return true;
}
private int getDistance(int x, int y) {
return (x - RX) * (x - RX) + (y - RY) * (y - RY);
}
public onWhatListener onWhatListener;
public interface onWhatListener {
void onWhat(int type);
}
public void setOnWhatListener(MyView.onWhatListener onWhatListener) {
this.onWhatListener = onWhatListener;
}
}
主Activity
public class MainActivity extends AppCompatActivity {
private TextView my_tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
MyView myView = (MyView) findViewById(R.id.myview);
my_tv = (TextView) findViewById(R.id.my_tv);
myView.setOnWhatListener(new MyView.onWhatListener() {
@Override
public void onWhat(int type) {
switch (type) {
case 0:
my_tv.setText("白色");
break;
case 1:
my_tv.setText("红色");
break;
case 2:
my_tv.setText("蓝色");
break;
case 3:
my_tv.setText("绿色");
break;
case 4:
my_tv.setText("外太空");
break;
}
}
});
}
}
自定义View探究
最新推荐文章于 2021-12-26 01:17:17 发布