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

0 人关注

如标题所述,我试图在一个ConstraintLayout中以编程方式设置多个ImageViews的大小和位置百分比。

我成功地通过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;
    
java
android
position
size
percentage
vittoema96
vittoema96
发布于 2022-04-02
1 个回答
vittoema96
vittoema96
发布于 2022-04-03
已采纳
0 人赞同

好吧,我终于让它工作了,如果有人需要的话,我把解决方案贴出来。

// 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);