我有这门课:
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度角的点。
这对于点
来说工作正常,但是当我在这些点上绘制位图时,我得到了一点偏移。我附上了一个例子:
最佳答案
我认为如果您将位图中心与绘制的点对齐,那么它将给出正确的结果。
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/