How to automatically scale a custom View with ShapeDrawables?
-
21-12-2019 - |
Question
I am writing a custom view for my Android application, that consists of several ShapeDrawables
. For these drawables, I have to specify the dimensions using setBounds()
. Now, my view will always be square, but I want it to use as much space as I allow it within the parent where it is embedded.
So, right now I call setBounds()
with some hardcoded values, but when I specify android:layout_width
and android:layout_height
, I want my ShapeDrawables
to be scaled so that they appear as large as possible.
How can I accomplish this within my custom View class?
Solution
Found it myself:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
mSize = Math.min(width, height);
setMeasuredDimension(mSize, mSize);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow