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

时间:2013-09-07 16:44:57

标签: java android android-layout

我正在尝试使用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.如何在缩放后获得宽度值?

2 个答案:

答案 0 :(得分:5)

你可以乘以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()));
        }

    });

答案 1 :(得分: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).