自定义view可拖动的小圆点
/**
* Created by donghe on 2019/7/7.
*/
public class MyView extends View {
//定义画笔和初始位置
Paint p = new Paint();
public float currentX = 50;
public float currentY = 50;
public int textColor;
public MyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
//获取资源文件textColor的值
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.myView);
textColor = typedArray.getColor(R.styleable.myView_TextColor, Color.BLACK);
typedArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//画一个绿色的圆
p.setColor(Color.GREEN);
canvas.drawCircle(currentX,currentY,30,p);
p.setColor(textColor);
canvas.drawText("BY finch",currentX-30,currentY+50,p);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
currentX = event.getX();
currentY = event.getY();
invalidate();
return true;
}
}