gpt4 book ai didi

java - 试图在圆周上绘制位图,在实现中找到一些偏移量

转载 作者:行者123 更新时间:2023-11-30 08:42:45 26 4
gpt4 key购买 nike

我有这门课:

public class MockDraw extends View {

private Paint mPaint;
public static float angle = 0f;
private int width;
private int height;
Context ctx;
Bitmap icon;
PointF pf;

public MockDraw(Context ctx) {
super(ctx);
this.ctx = ctx;
setFocusable(true);
mPaint = new Paint();
this.mPaint.setAntiAlias(true);
this.mPaint.setStrokeWidth(4f);
this.mPaint.setColor(Color.WHITE);
this.mPaint.setStyle(Paint.Style.STROKE);
this.mPaint.setStrokeJoin(Paint.Join.ROUND);

icon = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.ic_action_send_now);
}


@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
this.width = w;
this.height = h;

float centerX = (float) width / 2;
float centerY = (float) height / 2;

pf = new PointF(centerX, centerY);
super.onSizeChanged(w, h, oldw, oldh);
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);


for (int i = 0; i <= 360; i += 45) {
PointF px = getPosition(pf, 400, i);

// canvas.drawPoint(centerX, centerY, mPaint);
canvas.drawPoint(px.x, px.y, mPaint);
canvas.drawBitmap(icon, px.x, px.y, mPaint);

}

}

private PointF getPosition(PointF center, float radius, float angle) {

return new PointF((float) (center.x + radius * Math.cos(Math.toRadians(angle))),
(float) (center.y + radius * Math.sin(Math.toRadians(angle))));
}

// canvas.drawText("TEST", centerX, centerY, mPaint);


}

这是匆忙的代码,我打算稍后清理这些代码以进行内存优化和逻辑标准。

我类的目的是找到屏幕的中心,计算出一定半径的圆周上45度角的点。

这对 Points 工作正常,但是当我在这些点上绘制位图时,我得到了一点偏移。我附上了一个例子:

enter image description here

最佳答案

我认为如果您将位图中心与绘制的点对齐,那么它会给出正确的结果。

canvas.drawBitmap(icon, px.x, px.y, mPaint);

改为

if(px.x-icon.getWidth()/2>0&&px.y-icon.getHeight()/2>0)
canvas.drawBitmap(icon, px.x-icon.getWidth()/2, px.y-icon.getHeight()/2, mPaint);

关于java - 试图在圆周上绘制位图,在实现中找到一些偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34470365/

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