Reputation: 2535
I have this 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);
}
This is hurried up code, which I intend to clean up later for memory optimization and logical standards.
My aim from the above class is to find the center of the screen, calculate the points at 45 degree angles on the circumference with a certain radius.
This is working properly for Points
however when I draw a bitmap on these points I get a little offset. I have attached an example:
Upvotes: 2
Views: 303
Reputation: 1255
i think if you aligned the bitmap center to the points drawn , then it will give the correct results.
canvas.drawBitmap(icon, px.x, px.y, mPaint);
change to
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);
Upvotes: 2