Android getMeasuredHeight返回错误的值!

Android getMeasuredHeight返回错误的值!,android,android-layout,android-widget,android-ui,android-xml,Android,Android Layout,Android Widget,Android Ui,Android Xml,我试图确定一些UI元素的真实尺寸(以像素为单位) 这些元素是从.xml文件中膨胀出来的,并用dip width和height初始化,这样GUI最终将支持多屏幕大小和dpi() 然后在我的代码中,我试图得到实际的像素大小,因为我需要它来画一些opengl的东西 LayoutInflater inflater = (LayoutInflater) _parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

我试图确定一些UI元素的真实尺寸(以像素为单位)

这些元素是从.xml文件中膨胀出来的,并用dip width和height初始化,这样GUI最终将支持多屏幕大小和dpi()

然后在我的代码中,我试图得到实际的像素大小,因为我需要它来画一些opengl的东西

LayoutInflater inflater     = (LayoutInflater) _parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewGroup root      = (ViewGroup) inflater.inflate(R.layout.tl_frame, null);
    ImageView frame = (ImageView) root.findViewById(R.id.TlFrame);
    
    frame.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    frame.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    final int w = frame.getMeasuredWidth();
    final int h = frame.getMeasuredHeight();
除了这些值远远大于ImageView的实际像素大小之外,一切似乎都正常工作

从getWindowManager().getDefaultDisplay().getMetrics(metrics)报告的信息; 以下是: 密度=1,5 密度dpi=240 宽度像素=600 高度像素=1024

现在,我知道android的规则是:像素=dip*(dpi/160)。但是返回的值没有任何意义。对于(90dip X 110dip)的ImageView,measure()方法的返回值是(270 X 218),我假设是像素

有人知道为什么吗? 返回的值是否为像素


顺便说一句:我一直在测试相同的代码,但使用的是TextView而不是ImageView,一切似乎都很正常!为什么

可能是由于AndroidManifest.xml()文件中的内容以及xml文件来自哪个drawable XXX目录,Android通过缩放操作加载资源。您决定使用“dip”()维度单位,它是虚拟的,而实际值(px)可能不同。

您调用的
度量值不正确

measure
获取由
MeasureSpec.makeMeasureSpec
专门打包的值<代码>测量
忽略布局参数。进行测量的父级应根据其自己的测量和布局策略以及子级的LayoutParams创建一个MeasureSpec

如果要测量
WRAP\u内容
通常在大多数版面中的工作方式,请按如下方式调用
measure

frame.measure(MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST),
        MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST));
如果没有最大值(例如,如果您正在编写具有无限空间的滚动视图),则可以使用
UNSPECIFIED
模式:

frame.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

好的!这里有点回答我自己的问题…但不完全

1-似乎在某些设备上,ImageView测量没有提供精确值。例如,我在Nexus和Galaxy设备上看到了很多关于这种情况的报道

2-我想出的一个解决办法:

将ImageView的宽度和高度设置为xml代码中的“包装内容”

膨胀代码内部的布局(我想通常是在UI初始化中)

根据典型图像,计算您自己的图像视图比率

使用计算出的大小在代码内部动态设置ImageView

frame.getLayoutParams().width = pixeWidth;
瞧!您的ImageView现在具有所需的倾角大小;)

这样做:

frame.measure(0, 0);

final int w = frame.getMeasuredWidth();
final int h = frame.getMeasuredHeight();

解决了

您必须创建自定义Textview并在布局中使用它,并使用getActual height函数在运行时设置高度

public class TextViewHeightPlus extends TextView {
    private static final String TAG = "TextViewHeightPlus";
    private int actualHeight=0;


    public int getActualHeight() {
        return actualHeight;
    }

    public TextViewHeightPlus(Context context) {
        super(context);
    }

    public TextViewHeightPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    public TextViewHeightPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

   @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        actualHeight=0;

        actualHeight=(int) ((getLineCount()-1)*getTextSize());

    }

}
应该使用

不幸的是,在生命周期方法中,例如尚未执行布局过程,因此您还无法检索视图层次结构中视图的大小。但是,您可以明确地要求Android使用

正如@adamp所指出的,您必须提供一些值,但找出正确的值可能有点让人望而生畏

以下方法尝试确定正确的值并测量传入的
视图

public class ViewUtil {

