代码之家  ›  专栏  ›  技术社区  ›  Seth

使用setScaleX()setScaleY()更改android窗口大小

  •  3
  • Seth  · 技术社区  · 9 年前

    我正在使用WindowManager添加视图。

    WindowManager.LayoutParameters 我将宽度和高度都设置为WRAP_CONTENT,因为内容应该决定视图的大小。

    然而,我允许用户调整布局的整体大小。

    然后,当我创建布局时,我只需应用保存的比例值和中提琴,它会显示新调整的视图。

    问题是,尽管缩放了实际视图,我还是添加了如下所示:

    myView.setScaleX(scaleFactor);
    myView.setScaleY(scaleFactor);
    mWindowManager.addView(myView, params);
    

    在我缩小的视野周围,似乎还有某种“容器”。

    所以我想 setScaleX() setScaleY() ,改为在我的视图中添加一个可运行的,以便在完成绘制和调用后运行 getWidth() getHeight() 然后使用我的 scaleFactor 值来计算新的高度和宽度。这是有效的,但现在一切都被切断了,因为 设置缩放X() 设置缩放Y() 实际上缩小了 每件事 在视图内部。整个 Paint 对象被缩小,包括文本、文本之间的空间和所有内容。

    有人知道我如何使绝对尺寸与缩小视图的尺寸相同吗?

    编辑:所以在搞乱了这个之后。我认为我需要做的是弄清楚如何调整父布局的大小,并允许裁剪其子布局而不是调整其大小。

    例如:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:id="@+id/parent_layout"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"/>
    </RelativeLayout>
    

    在上面的布局中,我缩放 TextView 对象使用 设置缩放X() 设置缩放Y() 这会导致textView缩放,但不会更改其实际尺寸。

    然后,我需要做的是获得 RelativeLayout 并将其乘以浮点标度值。这将获得新的尺寸。然后我需要在不改变 文本框 项目。

    1 回复  |  直到 9 年前
        1
  •  4
  •   Seth    9 年前

    所以我找到了答案,并发布了答案和代码。

    使用时 setScaleX() setScaleY() 它会根据视图的中心进行缩放。这将导致视图的顶部和左侧位置缩小或增大,而不会更改视图的实际尺寸。

    因此,为了补偿,我们必须从儿童的角度出发( textView1 )把它向左上移动,我们把它缩小了多少。

    下面是执行此操作的代码:

    RelativeLayout parent; //THis is the parent view that you added with a WindowManager object.
    TextView textView1; //This is the child view of our parent. For this example, I am using a TextView.
    float factor; //This is the amount we scaled our view by.
    parent.post(new Runnable() { //When you post a runnable to a view, it runs after the view is drawn and measured.
        @Override
        public void run() {
            int newWidth = Math.round(parent.getWidth() * factor), newHeight = Math.round(parent.getHeight() * factor); //Calculate what the new height and width will be for the parent view to get resized to.
            WindowManager.LayoutParams pp = (WindowManager.LayoutParams)parent.getLayoutParams(); //Get an instance of the parent LayoutParams so we can set the new height and width measurements.
            RelativeLayout.LayoutParams ch = (RelativeLayout.LayoutParams)chatHead.getLayoutParams(); //Get the child's layout parameters so we can adjust the margins as needed.
            int diffX = parent.getWidth() - newWidth, diffY = parent.getHeight() - newHeight; //Calculate the difference in sizes from the newly scaled size to the old size.
            diffX = -Math.round(diffX / 2); //Calculate the amount of space needed to move the child view inside its parent. The negative sign is needed here depending how you calculate the differences.
            diffY = -Math.round(diffY / 2); //Same as above, but for the height.
            ch.setMargins(diffX, diffY, Integer.MIN_VALUE, Integer.MIN_VALUE); //Set the new margins for the child view. This will move it around so it is anchored at the top left of the parents view. 
            rootChatHead.updateViewLayout(chatHead, ch); //Apply the new parameters.
            pp.width = newWidth; //Set the parent's new width.
            pp.height = newHeight; //Set the parent's new height.
            windowManager.updateViewLayout(rootChatHead, pp); //Update the parent view.
    }
    

    现在,我们的视图缩小了,容器的大小已经调整,以占用子视图缩放所导致的任何额外空间。

    如果将子视图缩放得更高,此方法也适用。它将重新调整父母的大小以适应孩子的新大小。