Android getMeasuredHeight 返回错误值! [英] Android getMeasuredHeight returns wrong values !

查看:48
本文介绍了Android getMeasuredHeight 返回错误值!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定某些 UI 元素的实际尺寸(以像素为单位)!

I'm trying to determine the real dimension in pixels of some UI elements !

这些元素是从一个 .xml 文件中膨胀出来的,并用倾斜宽度和高度初始化,这样 GUI 最终将支持多种屏幕尺寸和 dpi (android 规范推荐).

Those elements are inflated from a .xml file and are initialized with dip width and height so that the GUI will eventually support multiple screen size and dpi (as recommended by android specs).

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="150dip"
android:orientation="vertical">
    
<ImageView
    android:id="@+id/TlFrame" 
    android:layout_width="110dip" 
    android:layout_height="90dip"
    android:src="@drawable/timeline_nodrawing"
    android:layout_margin="0dip"
    android:padding="0dip"/></LinearLayout>

前面的 xml 代表一个框架.但我确实在水平布局中动态添加了许多内容:

This previous xml represent one frame. But I do add many dynamically inside a horizontal layout describe here :

<HorizontalScrollView 
        android:id="@+id/TlScroller"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_margin="0dip"
        android:padding="0dip"
        android:scrollbars="none"
        android:fillViewport="false"
        android:scrollbarFadeDuration="0"
        android:scrollbarDefaultDelayBeforeFade="0"
        android:fadingEdgeLength="0dip"
        android:scaleType="centerInside">
        
        <!-- HorizontalScrollView can only host one direct child -->
        <LinearLayout 
            android:id="@+id/TimelineContent" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_margin="0dip"
            android:padding="0dip"
            android:scaleType="centerInside"/>
   
    </HorizontalScrollView > 

定义在我的java代码中添加一帧的方法:

The method defined to add one frame inside my java code :

private void addNewFrame()
{       
    LayoutInflater inflater     = (LayoutInflater) _parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewGroup root      = (ViewGroup) inflater.inflate(R.layout.tl_frame, null);
    TextView frameNumber = (TextView) root.findViewById(R.id.FrameNumber);
    Integer val = new Integer(_nFramesDisplayed+1); //+1 to display ids starting from one on the user side 
    frameNumber.setText(val.toString());
    
    ++_nFramesDisplayed;
    _content.addView(root);
// _content variable is initialized like this in c_tor
// _content = (LinearLayout) _parent.findViewById(R.id.TimelineContent);
}

然后在我的代码中,我尝试以像素为单位获得实际的实际大小,因为我需要它来在其上绘制一些 opengl 内容.

Then inside my code, I try to get the actual real size in pixel because I need this to draw some opengl stuff over it.

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 的实际像素大小大得多.

Everything seems to work fine except that those values are way bigger than the actual pixel size of the ImageView.

来自 getWindowManager().getDefaultDisplay().getMetrics(metrics) 的报告信息;如下:密度 = 1,5密度Dpi = 240宽度像素 = 600高度像素 = 1024

Reported infos from getWindowManager().getDefaultDisplay().getMetrics(metrics); are the following : density = 1,5 densityDpi = 240 widthPixel = 600 heightPixel = 1024

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

Now, I know the rule from android is : pixel = dip * (dpi /160). But nothing makes any sense with the value returned. For that ImageView of (90dip X 110dip), the returned values of the measure() method is (270 x 218) which I assumed is in pixel !

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

Anyone has any idea why ? Is the value returned in pixel ?

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

By the way : I've been testing the same code but with a TextView instead than an ImageView and everything seems to be working fine ! Why !?!?

推荐答案

您错误地调用了 measure.

measure 需要 MeasureSpecMeasureSpec.makeMeasureSpec 特别打包的值.measure 忽略 LayoutParams.执行测量的父级应根据自己的测量和布局策略以及子级的 LayoutParams 创建 MeasureSpec.

measure takes MeasureSpec values which are specially packed by MeasureSpec.makeMeasureSpec. measure ignores LayoutParams. The parent doing the measuring is expected to create a MeasureSpec based on its own measurement and layout strategy and the child's LayoutParams.

如果您想测量 WRAP_CONTENT 通常在大多数布局中的工作方式,请像这样调用 measure:

If you want to measure the way that WRAP_CONTENT usually works in most layouts, call measure like this:

frame.measure(MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST),
        MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST));

如果您没有最大值(例如,如果您正在编写具有无限空间的 ScrollView 之类的内容),您可以使用 UNSPECIFIED 模式:

If you don't have max values (for example if you're writing something like a ScrollView that has infinite space) you can use the UNSPECIFIED mode:

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

这篇关于Android getMeasuredHeight 返回错误值!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