无法捏合和放大图像

时间:2013-01-11 04:53:04

标签: android image

我正在尝试在我的代码中实现捏合和缩放功能,但我遇到了一些错误。

我在onCreate()

中添加了一个onTouchListener方法
@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            displayImage();
            viewPager = (ViewPager) findViewById(R.id.view_pager);
            viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
            viewPager.setOnPageChangeListener(this);
            viewPager.setOnTouchListener(this);

            //gallery = (Gallery) findViewById(R.id.gallery);
            //gallery.setAdapter(new ImageAdapter());
            //gallery.setOnItemSelectedListener(this);

            //textView = (TextView) findViewById(R.id.title);

            handler = new Handler();

        }

然后这是我在网上检索的onTouch方法。

@Override
public boolean onTouch(View v, MotionEvent event) {
    ImageView imageView = new ImageView(Main.this);
    imageView = (ImageView) v;
    imageView.setScaleType(ImageView.ScaleType.MATRIX);
       float scale;

       // Dump touch event to log
       dumpEvent(event);

       // Handle touch events here...
       switch (event.getAction() & MotionEvent.ACTION_MASK) {

       case MotionEvent.ACTION_DOWN: //first finger down only
          savedMatrix.set(matrix);
          start.set(event.getX(), event.getY());
          Log.d(TAG, "mode=DRAG" );
          mode = DRAG;
          Log.i("saran","mode === drag");
          break;
       case MotionEvent.ACTION_UP: //first finger lifted
           Log.i("saran","action up");
       case MotionEvent.ACTION_POINTER_UP: //second finger lifted
          mode = NONE;
          Log.i("saran", "mode=NONE" );
          break;
       case MotionEvent.ACTION_POINTER_DOWN: //second finger down
          oldDist = spacing(event);
          Log.i("saran", "oldDist=" + oldDist);
          if (oldDist > 5f) {
             savedMatrix.set(matrix);
             midPoint(mid, event);
             mode = ZOOM;
             Log.i("saran", "mode=ZOOM" );
          }
          break;

       case MotionEvent.ACTION_MOVE: 
          /*if (mode == DRAG) { //movement of first finger
             matrix.set(savedMatrix);
             if (view.getLeft() >= -392){
                matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
             }
             Log.i("saran", "action move.....");
          }
          else*/ if (mode == ZOOM) { //pinch zooming
             float newDist = spacing(event);
             Log.d(TAG, "newDist=" + newDist);
             if (newDist > 5f) {
                matrix.set(savedMatrix);
                scale = newDist / oldDist; //**//thinking i need to play around with this value to limit it**
                matrix.postScale(scale, scale, mid.x, mid.y);
             }
             Log.i("saran", "zoom.......");
          }
          break;
       }

       // Perform the transformation
       imageView.setImageMatrix(matrix);

       return true; // indicate event was handled
    }

    private float spacing(MotionEvent event) {
       float x = event.getX(0) - event.getX(1);
       float y = event.getY(0) - event.getY(1);
       return FloatMath.sqrt(x * x + y * y);
    }

    private void midPoint(PointF point, MotionEvent event) {
       float x = event.getX(0) + event.getX(1);
       float y = event.getY(0) + event.getY(1);
       point.set(x / 2, y / 2);
    }

    /** Show an event in the LogCat view, for debugging */
    private void dumpEvent(MotionEvent event) {
       String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
          "POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
       StringBuilder sb = new StringBuilder();
       int action = event.getAction();
       int actionCode = action & MotionEvent.ACTION_MASK;
       sb.append("event ACTION_" ).append(names[actionCode]);
       if (actionCode == MotionEvent.ACTION_POINTER_DOWN
             || actionCode == MotionEvent.ACTION_POINTER_UP) {
          sb.append("(pid " ).append(
          action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
          sb.append(")" );
       }
       sb.append("[" );
       for (int i = 0; i < event.getPointerCount(); i++) {
          sb.append("#" ).append(i);
          sb.append("(pid " ).append(event.getPointerId(i));
          sb.append(")=" ).append((int) event.getX(i));
          sb.append("," ).append((int) event.getY(i));
          if (i + 1 < event.getPointerCount())
             sb.append(";" );
       }
       sb.append("]" );
       Log.d(TAG, sb.toString());

}

这是我的logcat输出

01-11 12:45:06.650: E/AndroidRuntime(16862): FATAL EXCEPTION: main
01-11 12:45:06.650: E/AndroidRuntime(16862): java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to android.widget.ImageView
01-11 12:45:06.650: E/AndroidRuntime(16862):    at com.github.browep.viewpagergallery.Main.onTouch(Main.java:185)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.View.dispatchTouchEvent(View.java:5536)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1951)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1684)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1912)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1860)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.View.dispatchPointerEvent(View.java:5721)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2890)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2475)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.os.Looper.loop(Looper.java:137)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at android.app.ActivityThread.main(ActivityThread.java:4446)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at java.lang.reflect.Method.invokeNative(Native Method)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at java.lang.reflect.Method.invoke(Method.java:511)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-11 12:45:06.650: E/AndroidRuntime(16862):    at dalvik.system.NativeStart.main(Native Method)
出了什么问题?我尝试使用imageview作为getView()设置,但它不起作用。请指出我的错误和帮助将不胜感激!非常感谢你

1 个答案:

答案 0 :(得分:0)

viewPager.setOnTouchListener(this);

设置ViewPager的监听器,但在监听器内部,您可以通过

将视图转换为ImageView
imageView = (ImageView) v;

您应该将听众附加到ImageView