0

I'm developing a custom view and I need to draw drawables inside. Those drawable must have their position relative.

Here is my code :

    @Override
protected void onDraw(Canvas canvas) {
    //drawBackground(canvas);

    float height = (float) getHeight();
    float width = (float) getWidth();
    canvas.save(Canvas.MATRIX_SAVE_FLAG);
    canvas.scale(width, height);
    drawFlowChart(canvas);
    drawDrawables(canvas);
    canvas.restore();
}

private void drawDrawables(Canvas canvas) {
    canvas.save(Canvas.MATRIX_SAVE_FLAG);
    Resources res = getResources();
    Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.motor);
    canvas.drawBitmap(bitmap, 0.51f, 0.35f, null);
    canvas.restore();
}

But it draw nothing. Any idea ?

I tried to use the drawBitmap(bitmap, src, dest, paint) but I don't know how to position my bitmap with relative position with this definition.

Magnas
  • 869
  • 1
  • 5
  • 18

1 Answers1

0

May be getHeight() and getWidth() function that you call in onDraw return 0 so that canvas scaled and nothing to see.

You can refer two that link to update width and height of canvas to draw:

Android getWidth() return 0 in View's onDraw Android: How to get a custom View's height and width?

Community
  • 1
  • 1
mdtuyen
  • 4,470
  • 5
  • 28
  • 50
  • Unfortunately no, DrawFlowChart() called just before drawDrawables draw my RectF, Path etc just fine – Magnas Jul 08 '16 at 08:08
  • as I see, in your onDraw you you call canvas.scale(width, height), what happen if width = height = 0 ? – mdtuyen Jul 08 '16 at 08:11
  • I agree it won't work if this case happen, but it's not what's happening. Anyway, I'll follow your two links since it's cleaner than what I'm doing and may remove potentials errors,thanks. – Magnas Jul 08 '16 at 08:15