在android中使用ScaleGestureDetector时需要帮助

时间:2012-03-23 11:03:06

标签: android

我使用以下代码来检测视图的ScaleGesture。 ScaleGestureDetector工作正常。我在布局中显示了两张图片。在检测ScaleGesture时,我减少并增加了图像的大小。我还需要单击或触摸图像以执行某些过程。如果我使用setOnClickListener而不是setOnTouchListener,则ScaleGestureDetector不起作用。在用两根手指触摸屏幕时,setOnTouchListener在ScaleGestureDetector之前触发。如何实现ontouch和scaleGesture。

package com.pinch.detect;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class PinchDetectorActivity extends Activity {
    TextView textGestureAction;
    ImageView img1,img2;
    static Bitmap bm, bm1;
    String url1="url1111";
        String url2="url2222";

    private ScaleGestureDetector scaleGestureDetector;

      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          textGestureAction = (TextView)findViewById(R.id.GestureAction);
          img1=(ImageView)findViewById(R.id.img_left);
          img2=(ImageView)findViewById(R.id.img_right);

        try {
            URL aURL = new URL(url1);
            // parseBitmap(aURL);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();

            /* Buffered is always good for a performance plus. */
            BufferedInputStream bis = new BufferedInputStream(is);

            /* Decode url-data to a bitmap. */
            bm = BitmapFactory.decodeStream(bis);

            bis.close();
            is.close();

            /* Apply the Bitmap to the ImageView that will be returned. */

            img1.setImageBitmap(bm);

            URL aURL1 = new URL(url2);
            URLConnection conn1 = aURL1.openConnection();
            conn1.connect();
            InputStream is1 = conn1.getInputStream();

            BufferedInputStream bis1 = new BufferedInputStream(is1);

            bm1 = BitmapFactory.decodeStream(bis1);

            bis1.close();
            is1.close();

            img2.setImageBitmap(bm1);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          scaleGestureDetector = new ScaleGestureDetector(this,
            new MySimpleOnScaleGestureListener());

          img1.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "clicked image 1", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

          img2.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "clicked image 2", Toast.LENGTH_SHORT).show();
                    return false;
                }
            });

         /* img1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "clicked image 1", Toast.LENGTH_SHORT).show();
                }
            });
          img2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "clicked image 2", Toast.LENGTH_SHORT).show();
                }
            });*/
      }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
     // TODO Auto-generated method stub
     scaleGestureDetector.onTouchEvent(event);
     return true;
    }

    public class MySimpleOnScaleGestureListener extends
    SimpleOnScaleGestureListener {

     @Override
     public boolean onScale(ScaleGestureDetector detector) {
      // TODO Auto-generated method stub

      float scaleFactor = detector.getScaleFactor();
      if(scaleFactor > 1){
          Log.v("inside scale factor if","if");
       textGestureAction.setText("Scale Out: " + String.valueOf(scaleFactor));

        Bitmap resizedbitmap = Bitmap.createScaledBitmap(bm, 200,
                480, true);
        img1.setImageBitmap(resizedbitmap);
        Bitmap resizedbitmap1 = Bitmap.createScaledBitmap(bm1, 200,
                480, true);
        img2.setImageBitmap(resizedbitmap1);

      }else{
          Log.v("inside scale factor else","else");
       textGestureAction.setText("Scale In: " + String.valueOf(scaleFactor));


        Bitmap resizedbitmap = Bitmap.createScaledBitmap(bm, 400,
                480, true);

        img1.setImageBitmap(resizedbitmap);

        Bitmap resizedbitmap1 = Bitmap.createScaledBitmap(bm1, 400,
                480, true);

        img2.setImageBitmap(resizedbitmap1);


      }

      return true;
     }
    }
    }

2 个答案:

答案 0 :(得分:1)

onTouchEvent()应该像这样实现:

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean handled = scaleGestureDetector.onTouchEvent(event);
    return super.onTouchEvent(event) || handled;
}

答案 1 :(得分:1)

您在活动中覆盖了onTouchEvent。在此方法中,您将事件传递给ScaleGestureDetector,但问题是此处仅发送未由子视图处理的触摸事件。您在子onTouch()方法中返回false。这很好......接下来应该调用你活动中的onTouchEvent。

如果您想要使用应发送到子视图的事件,您应该查看ViewGroup中的onInterceptTouchEvent(MotionEvent事件)。 http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)。在这里,如果您想要使用该事件,请返回true。以下事件将发送到ViewGroup的onTouchEvent方法。如果您返回false,则事件将发送给ViewGroup的子项。

当然,您必须扩展ViewGroup才能覆盖此方法。

如果这是对您的问题的正确理解,请告诉我。如果可能的话,我可以尝试帮助更多。