Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scale a View without it moving X, Y position?

I have a RadioGroup that I'm trying to scale down because it's too big. So I use setScaleX() and setScaleY() and scale it down.

It works but the problem is that the View changes X and Y position when I scale it. I want it to keep the same top left coordinate after scaling. How to make the View stay put after scaling?

See code below:

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); 
like image 433
user112016322 Avatar asked Dec 15 '22 08:12

user112016322


1 Answers

The default pivot point for scaling and rotating a View is its center. You can set the desired pivot coordinates using View.setPivotX() and View.setPivotY():

rscroll.addView(rg, lp);

rg.setPivotX(0);
rg.setPivotY(0);  

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

This will cause the upper left corner to stay fixed.

like image 56
Bö macht Blau Avatar answered Jan 08 '23 10:01

Bö macht Blau