试图在圆周上绘制位图,在实现中找到一些偏移量
Trying to draw bitmaps on circumference of a circle, find some offset in the implementation
我有这个class:
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);
}
这是匆忙的代码,我打算稍后清理这些代码以进行内存优化和逻辑标准。
我上面的目的class是找到屏幕的中心点,计算出一定半径的圆周上45度角的点。
这对 Points
工作正常,但是当我在这些点上绘制位图时,我得到了一点偏移。我附上了一个例子:
我认为如果您将位图中心与绘制的点对齐,那么它会给出正确的结果。
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);
我有这个class:
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);
}
这是匆忙的代码,我打算稍后清理这些代码以进行内存优化和逻辑标准。
我上面的目的class是找到屏幕的中心点,计算出一定半径的圆周上45度角的点。
这对 Points
工作正常,但是当我在这些点上绘制位图时,我得到了一点偏移。我附上了一个例子:
我认为如果您将位图中心与绘制的点对齐,那么它会给出正确的结果。
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);