我有这堂课:
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
但是当我在这些点上绘制位图时,我会得到一点偏移。我附上了一个例子:
答案 0 :(得分:2)
我认为如果您将位图中心与绘制的点对齐,那么它将给出正确的结果。
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);