【问题标题】:How to scale a View without it moving X, Y position?如何在不移动 X、Y 位置的情况下缩放视图?
【发布时间】:2016-02-02 18:04:41
【问题描述】:

我有一个RadioGroup,我正试图缩小它,因为它太大了。所以我使用setScaleX()setScaleY() 并按比例缩小。

它可以工作,但问题是当我缩放视图时视图会改变 X 和 Y 位置。我希望它在缩放后保持相同的左上角坐标。缩放后如何使视图保持不变?

见下面的代码:

CSRadioButton radiobutton = (CSRadioButton) field;
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.VERTICAL);
JSONArray choices = radiobutton.getChoices();
for(int j = 0; j < choices.length(); j++){
    JSONObject currObj = choices.getJSONObject(j);
    String text = currObj.getString("text");
    RadioButton rb = new RadioButton(this);
    rb.setText(text);
    rg.addView(rb);
}

rscroll.addView(rg, lp);

rg.setScaleX(0.7f);
rg.setScaleY(0.7f); 

【问题讨论】:

    标签: android view scale


    【解决方案1】:

    缩放和旋转View 的默认轴心点是它的中心。您可以使用View.setPivotX()View.setPivotY() 设置所需的枢轴坐标:

    rscroll.addView(rg, lp);
    
    rg.setPivotX(0);
    rg.setPivotY(0);  
    
    rg.setScaleX(0.7f);
    rg.setScaleY(0.7f); 
    

    这将导致左上角保持固定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多