gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:33:21 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com