在Zoomed Canvas上执行捏合缩放和平移时画布移位

时间:2014-06-28 05:38:36

标签: android touch android-canvas scale

我正在尝试使用大型Canvas(大小为1440 * 2560)开发绘图应用程序。我已经实现了多点触控(双指)捏缩放平移

问题:
在Canvas上执行放大之后 我尝试缩小平移此画布,首先将画布移动一定距离(左侧),然后进行滚动或缩放。

实施
为了实现此应用程序,我创建了大小为3180 * 5420的自定义视图。我已将Canvas放置在此自定义视图的中心。要实现 Pinch-缩放我正在使用 ScaleGestureDetector 。对于平移我使用的是 ScrollView

代码
自定义视图

public class DrawingView extends View {



    private ScaleGestureDetector mScaleDetector;

        private float mScaleFactor = 1.f;

         private static  Bitmap RedoIt= Bitmap.createBitmap(1440,2560,   Config.ARGB_8888);;

         private  static Canvas mcanvas=new Canvas(RedoIt);

         DrawingView drawview=(DrawingView)findViewById(R.id.single_touch_view); 
        @SuppressLint("NewApi") 
        public DrawingView(Context context, AttributeSet attrs) {
            super(context, attrs);
                 paint.setAntiAlias(true);
                 paint.setStrokeWidth(5f);
                 paint.setColor(Color.BLACK);
                 paint.setStyle(Paint.Style.STROKE);
                 paint.setStrokeJoin(Paint.Join.ROUND); 
        } }
        protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        pathCounter++;
        g.setFlag(false);
        canvas.scale(mScaleFactor, mScaleFactor,scalePointX,scalePointY);
           canvas.drawBitmap(RedoIt,870,1430, null); // to place canvas at center

           if(g.getstr().equals("OLIVE"))
              setColorint(128,128,0);
           else if(g.getstr().equals("ORANGE"))
                  setColorint(255,153,0);
           else if(g.getstr().equals("BROWN"))
                  setColorint(165,42,42);
           else
               setColor(g.getstr());



            paths.add(path);
            colorsMap.put(path,paint.getColor());

           canvas.restore();

            }

            public boolean onTouchEvent(MotionEvent event) {

                // Get the coordinates of the touch event.

                   float eventX = event.getX()-870;
                   float eventY = event.getY()-1430;
            **// for Pinch Zoom and panning**   
                mode=1;
                switch (event.getAction()& MotionEvent.ACTION_MASK) {
                case Mot

    ionEvent.ACTION_DOWN:
                x1=event.getX(pointerIndex)-870;
                y1=event.getY(pointerIndex)-1430;

                 return true;

            case MotionEvent.ACTION_POINTER_DOWN:
                mSecondPointerId = event.getPointerId(1);
                counter=0;
                 pointerIndex2 = event.findPointerIndex(mSecondPointerId);
                 x2=event.getX(pointerIndex2)-870;
                 y2=event.getY(pointerIndex2)-1430;

                mScaleDetector = new ScaleGestureDetector(DrawingView.this.getContext() , new ScaleListener());

                 dist1=(float) Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));

            case MotionEvent.ACTION_MOVE:

                {
                x3=event.getX(pointerIndex)-870;
                y3=event.getY(pointerIndex)-1430;
                x4=event.getX(pointerIndex2)-870;
                y4=event.getY(pointerIndex2)-1430;

                dist2=(float) Math.sqrt(Math.pow(x3-x4,2)+Math.pow(y3-y4,2));
                counter++;

                if(counter==4){
            **//To detect two finger Panning action**
                if(Math.abs(dist1-dist2)<5){
                            flag=true;  // Enable panning
                    getParent().requestDisallowInterceptTouchEvent(false);
                    }

                }}
                 break;

            case MotionEvent.ACTION_POINTER_UP: 
                mode =1;
                flag2=false;
                getParent().requestDisallowInterceptTouchEvent(true);

                return true;
                default:

                return false;

        }
        invalidate();
        if(flag==false)// no panning action detected 
            {
                flagZoom=true;
                mScaleDetector.onTouchEvent(event);
                    getParent().requestDisallowInterceptTouchEvent(true);

            }
            return true;
            }
        }

       private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

            public boolean onScaleBegin(ScaleGestureDetector detector) {

                getParent().requestDisallowInterceptTouchEvent(true);
                           super.onScaleBegin(detector);
                return true;    
            }
            public boolean onScale(ScaleGestureDetector detector) {
             scalePointX =  detector.getFocusX();
                  scalePointY = detector.getFocusY();
                    getParent().requestDisallowInterceptTouchEvent(true);
                mScaleFactor *= detector.getScaleFactor();
                mScaleFactor = Math.max(0.25f, Math.min(mScaleFactor, 2.0f));

                invalidate();
                return true;
            }

            public void onScaleEnd(ScaleGestureDetector detector1) {
                  getParent().requestDisallowInterceptTouchEvent(false);
               super.onScaleEnd(detector1);

            }

        }

自定义视图布局文件

<HorizontalScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:fadeScrollbars="false"
    android:id="@+id/horizontalscrollview"
  >

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fadeScrollbars="false"
    android:id="@+id/scrollview">

<AbsoluteLayout 
     android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <!-- This is the view on which we will draw. -->
     <view
        android:layout_width="3180px"
        android:layout_height="5420px"
        android:scaleType="matrix"
     android:layout_x="0px"
     android:layout_y="0px"
        class="com.example.DrawingView"
        android:id="@+id/single_touch_view"
       android:scrollbars="horizontal|vertical"
        android:background="@drawable/brown_wood" />

</AbsoluteLayout>
</ScrollView>
</HorizontalScrollView>

由于我始终保持 ScaleGesturedetector 处于活动状态,这可能是此问题的可能原因。有没有办法禁用 ScaleGesturedetector ?或者代码中的任何其他修改都是必需的。

0 个答案:

没有答案