Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For drawing purposes, you should not really use the dimensions of the <code>Canvas</code> object.</p> <p>Just use the dimensions provided to you in the <code>onSizeChanged</code> method. You can either store the dimensions for use in the <code>onDraw</code> method or resize/draw to a backing bitmap that you can draw with later.</p> <p><strong>Update:</strong></p> <p>Quickly whipped up some code, it looks like this works:</p> <pre><code>public class CustomView extends View{ private Paint paint; private int w; private int h; public CustomView(Context context, AttributeSet attr) { super(context, attr); paint = new Paint(); paint.setTextAlign(Align.CENTER); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { this.w = w; this.h = h; super.onSizeChanged(w, h, oldw, oldh); } @Override protected void onDraw(Canvas canvas) { canvas.drawColor(Color.WHITE); canvas.drawText("TEST", w/2, h/2, paint); } } </code></pre> <p><strong>Update 2</strong></p> <p>Following the circle code update.</p> <p>We can do this:</p> <pre><code> @Override protected void onDraw(Canvas canvas) { canvas.drawColor(Color.WHITE); float centerX = (float) w/2; float centerY = (float) h/2; float radianAngle = (float) Math.toRadians(startAngle); radius[0] = centerX; radius[1] = centerY; radius[2] = centerX + centerX * FloatMath.cos(radianAngle); radius[3] = centerY + centerY * FloatMath.sin(radianAngle); paint.setColor(0xFF330000); paint.setStrokeWidth(1); canvas.drawLines(radius, paint); } </code></pre> <p>You'll see that this now works on any sized view.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload