本文介绍了填写与固定纵横比surfaceview剩余空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想布局类似如下的观点,但我不知道如何去这样做的:

I'd like to layout a view like the following, but I'm not sure how to go about doing so:

  • 在屏幕的底部是一个普通的图,具有固定大小(填充宽度和DP为高度的一些数)
  • 填充剩余的垂直空间,我想向自定义视图继承自glSurfaceView,与它必须有一个固定的纵横比,这将是编程设定,但活动创建之前已知的限制。 (在这种情况下假设它与为2:3瓦特:小时)。

下面是一个什么样我想象一个小图:

Here's a little picture of what I'm imagining:

 .----------------------.
 |   .--------------.   |
 | b |              | b |
 | g |              | g |
 |   |              |   |
 | i |  MySurface   | i |
 | m |     View     | m |
 | a |              | a |
 | g |  (2:3 w:h)   | g |
 | e |              | e |
 |   *--------------*   |
 |.--------------------.|
 ||                    ||
 ||   SomeOtherView    ||
 ||                    ||
 |*--------------------*|
 *----------------------*

至于设立MySurfaceView,我认为正常layout_width和layout_height不会帮助我,因为我不知道可用的屏幕大小,直到之后的屏幕已经部分摆出来, SomeOtherView 已被放置。

有没有在 MySurfaceView 部分功能,我要重写,使得例如将作为参数传递可用的屏幕空间,让我决定如何设置宽度和该视图的高度?

Is there some function in MySurfaceView that I want to override, that for example will pass as arguments the available screen space, and let me decide how to set the width and height of the view?

是'通货膨胀'正确的术语我正在寻找在这里?我认为这事做与我在寻找什么,但我真的不知道的术语。

Is 'inflation' the right term I'm looking for here? I think that has something to do with what I'm looking for, but I don't really know the terminology.

更新下面与我的研究的结果,我认为可能的正确的解决方案。

Updated below with the result of my research, and I think a possible correct solution.

推荐答案

做了更多的研究,我想我想通了什么,我需要做的:

Did more research and I think I figured out what I need to do:

我一直在寻找替代的功能是 onMeasure(INT widthSpec,诠释heightSpec),这就是所谓的父与建议宽度和高度(见View.MeasureSpec).我还设置 MySurfaceView WRAP_CONTENT 的layout_width /身高的XML。然后,当 onMeasure 被调用我可以计算出可用的纵横比,可以根据需要设定的我的观点的宽度和高度。

The function I was looking to override is onMeasure(int widthSpec, int heightSpec), which is called by the parent with suggestions for width and height (see View.MeasureSpec). I also set MySurfaceView to have a layout_width/height of wrap_content in the xml. Then when onMeasure gets called I can compute the available aspect ratio and set the width and height of my view as desired.

第二件事情我没有做这项工作,就是把我的 MySurfaceView A的FrameLayout里面的唯一的孩子。我本来 MySurfaceView SomeOtherView A RelativeLayout的从我的形象。与RelativeLayout的问题,是它不会谈判的宽度/高度的方式,让我固定纵横比。

The second thing I did to make this work was to put my MySurfaceView inside of a FrameLayout as the only child. I originally had MySurfaceView in a RelativeLayout with the SomeOtherView from my image. The problem with the RelativeLayout, is that it would not negotiate the width/height in a way that allowed me to fix the aspect ratio.

onMeasure 被测量过程中调用多次,并且RelativeLayout的工作方式是,它首先要求它的宽度没有告诉它可用高度,然后再自定义视图回来并要求高度,而宽度被锁定到你在第一遍指定的( MeasureSpec.EXACTLY )。这使得不可能产生一定比率的视图,因为你必须知道可用高度之前确认的宽度。

onMeasure gets called multiple times during measuring, and the way RelativeLayout works is that it first asks the custom view for it's width without telling it the available height, and then later comes back and asks for the height, while the width is locked to what you specified in the first pass (MeasureSpec.EXACTLY). This makes it impossible to generate a view of a certain ratio, as you must confirm the width before knowing the available height.

一旦的FrameLayout里面,我没有这个问题,因为MeasureSpecs传递给 onMeasure 只过了限制 AT_MOST 。这意味着我是自由onMeasure的每一个传球时更改宽度和高度,所以我可能最终计算我的纵横比所需提供的可用面积。

Once inside of the FrameLayout, I didn't have this problem, as the MeasureSpecs passed to onMeasure only ever had the restriction AT_MOST. This means I was free to change the width and height during every pass of onMeasure, and so I could end up calculating my aspect ratio as desired given the available area.

还没有确定下来,这适用于所有情况,但希望这将帮助别人。

Haven't confirmed yet that this works for all cases, but hopefully this will help someone.

这篇关于填写与固定纵横比surfaceview剩余空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:26