android中画布上的意外缩放行为
我有一个视图,我在其中绘制矩形和10条线。我正在使用“收缩缩放”来缩放视图,但出现了意外行为。线条正在缩放,但矩形不是 有人能指出我的错误吗 视图的onDraw()方法是:android中画布上的意外缩放行为,android,performance,android-layout,layout,pinchzoom,Android,Performance,Android Layout,Layout,Pinchzoom,我有一个视图,我在其中绘制矩形和10条线。我正在使用“收缩缩放”来缩放视图,但出现了意外行为。线条正在缩放,但矩形不是 有人能指出我的错误吗 视图的onDraw()方法是: onDraw() method of the view is: Rect r =new Rect(0, 0, 700, 40); public void onDraw(Canvas canvas) { float kk=mScaleFact
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
}
我认为问题在于你在画布的中心缩放画布,但是你应该给pivotX和pivotY指定矩形的中心。我假设
canvas.scale
中出现的sf
参数是kk
在onDraw
的开头定义的?此外,您应该在设置比例之前保存画布的矩阵,然后在设置完成后将其还原。否则,每次触发收缩事件时,都会累积缩放矩阵。这10行的缩放是否正确?@Saran我已经编辑了代码。是的,我的矩形内的线条正在缩放。但矩形不是。@Saran我也有非常意想不到的行为。我已经编辑了这个问题并将我的问题发布在那里。