30

Can I take a view that has been rendered by the Android framework and rescale it to some other size?

bjdodson
  • 519
  • 2
  • 8
  • 11
  • 1
    http://stackoverflow.com/questions/2963152/android-how-to-resize-a-custom-view-programmatically – Nikunj Patel Oct 01 '11 at 11:07
  • 3
    I 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 Answers3

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
10

In your XML

android:scaleX="0.5"
android:scaleY="0.5"
Taiti
  • 375
  • 2
  • 6
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);

Result
Example

Demo

Linh
  • 57,942
  • 23
  • 262
  • 279