Reputation: 2093
I have a custom view in an Android project and in that custom view, I want to draw a bitmap that's about 1/3th the size of the view element.
I use the following code
Bitmap bigIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
Bitmap icon = Bitmap.createScaledBitmap(bigIcon, newWidth, newHeight, false);
width newWidth
and newHeight
being 1/3th of the view's width and height.
The problem is now that if I want to get the view's width or height, I'd have to get it in the onMeasure
or onDraw
method. And I'd really prefer not to scale it every single frame (the bigIcon
is loaded only once in the constructor). The createScaledBitmap
needs also to be called only once but the constructor is too soon since the View is not yet created.
So the question: how can I access the view's width and height (knowing that the view's size doesn't change)?
Upvotes: 0
Views: 63
Reputation: 4849
I think you can accomplish what you want by overriding your custom View's onSizeChanged method.
Here, you can calculate the dimensions of your bitmap based on the View's dimensions only when the size has actually changed.
Upvotes: 1