【发布时间】:2014-04-29 02:46:13
【问题描述】:
我有一个视图,我在其中绘制矩形和 10 条线。我正在使用捏缩放来缩放视图,但我得到了意想不到的行为。线条正在缩放,但矩形不是。
谁能指出我的错误。
视图的onDraw()方法是:
onDraw() method of the view is:
Rect r =new Rect(0, 0, 700, 40);
public void onDraw(Canvas canvas) {
float kk=mScaleFactor; //this variable will be set by pinch zoom event and represent how much to scale
sf*=kk; // this view must scale according to previous scaling
canvas.save();
makeLinesinRangetwo(10, canvas); // this will make l0 lines within the width of rectangle
float cX = canvas.getWidth()/2.0f; //Width/2 gives the horizontal centre
float cY = canvas.getHeight()/2.0f; //Height/2 gives the vertical centre
canvas.scale(sf , 1,cX,cY);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLUE);
canvas.drawRect(r, paint);
canvas.restore();
requestLayout(); //this will change view width to fit the expanded rectangle
}
当我缩放 (0,0) 时也是如此。矩形和线条都在缩放。我没有得到这种行为。
public void onDraw(Canvas canvas) {
float kk=mScaleFactor; //this variable will be set by pinch zoom event and represent how much to scale
sf*=kk; // this view must scale according to previous scaling
canvas.save();
makeLinesinRangetwo(10, canvas); // this will make l0 lines within the width of rectangle
canvas.scale(sf , 1);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLUE);
canvas.drawRect(r, paint);
canvas.restore();
requestLayout(); //this will change view width to fit the expanded rectangle
}
【问题讨论】:
-
我假设出现在
canvas.scale中的sf参数应该是在onDraw开头定义的kk?此外,您应该在设置比例之前保存 Canvas 的矩阵,然后在完成后恢复它。否则,每次触发捏合事件时,您最终都会累积缩放矩阵。这 10 条线的缩放比例是否真正正确? -
@Saran 我已经编辑了代码。是的,我在矩形内的线条正在缩放。但矩形不是。
-
@Saran 我也遇到了非常意外的行为。我已经编辑了问题并在那里发布了我的问题。
标签: android performance android-layout layout pinchzoom