    public static void measure(@NonNull final View view) {
        final ViewGroup.LayoutParams layoutParams = view.getLayoutParams();

        final int horizontalMode;
        final int horizontalSize;
        switch (layoutParams.width) {
            case ViewGroup.LayoutParams.MATCH_PARENT:
                horizontalMode = View.MeasureSpec.EXACTLY;
                if (view.getParent() instanceof LinearLayout
                        && ((LinearLayout) view.getParent()).getOrientation() == LinearLayout.VERTICAL) {
                    ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
                    horizontalSize = ((View) view.getParent()).getMeasuredWidth() - lp.leftMargin - lp.rightMargin;
                } else {
                    horizontalSize = ((View) view.getParent()).getMeasuredWidth();
                }
                break;
            case ViewGroup.LayoutParams.WRAP_CONTENT:
                horizontalMode = View.MeasureSpec.UNSPECIFIED;
                horizontalSize = 0;
                break;
            default:
                horizontalMode = View.MeasureSpec.EXACTLY;
                horizontalSize = layoutParams.width;
                break;
        }
        final int horizontalMeasureSpec = View.MeasureSpec
                .makeMeasureSpec(horizontalSize, horizontalMode);

        final int verticalMode;
        final int verticalSize;
        switch (layoutParams.height) {
            case ViewGroup.LayoutParams.MATCH_PARENT:
                verticalMode = View.MeasureSpec.EXACTLY;
                if (view.getParent() instanceof LinearLayout
                        && ((LinearLayout) view.getParent()).getOrientation() == LinearLayout.HORIZONTAL) {
                    ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
                    verticalSize = ((View) view.getParent()).getMeasuredHeight() - lp.topMargin - lp.bottomMargin;
                } else {
                    verticalSize = ((View) view.getParent()).getMeasuredHeight();
                }
                break;
            case ViewGroup.LayoutParams.WRAP_CONTENT:
                verticalMode = View.MeasureSpec.UNSPECIFIED;
                verticalSize = 0;
                break;
            default:
                verticalMode = View.MeasureSpec.EXACTLY;
                verticalSize = layoutParams.height;
                break;
        }
        final int verticalMeasureSpec = View.MeasureSpec.makeMeasureSpec(verticalSize, verticalMode);

        view.measure(horizontalMeasureSpec, verticalMeasureSpec);
    }

}
然后,您可以简单地调用:

ViewUtil.measure(view);
int height = view.getMeasuredHeight();
int width = view.getMeasuredWidth();

或者,作为@Amit Yadav,您可以使用在执行布局传递后调用侦听器。以下是处理跨版本注销侦听器和方法命名更改的方法:

public class ViewUtil {

    public static void captureGlobalLayout(@NonNull final View view,
                                           @NonNull final ViewTreeObserver.OnGlobalLayoutListener listener) {
        view.getViewTreeObserver()
                .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        final ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            viewTreeObserver.removeOnGlobalLayoutListener(this);
                        } else {
                            //noinspection deprecation
                            viewTreeObserver.removeGlobalOnLayoutListener(this);
                        }
                        listener.onGlobalLayout();
                    }
                });
    }

}
然后你可以:

ViewUtil.captureGlobalLayout(rootView, new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int width = view.getMeasureWidth();
        int height = view.getMeasuredHeight();
    }
});

其中,
rootView
可以是视图层次结构的根视图,
view
可以是层次结构中您希望了解其维度的任何视图。

Manifest仅指定活动、意图和sdk版本。与屏幕大小或支持无关。另外,我所有的可绘制文件都在基本的res/drawable文件夹中…没有mdpi或hdpi前缀/后缀/子文件夹…在emu上运行应用程序的位置,在哪个设备上?ImageView的缩放类型如何?ScaleType设置为“centerInside”…我尝试过删除它,结果是一样的…“与屏幕大小或支持无关。”这不是真的,似乎也不起作用……我尝试了max values和MesureSpec的每一种组合,结果要么与以前相同,要么与我用于测试的“伪”maxValues相同,要么为零。我不知道这是否与此相关……但上面的线性布局是动态添加的(在java代码中)…在水平滚动视图中逐帧,在线性布局中…目标是创建一个动态时间轴GUI,并使用openGl在其中绘制!有没有办法动态设置ImageView的宽度和高度???我正在考虑根据屏幕大小和dpi自己进行计算……澄清一下,如果你想测量包裹的文本,你不应该对两个维度都使用
MeasureSpec.UNSPECIFIED
。这将本质上给你无限的水平滚动,并导致文本去包装。您需要将垂直尺寸设置为
MeasureSpec.UNSPECIFIED
,将水平尺寸设置为最大宽度,以便触发文本换行并获得准确的高度测量。请记住在必要时解释填充。@BobAman非常感谢您的评论。这完全解决了我在getMeasuredHeight/Width上遇到的问题。@Simon,你的设备是什么?对我来说,通过指定
MeasureSpec.UNSPECIFIED
(双关语非本意),这个神秘的度量解决了我最初和后来调用时返回不同值的问题,谢谢@Pavlus它与使用
MeasureSpec.UNSPECIFIED
调用相同,因为它是一个常量,其值为0<
public class TextViewHeightPlus extends TextView {
    private static final String TAG = "TextViewHeightPlus";
    private int actualHeight=0;


