简体   繁体   中英

Can Anyone Tell me Why All Rects fires to same Activity

* MyImage class Extends View And Implements ontouchListener * enter code here

public class MyImage extends View implements OnTouchListener {

Bitmap orignal;
Bitmap scaled;
Matrix m;
Context ctext;
ArrayList<Rect> rect;

I have Four Rectangle and all Rectangle are used to move to diff diff Activity Rect rectangle0; Rect rectangle1; Rect rectangle2; Rect rectangle3; Rect rectangle4; private int X_POSITION; private int Y_POSITION;

public MyImage(Context context) {
    super(context);

    ctext = context;

}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

@Override
public View findFocus() {
    // TODO Auto-generated method stub
    return super.findFocus();
}

@Override
public boolean isPressed() {
    // TODO Auto-generated method stub
    return super.isPressed();

}

@Override
public boolean isSelected() {
    // TODO Auto-generated method stub
    return super.isSelected();

}



@Override
public boolean onTouchEvent(MotionEvent event) {


     X_POSITION = (int)event.getX();
     Y_POSITION = (int)event.getY();

    System.out.println(X_POSITION + "\t" +Y_POSITION); 

The Problem Occur here i dnt know why all rectangle fires to same activity and how can it be solved so that all rectangle fire to diff actvity

    switch (event.getAction()) {

    case MotionEvent.ACTION_DOWN:


        Intent i = new Intent(ctext , SelecterImage.class);
        ctext.startActivity(i);

    }

return true;

}

@Override
public void onDraw(Canvas canvas) {
    Paint paint = new Paint();

    int X_WIDTH = canvas.getWidth();
    int Y_HEIGHT = canvas.getHeight() / 2;

    System.out.println(X_WIDTH + "\n" + Y_HEIGHT);

    Paint rectanglePaint = new Paint();
    rectanglePaint.setARGB(255, 0, 0, 0);
    rectanglePaint.setStrokeWidth(2);
    rectanglePaint.setColor(Color.BLUE);

    rectangle0 = new Rect(10, 420, 60, 380);
    rectangle1 = new Rect(70, 420, 120, 380);


    rectangle2 = new Rect(130, 420, 180, 380);
    rectangle3 = new Rect(190, 420, 240, 380);
    rectangle4 = new Rect(250, 420, 300, 380);


    canvas.drawRect(rectangle0, rectanglePaint);

    canvas.drawRect(rectangle1, rectanglePaint);

    canvas.drawRect(rectangle2, rectanglePaint);

    canvas.drawRect(rectangle3, rectanglePaint);

    canvas.drawRect(rectangle4, rectanglePaint);

    scaled = Bitmap.createScaledBitmap(orignal, X_WIDTH, Y_HEIGHT, false);

    canvas.drawBitmap(scaled, 0, 0, paint);

}


void setBitmap(Bitmap bitmap) {
    // TODO Auto-generated method stub

    orignal = bitmap;

}



@Override
public boolean onTouch(View v, MotionEvent event) {
    return false;
    // TODO Auto-generated method stub




}   


}

strong text

First make sure that the onTouchEvent works correctly (does actually returns you X and Y like you do) and then

@Override 
public boolean onTouchEvent(MotionEvent event) { 


    X_POSITION = (int)event.getX(); 
    Y_POSITION = (int)event.getY(); 

    if (event.getAction == MotionEvent.ACTION_DOWN)
    {
        if (rectangle0.contains(X_POSITION,Y_POSITION))
        {
           Intent i = new Intent(ctext , Activity1.class); 
           ctext.startActivity(i); 
        }
        else if (rectangle1.contains(X_POSITION,Y_POSITION))
        {
           Intent i = new Intent(ctext , Activity2.class); 
           ctext.startActivity(i); 
        }

        ....

    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM