Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I need to draw a cross shape in android, i have fixed set of points to draw,

Java
Path path = new Path();
path.moveTo(133.133f, 45.7109f);
path.lineTo(154.307f, 24.5363f);
path.lineTo(175.482f, 45.7109f);
path.lineTo(154.307f, 66.8856f);
path.lineTo(175.482f, 88.0603f);
path.lineTo(154.307f, 109.235f);
path.lineTo(133.133f, 88.0603f);
path.lineTo(111.958f, 109.235f);
path.lineTo(90.7835f, 88.0603f);
path.lineTo(111.958f, 66.8856f);
path.lineTo(90.7835f, 45.7109f);
path.lineTo(111.958f, 24.5363f);
path.lineTo(133.133f, 45.7109f);
canvas.drawPath(path, paint);


Working fine upto this,

My Requirement is to scale this shape to various size, i have tried to scale the whole view, but the other elements which is drew in the same canvas was effected. Could you please suggest me the right way to archive this?
Posted
Updated 25-Aug-14 18:59pm
v2

Use a scale Transform;
Matrix matrix = new Matrix();
matrixsetScale(2.0f, 2.0f);
// path is your path
path.transform(matrix); 

Or, if you want to scale around the center of the path;
Matrix matrix = new Matrix();
RectF boundingBox = new RectF();
path.computeBounds(boundingBox, true);
matrix.setScale(2.0f, 2.0f, boundingBox.centerX(), boundingBox.centerY());
path.transform(matrix); 

Hope this helps,
Fredrik
 
Share this answer
 
Comments
User 1234567 26-Aug-14 23:59pm    
Works like charm. thanks
Fredrik Bornander 27-Aug-14 3:40am    
Glad I could help.
Using matrices will allow you to easily scale, skew, rotate and translate (move) paths.
They're awesome.
Don't use fixed points in this way. First calculate the relative distance between each point and then scale up or down as you draw each line.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900