gpt4 book ai didi

android - setScaleX() 不会改变实际 View 大小

转载 作者:行者123 更新时间:2023-11-29 01:35:19 30 4
gpt4 key购买 nike

我有一个 RelativeLayout,它在运行时充满了不同的 View (按钮、复选框...)。

此容器位于名为 TwoDScrollView.java 的 View 中。这基本上是一个 ScrollView ,但允许我在两个方向上滚动。甚至对角线。

它显然只有在其内容大于自身时才可滚动。

如果我现在使用函数 setScaleXsetScaleY 更改我的 RelativeLayout container 的比例,它显然可以正确缩放。问题是,我的容器的实际大小根本没有改变。 (container.getWidth()container.getHeight() 总是给出相同的值)

这是我的 onClick 函数:

    @Override
public void onClick(View v) {
int currentPageId = pager.getCurrentItem();
PageFragment page = (PageFragment)pagePagerAdapater.getItem(currentPageId);
View view = page.getView();
RelativeLayout container = (RelativeLayout) view.findViewById(R.id.container);

float prevScale = pageZoomFactor[currentPageId];

if(v.getId() == R.id.bttZoomIn){
pageZoomFactor[currentPageId] += 0.1f;
}else if(v.getId() == R.id.bttZoomOut){
pageZoomFactor[currentPageId] -= 0.1f;
}else if(v.getId() == R.id.bttZoomReset){
pageZoomFactor[currentPageId] = 1;
}

container.setPivotX(0);
container.setPivotY(0);
container.setScaleX(pageZoomFactor[currentPageId]);
container.setScaleY(pageZoomFactor[currentPageId]);

android.widget.FrameLayout.LayoutParams lp = (android.widget.FrameLayout.LayoutParams) container.getLayoutParams();
lp.width = Math.round((container.getWidth() / prevScale) * pageZoomFactor[currentPageId]);
lp.height = Math.round((container.getHeight() / prevScale) * pageZoomFactor[currentPageId]);

container.setLayoutParams(lp);

System.out.println("Width: "+lp.width);
System.out.println("Height: "+lp.height);
}

如您所见,我已经尝试手动更改大小,但我似乎做不对。它要么比我希望的指数大或小。

最佳答案

我找到了解决办法:

在提到的 TwoDScrollView 中,我更改了 measureChild 函数,如下所示:

@Override
protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
ViewGroup.LayoutParams lp = child.getLayoutParams();
int childWidthMeasureSpec;
int childHeightMeasureSpec;

int scaledWidth = Math.round(lp.width * child.getScaleX());
int scaledHeight = Math.round(lp.height * child.getScaleY());

childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(scaledWidth, MeasureSpec.EXACTLY);
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(scaledHeight, MeasureSpec.EXACTLY);

child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}

我希望这能帮助其他面临类似问题的人。

关于android - setScaleX() 不会改变实际 View 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28580728/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com