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

时间:2016-02-18 05:19:13

标签: 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;
}
}

1 个答案:

答案 0 :(得分:1)

好的,我解决了代码:

(include/reader (file "/path/to/source-file")
                (lambda (stx in)
                  (cond [(eof-object? (peek-char in))
                         eof]
                        [else
                         ((lambda (stx)
                            (syntax-case stx ()
                              [(a ...)
                               #'(string-append a ...)]))
                          (syntax->list
                           (at-template-reader stx in)))])))