Can I take a view that has been rendered by the Android framework and rescale it to some other size?
Asked
Active
Viewed 3.9k times
30
-
1http://stackoverflow.com/questions/2963152/android-how-to-resize-a-custom-view-programmatically – Nikunj Patel Oct 01 '11 at 11:07
-
3I am not just trying to set the size of a view-- I would like to take a view and scale it down to a smaller size, basically taking a thumbnail of the view after it has been laid out. – bjdodson Oct 02 '11 at 02:52
3 Answers
60
You need API 11 or above to scale a view. Here is how:
float scalingFactor = 0.5f; // scale down to half the size
view.setScaleX(scalingFactor);
view.setScaleY(scalingFactor);

Gadzair
- 1,221
- 14
- 21
-
3Praise be to God. This tiny piece of code worked amazingly well for me !! :) Thank you ! – real 19 Feb 11 '15 at 05:39
-
43In this case it's scaled but still taking the original area in the layout !! any solutions? – Hamzeh Soboh Mar 12 '15 at 08:48
-
-
@Pranav use layout params to scale, it'll adapt the view itself and give a rel dimensions. – Hamzeh Soboh Aug 02 '19 at 13:58
4
For scale, I resize the width
and height
of the view to make it affect the area and position another view
.
If you don't want it affect the area and position, use answer of @Gadzair
and @Taiti
private void resize(View view, float scaleX, float scaleY) {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = (int) (view.getWidth() * scaleX);
layoutParams.height = (int) (view.getHeight() * scaleY);
view.setLayoutParams(layoutParams);
}
Example using
resize(view, 0.5f, 0.8f);

Linh
- 57,942
- 23
- 262
- 279
-
-
3be careful to use this solution. because width & height is int, scale is float -> if you care about ratio of width and height, this way can lead to wrong ratio. – Mạnh Hoàng Huynh Dec 09 '19 at 07:18