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

问题描述

限时删除!!

我试图确定一些UI元素像素的实数维!

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

这些元素从一个.xml文件膨胀和浸宽度和高度被初始化,这样的图形用户界面最终将支持多个屏幕大小和DPI(的)。

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>

这previous XML重新present一帧。但我动态添加许多水平布局内的在这里描述的:

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 code一帧的方法:

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);
}

然后在我的code,我尝试在像素的实际真正的大小,因为我需要这个画一些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(度量)。有以下几种: 密度= 1,5 densityDpi = 240 widthPixel = 600 heightPixel = 1024

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

现在,我知道,从Android的规则是:像素=浸*(DPI / 160)。但没有什么使得与返回的值任何意义。对于这ImageView的(90dip X 110dip),该措施的返回值()方法是(270×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 ?

顺便说一句:我一直在测试相同的code,但与一个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 !?!?

推荐答案

您正在呼叫测量不正确。

测量需要<$c$c>MeasureSpec这是由 MeasureSpec.makeMeasureSpec 专门准备的值。 测量忽略的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 通常工作在大多数布局,调用测量是这样的:

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));

如果您还没有最大值(例如,如果你写的东西就像一个滚动型,有无限的空间),你可以使用不定的模式:

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返回错误的值!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:47