【问题标题】:Unexpected scaling behavior on a canvas in androidandroid中画布上的意外缩放行为
【发布时间】: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


【解决方案1】:

我认为问题在于您在中心缩放画布,但您应该将 pivotX 和 pivotY 指定为矩形的中心。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 2012-05-21
    • 1970-01-01
    • 2011-06-22
    • 2011-05-29
    • 2016-08-26
    相关资源
    最近更新 更多