Java 算法改进:在定义形状的边界上保持圆

Java 算法改进:在定义形状的边界上保持圆,java,android,xml,android-studio,Java,Android,Xml,Android Studio,我写了一个代码,在我触摸的任何地方画一个圆圈,它将保持在较大的圆圈内或任何形状,如果你一直按下它,它将保持在那里,但如果你触摸或走出圆圈,它将返回到中心。但每当我只是按住我的触摸,我希望较小的圆圈在离开较大的圆圈时保持在边界处。 这是我的密码: public class MainActivity extends Activity implements OnTouchListener { static private float x; static private float

我写了一个代码,在我触摸的任何地方画一个圆圈,它将保持在较大的圆圈内或任何形状,如果你一直按下它,它将保持在那里,但如果你触摸或走出圆圈,它将返回到中心。但每当我只是按住我的触摸,我希望较小的圆圈在离开较大的圆圈时保持在边界处。 这是我的密码:

public class MainActivity extends Activity implements OnTouchListener  {
     static private float x;
     static private float y;
    static float lasttouchx;
    static float lasttouchy;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MyCustomPanel view = new MyCustomPanel(this);

    ViewGroup.LayoutParams params =
            new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT);
    addContentView(view, params);
    view.setOnTouchListener(this);

}

private class MyCustomPanel extends View {


    public MyCustomPanel(Context context) {
        super(context);

    }
    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        Paint paint = new Paint();



        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(20);
        paint.setAntiAlias(true);
        if(lasttouchx!=0&&lasttouchy!=0) {
            paint.setColor(Color.BLACK);
            canvas.drawCircle(lasttouchx, lasttouchy, 400, paint);
            paint.setStrokeWidth(5);
            canvas.drawLine(lasttouchx, lasttouchy - 400, lasttouchx, lasttouchy + 400, paint);
            canvas.drawLine(lasttouchx- 400, lasttouchy , lasttouchx+400,lasttouchy,paint);
        }
        else
        {}
        paint.setColor(Color.YELLOW);

        paint.setStrokeWidth(5);
        paint.setTextSize(50);
        canvas.drawText(" X : " + (int) x + "\n Y : " + (int) y,                  canvas.getWidth() - 500, 200, paint);
        paint.setStyle(Paint.Style.FILL);

        if((x<=lasttouchx+410 && x>=lasttouchx-410&&x!=0)&&(y<=lasttouchy+420 && y>=lasttouchy-420&&y!=0)){





               paint.setColor(Color.MAGENTA);

               canvas.drawCircle(x, y, 70, paint);







        }
       else if(x!=0&&y!=0){
          paint.setColor(Color.RED);
            canvas.drawCircle(lasttouchx,lasttouchy, 70, paint);
        }
        else{}




    }
}

 @Override
 public boolean onTouch(View v, MotionEvent event) {
    x = event.getX();
    y = event.getY();
    int action = event.getActionMasked();
   switch (action){
       case MotionEvent.ACTION_DOWN:
           lasttouchx = event.getX();
           lasttouchy = event.getY();


           break;
       case MotionEvent.ACTION_UP:

           x=lasttouchx;
           y=lasttouchy;
           break;


    }


    v.invalidate();
    return true;
}
}
Edit: Nevermind i solved it here's the new code


public class MainActivity extends Activity implements OnTouchListener  {
     static private float x;
     static private float y;
    static float lasttouchx;
    static float lasttouchy;
    static float boundx;
    static float boundy;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MyCustomPanel view = new MyCustomPanel(this);

    ViewGroup.LayoutParams params =
            new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT);
    addContentView(view, params);
    view.setOnTouchListener(this);

}

private class MyCustomPanel extends View {


    public MyCustomPanel(Context context) {
        super(context);

    }
    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        Paint paint = new Paint();



        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(20);
        paint.setAntiAlias(true);
        if(lasttouchx!=0&&lasttouchy!=0) {
            paint.setColor(Color.BLACK);
            canvas.drawCircle(lasttouchx, lasttouchy, 400, paint);
            paint.setStrokeWidth(5);
            canvas.drawLine(lasttouchx, lasttouchy - 400, lasttouchx, lasttouchy + 400, paint);
            canvas.drawLine(lasttouchx- 400, lasttouchy , lasttouchx+400,lasttouchy,paint);
        }
        else
        {}
        paint.setColor(Color.YELLOW);

        paint.setStrokeWidth(5);
        paint.setTextSize(50);
        canvas.drawText(" X : " + (int) x + "\n Y : " + (int) y, canvas.getWidth() - 500, 200, paint);
        paint.setStyle(Paint.Style.FILL);

