Resize a path in android canvas

11,526

Solution 1

I have tried the solution provided by smitalm. Still the path was shifting its location. I have tried this way and it worked for me.

Matrix scaleMatrix = new Matrix();
RectF rectF = new RectF();
path.computeBounds(rectF, true);
scaleMatrix.setScale(1.25f, 1.25f,rectF.centerX(),rectF.centerY());
path.transform(scaleMatrix); 

Solution 2

Havent done this by myself, but you should probably use

Matrix scaleMatrix = new Matrix();
scaleMatrix.setScale(sx,sy, px, py);
p.transform(scaleMatrix); 

where sx,sy should be 2,2 in your case, if you want double size and px,py should probably be 0.5,0.5 in your case

Share:
11,526
Zach
Author by

Zach

Updated on July 21, 2022

Comments

  • Zach
    Zach almost 2 years

    I have drawn an path in my canvas. On Single tap I want to resize the path. Say I want to make it double the original size.

    I have tried path.transform but it shifts the path to a different location in canvass. This is the code I used for that

    Matrix translateMatrix = new Matrix();
    translateMatrix.setTranslate(100,0);
    p.transform(translateMatrix); 
    

    How can I resize a path in android?

  • Zach
    Zach almost 11 years
    Thanks for your response. I tried your code. It is scaling but at the same time it shifts its position. I want to scale it on centre. How can I do that?
  • hendrix
    hendrix almost 11 years
    @Zach then you have to use setScale version that allows pivots, see edited answer.
  • Mubashir Murtaza
    Mubashir Murtaza almost 4 years
    The rectF.centerX() and rectF.centerY() will help you to scale the path w.r.t its center position, 1.25f,1.25f are (x and y axis to scale) ,, PS: FOR SCALE LARGE (x,y axis)= 1.001; FOR SCALE SMALL (x,y axis) = 0.999;