使用 setScaleX() setScaleY() 更改 android window 大小

Change android window size with setScaleX() setScaleY()

我正在使用 WindowManager 添加视图。

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

但是,我允许用户调整布局的整体大小。

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

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

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

我的 sh运行k 视图似乎仍然存在某种 "container"。

所以我想不使用 setScaleX()setScaleY(),而是在完成绘制和调用 getWidth()getHeight() 然后使用我的 scaleFactor 值来计算新的高度和宽度。这行得通,但现在事情被切断了,因为 setScaleX()setScaleY() 实际上缩小了视图内的 everything。整个 Paint 对象是 sh运行k,包括文本,space 介于文本和所有内容之间。

有谁知道如何使绝对尺寸与 sh运行k 视图的尺寸相同?

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

例如:

<?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>

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

然后我需要做的是获取 RelativeLayout 的尺寸并将其乘以浮点比例值。这将获得新的尺寸。然后我需要在不更改 TextView 对象的维度的情况下更新这些维度。

所以我弄明白了并发布了答案和代码。

当使用 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.
}

现在我们的视图缩小了,容器的大小也被调整以占用因缩放我们的子视图而产生的任何额外 space。

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