        if((x<=lasttouchx+410 && x>=lasttouchx-410&&x!=0)&&(y<=lasttouchy+420 && y>=lasttouchy-420&&y!=0)){





               paint.setColor(Color.MAGENTA);

               canvas.drawCircle(x, y, 70, paint);







        }
       else if(x!=0&&y!=0){
          paint.setColor(Color.RED);
            canvas.drawCircle(boundx,boundy, 70, paint);
        }
        else{}




    }
}

  @Override
  public boolean onTouch(View v, MotionEvent event) {
    x = event.getX();
    y = event.getY();
    int action = event.getActionMasked();
   switch (action){
       case MotionEvent.ACTION_DOWN:
           lasttouchx = event.getX();
           lasttouchy = event.getY();
           break;
       case MotionEvent.ACTION_UP:
           x=lasttouchx;
           y=lasttouchy;
           break;


    }
   if((x<=lasttouchx+409 && x>=lasttouchx-409&&x!=0)&&(y<=lasttouchy+419 && y>=lasttouchy-419&&y!=0)){
       boundx = event.getX();
       boundy = event.getY();

   }


    v.invalidate();
    return true;
}
}
public类MainActivity扩展了活动实现到TouchListener上{
静态私有浮动x;
静态私有浮动;
静态浮点数;
静态浮动接触;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyCustomPanel视图=新建MyCustomPanel(此);
ViewGroup.LayoutParams参数=
新建ViewGroup.LayoutParams(LayoutParams.FILL\u父对象,
LayoutParams.FILL\u PARENT);
addContentView(视图,参数);
view.setOnTouchListener(这个);
}
私有类MyCustomPanel扩展视图{
公共MyCustomPanel(上下文){
超级(上下文);
}
@凌驾
公共空白绘制(画布){
超级绘画(画布);
油漆=新油漆();
绘制.设置样式(绘制.样式.笔划);
油漆。设置行程宽度(20);
paint.setAntiAlias(真);
如果(lasttouchx!=0&&lasttouchy!=0){
油漆。设置颜色(颜色。黑色);
画布.画圈(lasttouchx,lasttouchy,400,油漆);
油漆。设置行程宽度(5);
帆布.抽绳(lasttouchx,lasttouchy-400,lasttouchx,lasttouchy+400,油漆);
帆布.抽绳(lasttouchx-400,lasttouchy,lasttouchx+400,lasttouchy,油漆);
}
其他的
{}
油漆。设置颜色(颜色。黄色);
油漆。设置行程宽度(5);
油漆.尺寸(50);
canvas.drawText(“X:”+(int)X+“\n Y:”+(int)Y,canvas.getWidth()-500200,paint);
绘制.设置样式(绘制.样式.填充);
如果((x=lasttouchx-410&&x!=0)和((y=lasttouchy-420&&y!=0)){
油漆.色系颜色(颜色.洋红色);
画布.画圈(x,y,70,油漆);
}
如果(x!=0&&y!=0),则为else{
油漆。设置颜色(颜色。红色);
画布.画圈(lasttouchx,lasttouchy,70,绘画);
}
else{}
}
}
@凌驾
公共布尔onTouch(视图v,运动事件){
x=event.getX();
y=event.getY();
int action=event.getActionMasked();
开关(动作){
case MotionEvent.ACTION\u DOWN:
lasttouchx=event.getX();
lasttouchy=event.getY();
打破
case MotionEvent.ACTION\u UP:
x=最后一次触摸x;
y=最后一次接触;
打破
}
v、 使无效();
返回true;
}
}
编辑:没关系我解决了这是新代码
公共类MainActivity将活动实现扩展到TouchListener{
静态私有浮动x;
静态私有浮动;
静态浮点数;
静态浮动接触;
静态浮动边界;
静态浮动边界;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyCustomPanel视图=新建MyCustomPanel(此);
ViewGroup.LayoutParams参数=
新建ViewGroup.LayoutParams(LayoutParams.FILL\u父对象,
LayoutParams.FILL\u PARENT);
addContentView(视图,参数);
view.setOnTouchListener(这个);
}
私有类MyCustomPanel扩展视图{
公共MyCustomPanel(上下文){
超级(上下文);
}
@凌驾
公共空白绘制(画布){
超级绘画(画布);
油漆=新油漆();
绘制.设置样式(绘制.样式.笔划);
油漆。设置行程宽度(20);
paint.setAntiAlias(真);
如果(lasttouchx!=0&&lasttouchy!=0){
油漆。设置颜色(颜色。黑色);
画布.画圈(lasttouchx,lasttouchy,400,油漆);
油漆。设置行程宽度(5);
帆布.抽绳(lasttouchx,lasttouchy-400,lasttouchx,lasttouchy+400,油漆);
帆布.抽绳(lasttouchx-400,lasttouchy,lasttouchx+400,lasttouchy,油漆);
}
其他的
{}
油漆。设置颜色(颜色。黄色);
油漆。设置行程宽度(5);
油漆.尺寸(50);
canvas.drawText(“X:”+(int)X+“\n Y:”+(int)Y,canvas.getWidth()-500200,paint);
绘制.设置样式(绘制.样式.填充);
如果((x=lasttouchx-410&&x!=0)和((y=lasttouchy-420&&y!=0)){
油漆.色系颜色(颜色.洋红色);
画布.画圈(x,y,70,油漆);
}
如果(x!=0&&y!=0),则为else{
油漆。设置颜色(颜色。红色);
画布.画圈(boundx,boundy,70,paint);
}
else{}
}
}
@凌驾
公共布尔onTouch(视图v,运动事件){
x=event.getX();
y=event.getY();
int action=event.getActionMasked();
开关(动作){
case MotionEvent.ACTION\u DOWN:
lasttouchx=event.getX();
lasttouchy=event.getY();
打破
case MotionEvent.ACTION\u UP:
x=最后一次触摸x;
y=最后一次接触;
打破
}
如果((x=lasttouchx-409&&x!=0)和((y=lasttouchy-419&&y!=0)){
boundx=event.getX();
boundy=event.getY();
}
v、 使无效();
返回true;
}
}

好的,我解决了,代码如下:

public class MainActivity extends Activity implements OnTouchListener  {
 static private float x;
 static private float y;
static float lasttouchx;
static float lasttouchy;
static float boundx;
static float boundy;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyCustomPanel view = new MyCustomPanel(this);

ViewGroup.LayoutParams params =
        new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT);
addContentView(view, params);
view.setOnTouchListener(this);

}

