在onPreDraw()方法中全局访问变量

时间:2015-12-02 14:13:36

标签: java android

我想在方法之外使用变量finalHeight和finalWidth但是我不能使用它。它总是给我0作为价值。我怎样才能得到确切的价值。

        int finalHeight, finalWidth;
        iv = (imageView)findViewById(R.id.iv);
        ViewTreeObserver vto = iv.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
            iv.getViewTreeObserver().removeOnPreDrawListener(this);
            finalHeight = iv.getMeasuredHeight();
            finalWidth = iv.getMeasuredWidth();
            Log.d("height", "Height: " + finalHeight + " Width: " + finalWidth);

            return true;
        }
    });
        Log.d("Height1",Integer.toString(finalHeight));

在onPreDraw方法中我得到(728,528)。但是外循环它给出0。 如何在我的其他函数中全局使用finalHeight和finalWidth?

0 个答案:

没有答案