在 ConstraintLayout 中以编程方式将 ImageView 的大小和位置设置为百分比 (Android)

Setting ImageView's size AND position as percentages programmatically in ConstraintLayout (Android)

如标题中所述,我正在尝试以编程方式在 ConstraintLayout 中将多个 ImageView 的大小和位置设置为百分比。

我通过 ConstraintSet 或 ConstraintLayout.LayoutParams 成功地单独完成了,但是当我尝试同时进行这两个操作时,似乎没有任何效果。

我也尝试过使用 Guidelines 设置位置和 ConstraintSet 设置大小,但仍然没有结果。

这是我想要实现的 for 循环(位置工作,不知道如何做尺寸)

ConstraintLayout layout = findViewById(R.id.mapView);
for(Point p : getCoords()){
            ImageView iv = new ImageView(this);
            iv.setId(View.generateViewId());

            iv.setLayoutParams(getFeatureLayout(p.x/100f, p.y/100f));

            iv.setImageDrawable(getResources().getDrawable(R.drawable.area));

            layout.addView(iv);
        }

getFeatureLayout方法如下

private ConstraintLayout.LayoutParams getFeatureLayout(float percentFromLeft, float percentFromTop){
        assert percentFromTop>=0 && percentFromTop<=1 : "Inputs must be 0<=x<=1";
        assert percentFromLeft>=0 && percentFromLeft<=1 : "Inputs must be 0<=x<=1";

        ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(
                ConstraintProperties.WRAP_CONTENT,
                ConstraintProperties.WRAP_CONTENT
        );

        params.bottomToBottom= ConstraintProperties.PARENT_ID;
        params.topToTop= ConstraintProperties.PARENT_ID;
        params.startToStart= ConstraintProperties.PARENT_ID;
        params.endToEnd= ConstraintProperties.PARENT_ID;

        params.verticalBias=percentFromTop;
        params.horizontalBias=percentFromLeft;

        return params;
    }

好的,我终于让它工作了,如果有人需要它,请发布解决方案

// Define a constraint set that will be used to modify the constraint layout parameters of the child.
ConstraintSet set = new ConstraintSet();
// Start with a copy the original constraints.
set.clone(mapView);
// The background of the ConstraintLayout (i need it for width-height ratio)
Drawable map = getResources().getDrawable(R.drawable.pvp_map);
float heightPercentSize = .1f; // The area size will be this much of the map's height
// Defining the width-height ratio so that 
// width*heightPercentSize*ratioAdjuster == height*heightPercentSize
float ratioAdjuster = (float)map.getIntrinsicHeight()/map.getIntrinsicWidth();
        
for(Point p : getPvpCoords()){
        ImageView iv = new ImageView(this);
        Guideline gStart = getNewGuideline(this, ConstraintLayout.LayoutParams.VERTICAL);
        Guideline gTop = getNewGuideline(this, ConstraintLayout.LayoutParams.HORIZONTAL);

        iv.setId(View.generateViewId());

        gStart.setGuidelinePercent(p.x/100f-heightPercentSize/2);
        gTop.setGuidelinePercent(p.y/100f-heightPercentSize/2);
        set.connect(iv.getId(), ConstraintSet.START, gStart.getId(), ConstraintSet.END);
        set.connect(iv.getId(), ConstraintSet.TOP, gTop.getId(), ConstraintSet.BOTTOM);

        set.constrainPercentWidth(iv.getId(), heightPercentSize*ratioAdjuster);
        set.constrainPercentHeight(iv.getId(), heightPercentSize);

        iv.setImageDrawable(getResources().getDrawable(R.drawable.area));
        mapView.addView(gTop);
        mapView.addView(gStart);
        mapView.addView(iv);
}
// Apply the constraints for the child view to the parent layout.
set.applyTo(mapView);