    public int getActualHeight() {
        return actualHeight;
    }

    public TextViewHeightPlus(Context context) {
        super(context);
    }

    public TextViewHeightPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    public TextViewHeightPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

   @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        actualHeight=0;

        actualHeight=(int) ((getLineCount()-1)*getTextSize());

    }

}
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
 @SuppressLint("NewApi")
 @SuppressWarnings("deprecation")
 @Override
  public void onGlobalLayout() {
   //now we can retrieve the width and height
   int width = view.getWidth();
   int height = view.getHeight();


   //this is an important step not to keep receiving callbacks:
   //we should remove this listener
   //I use the function to remove it based on the api level!

    if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN){
        view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }else{
        view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    }
  }
 });
public class ViewUtil {

    public static void measure(@NonNull final View view) {
        final ViewGroup.LayoutParams layoutParams = view.getLayoutParams();

        final int horizontalMode;
        final int horizontalSize;
        switch (layoutParams.width) {
            case ViewGroup.LayoutParams.MATCH_PARENT:
                horizontalMode = View.MeasureSpec.EXACTLY;
                if (view.getParent() instanceof LinearLayout
                        && ((LinearLayout) view.getParent()).getOrientation() == LinearLayout.VERTICAL) {
                    ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
                    horizontalSize = ((View) view.getParent()).getMeasuredWidth() - lp.leftMargin - lp.rightMargin;
                } else {
                    horizontalSize = ((View) view.getParent()).getMeasuredWidth();
                }
                break;
            case ViewGroup.LayoutParams.WRAP_CONTENT:
                horizontalMode = View.MeasureSpec.UNSPECIFIED;
                horizontalSize = 0;
                break;
            default:
                horizontalMode = View.MeasureSpec.EXACTLY;
                horizontalSize = layoutParams.width;
                break;
        }
        final int horizontalMeasureSpec = View.MeasureSpec
                .makeMeasureSpec(horizontalSize, horizontalMode);

        final int verticalMode;
        final int verticalSize;
        switch (layoutParams.height) {
            case ViewGroup.LayoutParams.MATCH_PARENT:
                verticalMode = View.MeasureSpec.EXACTLY;
                if (view.getParent() instanceof LinearLayout
                        && ((LinearLayout) view.getParent()).getOrientation() == LinearLayout.HORIZONTAL) {
                    ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
                    verticalSize = ((View) view.getParent()).getMeasuredHeight() - lp.topMargin - lp.bottomMargin;
                } else {
                    verticalSize = ((View) view.getParent()).getMeasuredHeight();
                }
                break;
            case ViewGroup.LayoutParams.WRAP_CONTENT:
                verticalMode = View.MeasureSpec.UNSPECIFIED;
                verticalSize = 0;
                break;
            default:
                verticalMode = View.MeasureSpec.EXACTLY;
                verticalSize = layoutParams.height;
                break;
        }
        final int verticalMeasureSpec = View.MeasureSpec.makeMeasureSpec(verticalSize, verticalMode);

        view.measure(horizontalMeasureSpec, verticalMeasureSpec);
    }

}
ViewUtil.measure(view);
int height = view.getMeasuredHeight();
int width = view.getMeasuredWidth();
public class ViewUtil {

    public static void captureGlobalLayout(@NonNull final View view,
                                           @NonNull final ViewTreeObserver.OnGlobalLayoutListener listener) {
        view.getViewTreeObserver()
                .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        final ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            viewTreeObserver.removeOnGlobalLayoutListener(this);
                        } else {
                            //noinspection deprecation
                            viewTreeObserver.removeGlobalOnLayoutListener(this);
                        }
                        listener.onGlobalLayout();
                    }
                });
    }

}
ViewUtil.captureGlobalLayout(rootView, new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int width = view.getMeasureWidth();
        int height = view.getMeasuredHeight();
    }
});