使用setScaleX()setScaleY()更改android窗口大小
我正在使用WindowManager添加视图 在使用setScaleX()setScaleY()更改android窗口大小,android,view,android-windowmanager,Android,View,Android Windowmanager,我正在使用WindowManager添加视图 在WindowManager.LayoutParameters中,我设置了包裹内容的宽度和高度,因为内容应该决定视图的大小 但是,我允许用户调整布局的总体大小 然后,当我创建布局时,我只需应用保存的比例值,它就会显示新调整大小的视图 问题在于,尽管缩放了实际视图,我还是添加了如下所示的视图: myView.setScaleX(scaleFactor); myView.setScaleY(scaleFactor); mWindowManager.add
WindowManager.LayoutParameters
中,我设置了包裹内容的宽度和高度,因为内容应该决定视图的大小
但是,我允许用户调整布局的总体大小
然后,当我创建布局时,我只需应用保存的比例值,它就会显示新调整大小的视图
问题在于,尽管缩放了实际视图,我还是添加了如下所示的视图:
myView.setScaleX(scaleFactor);
myView.setScaleY(scaleFactor);
mWindowManager.addView(myView, params);
在我缩小的视野中似乎仍然有某种“容器”
因此,我没有使用setScaleX()
和setScaleY()
,而是向视图中添加一个runnable,以便在绘制完成后运行,并调用getWidth()
和getHeight()
,然后使用我的scaleFactor
值来计算新的高度和宽度。这是可行的,但是现在事情被切断了,因为setScaleX()
和setScaleY()
实际上收缩了视图中的所有内容。整个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>
在上面的布局中,我使用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.
}
现在,我们的视图缩小了,容器的大小已经调整,以占用子视图缩放所产生的任何额外空间
如果将子视图缩放得更高,此方法也有效。它将重新调整父对象的大小以适应子对象的新大小