private class MyCustomPanel extends View {


public MyCustomPanel(Context context) {
    super(context);

}
@Override
public void draw(Canvas canvas) {
    super.draw(canvas);

    Paint paint = new Paint();



    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(20);
    paint.setAntiAlias(true);
    if(lasttouchx!=0&&lasttouchy!=0) {
        paint.setColor(Color.BLACK);
        canvas.drawCircle(lasttouchx, lasttouchy, 400, paint);
        paint.setStrokeWidth(5);
        canvas.drawLine(lasttouchx, lasttouchy - 400, lasttouchx, lasttouchy + 400, paint);
        canvas.drawLine(lasttouchx- 400, lasttouchy , lasttouchx+400,lasttouchy,paint);
    }
    else
    {}
    paint.setColor(Color.YELLOW);

    paint.setStrokeWidth(5);
    paint.setTextSize(50);
    canvas.drawText(" X : " + (int) x + "\n Y : " + (int) y, canvas.getWidth() - 500, 200, paint);
    paint.setStyle(Paint.Style.FILL);

    if((x<=lasttouchx+410 && x>=lasttouchx-410&&x!=0)&&(y<=lasttouchy+420 && y>=lasttouchy-420&&y!=0)){





           paint.setColor(Color.MAGENTA);

           canvas.drawCircle(x, y, 70, paint);







    }
   else if(x!=0&&y!=0){
      paint.setColor(Color.RED);
        canvas.drawCircle(boundx,boundy, 70, paint);
    }
    else{}




}
}

@Override
public boolean onTouch(View v, MotionEvent event) {
x = event.getX();
y = event.getY();
int action = event.getActionMasked();
switch (action){
   case MotionEvent.ACTION_DOWN:
       lasttouchx = event.getX();
       lasttouchy = event.getY();
       break;
   case MotionEvent.ACTION_UP:
       x=lasttouchx;
       y=lasttouchy;
       break;


}
if((x<=lasttouchx+409 && x>=lasttouchx-409&&x!=0)&&(y<=lasttouchy+419 &&   y>=lasttouchy-419&&y!=0)){
   boundx = event.getX();
   boundy = event.getY();

}


v.invalidate();
return true;
}
}
public类MainActivity扩展了活动实现到TouchListener上{
静态私有浮动x;
静态私有浮动;
静态浮点数;
静态浮动接触;
静态浮动边界;
静态浮动边界;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyCustomPanel视图=新建MyCustomPanel(此);
ViewGroup.LayoutParams参数=
新建ViewGroup.LayoutParams(LayoutParams.FILL\u父对象,