简体   繁体   English

getWidth在缩放后不返回不同的值

[英]getWidth not returning different value after scaling

I'm trying to find what I'm missing when using setScaleX. 我正在尝试使用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 would of thought the width would have become 30. How do I get the value of the width after scaling? 我认为宽度会变成30.如何在缩放后获得宽度值?

you can solve it by multiply by scale x or y 你可以通过乘以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()));
        }

    });

getWidth() returns the width before transforms are applied to the view ( setScaleX , etc...). getWidth()返回变换应用于视图之前的宽度( setScaleX等)。 So this is expected. 所以这是预期的。 This is hinted by the documentation of getDrawingRect : getDrawingRect的文档暗示了这一点

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM