java - 算法改进 : Keeping circle at boundary of a defined shape

标签 java android xml android-studio

我编写的代码在我触摸的任何地方绘制一个圆圈,它会保持在更大的圆圈或任何形状内,如果你一直按下它会保持在那里但如果你触摸或离开圆圈,它会回到中心.但是每当我只是按住我的触地时,我希望小圆圈在离开大圆圈时保持在边界处。 这是我的代码:

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 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;
}
}

关于java - 算法改进 : Keeping circle at boundary of a defined shape,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35473413/

相关文章:

xml - 多个 ID - 多个 IDREF - 是否可以将特定 IDREF 分配给 DTD 中的特定 ID?

android - Xml布局在改变方向时发生变化

java - 如何搜索存储在 hashmap 中的匹配对?

java - 如何与 friend 分享推荐代码和 Play 商店应用程序

Java - 异常处理 - 正则表达式 - 匹配方法不适用于扫描仪输入

android - CardView 项目不可点击?

android - dialogFragment 中的视频全屏模式

xml - Confluence 存储格式 - 任何人都玩过 anchor 和 anchor 链接宏

java - 来自 Timestamp JPA 模型字段的 Primefaces 日历组件

java - JSONparser 读取句子中的第一个单词