0

I'm creating a custom view. In onDraw method I draw a bitmap. The Bitmaps can have a different height. I need to set the height of view after the picture is loaded. I got onMeasure and onDraw in the logs. onMeasure is called before onDraw. In tutorials is said that the size should be set in onMeasure. What to do if the computation of heights is time consuming and I need set it after this computation? do I have to pre-calculate the size before calling onDraw?

Mansur Nashaev
  • 293
  • 1
  • 5
  • 15
  • do you have the bitmap already decoded at this point? – ootinii Feb 17 '16 at 20:51
  • no, I decode them in onDraw. This is the right approach? – Mansur Nashaev Feb 17 '16 at 21:00
  • I would probably decode the bitmap not inside onDraw, honestly. You should probably decode it whenever it's set, whether you're setting a path or a resource, etc. Doing it in onDraw will likely lead to performance issues. – ootinii Feb 17 '16 at 21:17

1 Answers1

0

You can try

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);

Note: there are other methods available that decode resources and the like, I just used the decodeFile as the example. This will give you the dimensions of the Bitmap without fully decoding it, which would allow you to set your measured dimensions to what you need them to be. The dimensions will be in the options.

final int width = options.outWidth;
final int height = options.outHeight;
ootinii
  • 1,795
  • 20
  • 15