GetWidth does not return another value after scaling

I am trying to find what I am missing when using setScaleX.

final ImageView iv = (ImageView) findViewById(R.id.imageView); ViewTreeObserver vto = iv.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // Start at 60 Log.d("test", "Before"+String.valueOf(iv.getWidth())); iv.setScaleX((float) .5); // Still at 60 after scale Log.d("test", "After"+String.valueOf(iv.getWidth())); } }); 

I thought the width would be 30. How do I get the width value after scaling?

+4
source share
2 answers

you can solve it by multiplying by scale x or y

  final ImageView iv = (ImageView) findViewById(R.id.imageView); ViewTreeObserver vto = iv.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // Start at 60 Log.d("test", "Before"+String.valueOf(iv.getWidth()* iv.getScaleX())); iv.setScaleX((float) .5); // Still at 60 after scale Log.d("test", "After"+String.valueOf(iv.getWidth()* iv.getScaleX())); } }); 
+5
source

getWidth() returns the width before conversion to the view ( setScaleX , etc.). So it is expected. This is confirmed by the getDrawingRect documentation:

  These bounds do not account for any transformation properties currently set on the view, such as setScaleX(float) or setRotation(float). 
0
source

Source: https://habr.com/ru/post/1501085/


All Articles