且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

安卓:在任意位置放置一个视图

更新时间:2023-11-19 14:19:22

解决方案是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