gpt4 book ai didi

java - Android:继承的 View 比例

转载 作者:行者123 更新时间:2023-12-02 12:02:41 26 4
gpt4 key购买 nike

简单的问题:我有一个带有多个子对象的RelativeLayout,其中一个是另一个带有多个子对象的布局。 relativeLayout 可以根据用户的选择进行缩放,这也会缩放其子级。我需要获取子对象的比例因子之一,但即使父对象(或祖对象)缩放,它也始终返回 1.0。因此,我需要一种方法来找出对象的显示比例,而不是其设置比例。有什么方法可以在不手动检查父对象列表及其比例的情况下执行此操作?提前致谢!

最佳答案

我相信我已经明白了。我就是这样做的。

public static float[] getInheritedScale(@NonNull View v)
{
if(v.getParent() == null || !(v.getParent() instanceof View)){
return new float[]{v.getScaleX(), v.getScaleY()};
}

float[] coords = new float[2];

coords[0] = v.getScaleX();
coords[1] = v.getScaleY();

// Iterate through the View's family tree, getting each parent View's scale and calculating
// it into the total scale factor.
boolean done = false;
View current = (View)v.getParent();
while(!done)
{
coords[0] = coords[0] * current.getScaleX();
coords[1] = coords[1] * current.getScaleY();

// Check that we have not reached the top of the tree. If we have, set the done flag.
if(current.getParent() != null && current.getParent() instanceof View){
current = (View)current.getParent();
}else{
done = true;
}
}

return coords;
}

这在我尝试过的每种情况下都有效。它返回传递给它的 View 的显示(或明显)比例(X/Y)。

关于java - Android:继承的 View 比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47128751/

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