安卓:在任意位置放置一个视图 [英] Android: Placing a view in an arbitrary location

查看:124
本文介绍了安卓:在任意位置放置一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力放在任意位置的看法。

我的目标是::要覆盖一个JPG / PNG的一些长方形,定坐标的,涉及到JPG / PNG 的,与其他一些观点,比如画廊,或一些视频。

我不想使用AbsoluteLayout,因为它是depricated

因此​​,我使用的RelativeLayout,定义一个虚拟文本框作为占位符,并把我的观点RIGHT_TO和下面的文本框。

  + -------- +
|的TextView |
| | (X,Y)
+ -------- + ----------------------- +
         | |
         |我的视图|
         | |
         + ----------------------- +

我的问题是:有没有更有力,更优雅的方式来做到这一点。

以上建议的方式问题,是它的非常脆弱:坐标需要重新计算每一个新的屏幕。我的看法如下覆盖的东西。它需要准确地放置,这是pretty很难做到的。


  • 我将需要重新布局的屏幕,只要旋转或变或什么的。 更糟糕的:

    • 在初始化时间,用于放置图像的典型时间,画面的坐标是无效的。坐标是放置实体在屏幕上才有效。为了准确地放置在屏幕上的实体,我们需要坐标。鸡和蛋的问题:(

    • 在旋转,坐标包括利润率,而不是视图本身(的宽度,例如:在肖像AVD,显示人像ImageView的,那么preSS CTRL + F12旋转左,右,上和是完全没有达到预期的ImageView的底部)。

    • 当量表动画效果,坐标缩放。


所以,我想找到一个更好的方式来做到这一点。

 感谢
    M.


解决方案

解决方案是pretty丑陋的,它不是相对的,但有些人可能说这是合理的:

添加的FrameLayout作为容器(而非RelativeLayout的下面说明)。要显示的看法是的FrameLayout的孩子。
为了将它在的地方,添加填充。

 无效addViewInAnArbitraryRect(矩形RECT,
                               上下文的背景下,
                               查看subjectView,
                               查看父){
    集装箱的FrameLayout =新的FrameLayout(背景);
    parent.addView(容器);
    container.setPadding(rect.left,
                          rect.top,
                          container.getWidth() - rect.right,
                          container.getHeight() - rect.bottom);
    container.addView(subjectView);}

注意:您可能希望将页面的坐标调整到屏幕上​​的坐标。只要记住:


  1. 坐标/宽/高都没有准备好,直到onSizeChanged被调用。

  2. 请不要操纵来自onSizeChanged范围的意见,也可以剪裁自己的看法。意见就必须从创建它们的线程的上下文中操作。使用处理器为。

我希望它帮助别人。

Meymann

I have been trying to place a view in an arbitrary location.

My aim: To overlay some rectangle of a JPG/PNG, given coordinates that relate to the JPG/PNG, with some other view, say, Gallery, or some video.

I don't want to use AbsoluteLayout, as it is depricated.

Therefore, I am using RelativeLayout, defining a dummy textbox as a placeholder, and putting my view RIGHT_TO and BELOW the textbox.

+--------+
|TextView|
|        | (x,y)
+--------+-----------------------+
         |                       |
         |     My View           |
         |                       |
         +-----------------------+

My question is: Is there a more robust and elegant way to do that?

The problem with the way suggested above, is that it's very fragile: The coordinates need to be recalculated for every new screen. My view overlays something below. It needs to be accurately placed, which is pretty difficult to do.

  • I will need to layout the screen again whenever rotated or zoomed or whatever. Worse:
    • On init time, the typical time for placing the images, the coordinates of the screen are not valid. The coordinates are valid after placing the entities on screen. In order to place the entities on screen accurately, we need coordinates. Chicken and egg problem :(
    • On rotation, the coordinates include the margins rather than the width of the view itself (example: on a portrait AVD, display a portrait ImageView, then press CTRL+F12 to rotate. The left, right, top and bottom of the ImageView are totally not as expected).
    • When Scale-Animating, coordinates are scaled.

So I would like to find a better way to do that.

Thanks
    M.

解决方案

The solution is pretty ugly, it's not relative, but some may say it's plausible:

Adding a FrameLayout as a container (rather than the RelativeLayout noted below). The view to display is the child of that FrameLayout. In order to place it in place, add padding.

void addViewInAnArbitraryRect( Rect rect, 
                               Context context, 
                               View subjectView, 
                               View parent ) {
    FrameLayout container = new FrameLayout( context );
    parent.addView( container );
    container.setPadding( rect.left,
                          rect.top,
                          container.getWidth() - rect.right,
                          container.getHeight() - rect.bottom );
    container.addView( subjectView );

}

Note: You may want to adjust the coordinates of the page to the coordinates on screen. Just remember:

  1. Coordinates/width/height are not ready till onSizeChanged is called.
  2. Don't manipulate Views from onSizeChanged scope, or it may crop your view. Views do have to be manipulated from the context of the thread that created them. Use Handler for that.

I hoped it helped somebody.

Meymann

这篇关于安卓:在任意位置放置一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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