gpt4 book ai didi

android - 当 startAngle > endAngle 在 android 中时,drawArc 无法正确绘制

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:02:14 29 4
gpt4 key购买 nike

我正在尝试使用触摸监听器在圆中的两点之间绘制弧线。这是我的代码:

public class MainActivity extends Activity implements OnTouchListener {

ImageView whiteCircle, centreCircle, slideArcFrame;
int xStart = 0, yStart = 0, xEnd = 0, yEnd = 0, centerIx = 0, centerIy = 0;
Bitmap output;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
whiteCircle = (ImageView) findViewById(R.id.centerImageWhite);
centreCircle = (ImageView) findViewById(R.id.centerImage2);
slideArcFrame = (ImageView) findViewById(R.id.centerImage);
slideArcFrame.setOnTouchListener(this);
slideArcFrame.setDrawingCacheEnabled(true);
slideArcFrame.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
slideArcFrame.layout(0, 0, slideArcFrame.getMeasuredWidth(),
slideArcFrame.getMeasuredHeight());
slideArcFrame.buildDrawingCache(true);
output = Bitmap.createBitmap(slideArcFrame.getDrawingCache());
centerIx = 100;
centerIy = 100;
}

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
xStart = (int) arg1.getX();
yStart = (int) arg1.getY();
}
if (arg1.getAction() == MotionEvent.ACTION_MOVE) {
xEnd = (int) arg1.getX();
yEnd = (int) arg1.getY();
drawArc();
}
if (arg1.getAction() == MotionEvent.ACTION_UP) {
xEnd = (int) arg1.getX();
yEnd = (int) arg1.getY();
}
return true;
}

private void drawArc() {
Bitmap outp = Bitmap.createBitmap(output.getWidth(),
output.getHeight(), Config.ARGB_8888);
Canvas can = new Canvas(outp);
final Paint paint = new Paint();
RectF rect = new RectF(0, 0, output.getWidth(), output.getHeight());
can.drawARGB(0, 0, 0, 0);
paint.setAntiAlias(true);
paint.setColor(getResources().getColor(R.color.white));
double quadXS, quadYS, quadXE, quadYE;
double quadrantS = 0, quadrantE = 0;
quadXS = xStart - centerIx;
quadYS = yStart - centerIy;
quadXE = xEnd - centerIx;
quadYE = yEnd - centerIy;
if (quadXS >= 0 && quadYS >= 0)
quadrantS = 1;
if (quadXS < 0 && quadYS >= 0)
quadrantS = 2;
if (quadXS < 0 && quadYS < 0)
quadrantS = 3;
if (quadXS >= 0 && quadYS < 0)
quadrantS = 4;
if (quadXE >= 0 && quadYE >= 0)
quadrantE = 1;
if (quadXE < 0 && quadYE >= 0)
quadrantE = 2;
if (quadXE < 0 && quadYE < 0)
quadrantE = 3;
if (quadXE >= 0 && quadYE < 0)
quadrantE = 4;
double startAngle = 0, endAngle = 0;
if (quadrantS == 1) {
startAngle = Math.toDegrees(Math.atan((double) (quadYS) / quadXS));
}
if (quadrantS == 2) {
startAngle = 180 - Math.toDegrees(Math.atan((double) (quadYS)
/ -quadXS));
}
if (quadrantS == 3) {
startAngle = 180 + Math.toDegrees(Math.atan((double) (-quadYS)
/ -quadXS));
}
if (quadrantS == 4) {
startAngle = 360 - Math.toDegrees(Math.atan((double) (-quadYS)
/ quadXS));
}
if (quadrantE == 1) {
endAngle = Math.toDegrees(Math.atan((double) (quadYE) / quadXE));
}
if (quadrantE == 2) {
endAngle = 180 - Math.toDegrees(Math.atan((double) (quadYE)
/ -quadXE));
}
if (quadrantE == 3) {
endAngle = 180 + Math.toDegrees(Math.atan((double) (-quadYE)
/ -quadXE));
}
if (quadrantE == 4) {
endAngle = 360 - Math.toDegrees(Math.atan((double) (-quadYE)
/ quadXE));
}
can.drawArc(rect, (float) startAngle, (float) endAngle
- (float) startAngle, true, paint);
slideArcFrame.setImageBitmap(outp);
}
}

当起始角大于终止角时,从终止角到起始角绘制圆弧。谁能解释一下?

最佳答案

对于 sweepAngle,您正在使用 endAngle - startAngle。如果 startAngle 更高,它将以负数结束。

The docs这么说:

If the sweep angle is negative, the sweep angle is treated as sweep angle modulo 360.

......

The arc is drawn clockwise.

事情是,Java can return negative numbers with modulus , 所以 -30 mod 360 = -30。对于负度数,它将逆时针绘制。如果你想让它总是顺时针绘制,你需要给它一个正数:

float sweepAngle = endAngle - startAngle;
while(sweepAngle < 0)
sweepAngle += 360;

关于android - 当 startAngle > endAngle 在 android 中时,drawArc 无法正确绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19863493/

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