开发者

Error: MyGestureDetector cannot be resolved to a type. how to solve?

开发者 https://www.devze.com 2023-02-27 06:56 出处:网络
Hi I had download sample gallery resource. If i run it\'s indicate MyGestureDectector cannot be resolved to a type error....how to solve these error? please help me.....

Hi I had download sample gallery resource. If i run it's indicate MyGestureDectector cannot be resolved to a type error....how to solve these error? please help me.....

coding:

package Gallery.Gallery;

 import android.app.Activity; 
 import android.content.Context; 
 import android.graphics.Matrix; 
 import android.graphics.PointF; 
 import android.os.Bundle; 
 import android.util.FloatMath; 
 import android.view.GestureDetector; 
 import android.view.MotionEvent; 
 import android.view.View; 
 import android.view.View.OnClickListener; 
 import android.widget.Button; 
 import android.widget.ImageView;

 public class Gallery extends Activity { 
 public ImageView img1,img2,img3; 
 public static ImageView contentImage; 
 boolean click1=true; 
 boolean click2=true; 
 boolean click3=true; 
 boolean imageTouch=false; 
 Context myContext=Gallery.this; 
 Matrix matrix = new Matrix(); 
 Matrix savedMatrix = new Matrix(); 
 PointF start=new PointF(); 
 PointF mid = new PointF(); 
 float oldDist = 1f;
 static final int NONE = 0;
 static final int DRAG = 1;
 static final int ZOOM = 2;
 int mode = NONE;
 public GestureDetector mDetector;

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mDetector = new GestureDetector(Gallery.this,new MyGestureDetector(Gallery.this));

    Button btn1=(Button)findViewById(R.id.button1);
    Button btn2=(Button)findViewById(R.id.button2);
    Button btn3=(Button)findViewById(R.id.button3);

    img1=(ImageView)findViewById(R.id.imageView1);
    img2=(ImageView)findViewById(R.id.imageView2);
    img3=(ImageView)findViewById(R.id.imageView3);
    contentImage=(ImageView)findViewById(R.id.contentImage);

    img1.setAlpha(100);
    img2.setAlpha(100);
    img3.setAlpha(100);

    btn1.setOnClickListener(new View.On开发者_StackOverflow社区ClickListener()
    {
        @Override
        public void onClick(View arg0) 
        {
                if (click1) 
            {
                img1.setAlpha(255);
                img2.setAlpha(100);
                img3.setAlpha(100);
                click1=false;
            } 
            else 
            {
                img1.setAlpha(255);
                img2.setAlpha(100);
                img3.setAlpha(100);
                click1=true;
            }
        }
    });

    btn2.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View arg0) 
        {
            if (click2) 
            {
                img1.setAlpha(100);
                img2.setAlpha(255);
                img3.setAlpha(100);
                click2=false;
            } 
            else 
            {
                img1.setAlpha(80);
                img2.setAlpha(255);
                img3.setAlpha(80);
                click2=true;
            }
        }
    }); 


        btn3.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View arg0) 
        {
            if (click3) 
            {
                img1.setAlpha(100);
                img2.setAlpha(100);
                img3.setAlpha(255);
                click3=false;
            } 
            else 
            {
                img1.setAlpha(100);
                img2.setAlpha(100);
                img3.setAlpha(255);
                click3=true;
            }
        }

      });

     img1.setOnClickListener(new OnClickListener()
    {
      @Override
      public void onClick(View v)
     {
        if (click1) 
        {
            img1.setAlpha(255);
            img2.setAlpha(100);
            img3.setAlpha(100);
            click1=false;
        } 
        else 
        {
            img1.setAlpha(255);
            img2.setAlpha(100);
            img3.setAlpha(100);
            click2=true;
        }

        contentImage.setImageResource(R.drawable.img1);
      }
      });

      img2.setOnClickListener(new OnClickListener()
       {
       @Override
       public void onClick(View v)
      {
        if (click2) 
        {
            img1.setAlpha(100);
            img2.setAlpha(255);
            img3.setAlpha(100);
            click2=false;
        } 
        else 
        {
            img1.setAlpha(80);
            img2.setAlpha(255);
            img3.setAlpha(80);
            click2=true;
        }
        contentImage.setImageResource(R.drawable.img2);
    }
    });  
    img3.setOnClickListener(new OnClickListener() 
    {

     @Override
     public void onClick(View v)
     {
        if (click3) 
        {
            img1.setAlpha(100);
            img2.setAlpha(100);
            img3.setAlpha(255);
            click3=false;
        } 
        else 
        {
            img1.setAlpha(100);
            img2.setAlpha(100);
            img3.setAlpha(255);
            click3=true;
        }
        contentImage.setImageResource(R.drawable.img3);
     }
      });      

    contentImage.setOnTouchListener(new View.OnTouchListener()
    {

    @Override
    public boolean onTouch(View v, MotionEvent event) 
    {

            ImageView view=(ImageView)v;
           System.out.println("matrix="+savedMatrix.toString());
           switch (event.getAction() & MotionEvent.ACTION_MASK) 
           {
           case MotionEvent.ACTION_DOWN:

               savedMatrix.set(matrix);
                 start.set(event.getX(), event.getY());
                 mode = DRAG;
                 break;


          case MotionEvent.ACTION_POINTER_DOWN:

                oldDist = spacing(event);

                 if (oldDist > 10f) 
                 {
                    savedMatrix.set(matrix);
                    midPoint(mid, event);
                    mode = ZOOM;
                 }
                 break;

           case MotionEvent.ACTION_UP:


           case MotionEvent.ACTION_POINTER_UP:
                 mode = NONE;

                 break;


           case MotionEvent.ACTION_MOVE:
                  if (mode == DRAG) 
                  {
                     matrix.set(savedMatrix);
                     matrix.postTranslate(event.getX() - start.x,
                     event.getY() - start.y);
                  }
                  else if (mode == ZOOM) 
                     {
                        float newDist = spacing(event);
                        if (newDist > 10f) 
                        {
                           matrix.set(savedMatrix);
                           float scale = newDist / oldDist;
                           matrix.postScale(scale, scale, mid.x, mid.y);
                        }
                     }
                  break;

           }
           view.setImageMatrix(matrix);


        return true;
    }
    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);
       }
   });

   }//onCreate Method over..... 

   }

Error line:

mDetector = new GestureDetector(Gallery.this,new MyGestureDetector(Gallery.this));

MyGestureDetector cannot be resolved to a type Gallery.java /Gallery/src/Gallery /Gallery line 41 Java Problem


you need to create a MyGestureDetector class if you are going to use it, and I think you are looking for MyGestureDetectorListener.

     GestureDetector(Context context, GestureDetector.OnGestureListener listener) 
                    Creates a GestureDetector with the supplied listener.

Which you will have to implement


Seems you don't have MyGestureDetector class on class path. Ensure you have this class and compiler know about it.

0

精彩评论

暂无评论...
验证码 换一张
取 消