0

* MyImage 类扩展视图并实现 ontouchListener * enter code here

公共类 MyImage 扩展视图实现 OnTouchListener {

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

我有四个矩形,所有矩形都用于移动到 diff diff Activity Rect rectangle0; 矩形矩形1;矩形矩形2;矩形矩形3; 矩形矩形4;私人 int X_POSITION; 私人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); 

这里发生的问题我不知道为什么所有矩形都会触发相同的活动以及如何解决它以便所有矩形触发不同的活动

    switch (event.getAction()) {

    case MotionEvent.ACTION_DOWN:


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

    }

返回真;

}

@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




}   


}

强文本

4

1 回答 1

0

首先确保 onTouchEvent 正常工作(实际上会像你一样返回 X 和 Y),然后

@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); 
        }

        ....

    } 
}
于 2012-05-09T12:27:02.377 回答