4

我试图找到使用 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()));
        }

    });

我以为宽度会变成 30。缩放后如何获得宽度的值?

4

2 回答 2

6

你可以通过乘以解决它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()));
        }

    });
于 2013-09-07T16:48:55.537 回答
0

getWidth()返回将变换应用于视图(等)之前的宽度。setScaleX所以这是意料之中的。getDrawingRect的文档暗示了这一点:

 These bounds do not account for any transformation properties currently set on the view, 
 such as setScaleX(float) or setRotation(float).
于 2016-03-08T21:53:59.197 回答