在Android中绘制可缩放的形状 [英] Drawing scalable shapes in Android

查看:84
本文介绍了在Android中绘制可缩放的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在android中绘制一个十字形状,我有固定的点集来绘制,



I need to draw a cross shape in android, i have fixed set of points to draw,

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?

推荐答案

使用比例转换;

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



希望这会有所帮助,

Fredrik


Hope this helps,
Fredrik


不要以这种方式使用固定点。首先计算每个点之间的相对距离,然后在绘制每条线时向上或向下放大。
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.


这篇关于在Android中绘制可缩放的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