简体   繁体   English

谁能告诉我为什么所有Rect都会触发相同的活动

[英]Can Anyone Tell me Why All Rects fires to same Activity

* MyImage class Extends View And Implements ontouchListener * enter code here * MyImage类扩展视图并实现ontouchListener * enter code here

public class MyImage extends View implements OnTouchListener { 公共类MyImage扩展了View的实现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; 我有四个Rectangle,并且所有Rectangle都用于移动到diff diff Activity Rectrectangle0; Rect rectangle1; 矩形矩形1; Rect rectangle2; 矩形矩形; Rect rectangle3; 矩形4; Rect rectangle4; 矩形4; private int X_POSITION; private int X_POSITION; private int Y_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; 返回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 首先,请确保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); 
        }

        ....

    } 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谁能告诉我为什么这个计算为0? - Can anyone tell me why this is evaluating to 0? 谁能告诉我为什么在我的 evaulateExpression 中使用 pop() 不起作用? - Can anyone tell me why the use of pop() in my evaulateExpression is not working? 谁能告诉我为什么我的 IFile 总是返回 null? - Can Anyone tell me why my IFile always returns null? 谁能告诉我为什么我的旧列表具有仅在新列表中应该存在的所有值? - Can anyone tell me why my old list has all the values which should be there only in the new list.? 谁能告诉我这段代码是如何工作的? - Can anyone tell me how is this code working? 谁能告诉我为什么我不能在此AES实现中使用填充? - Can anyone tell me why I can't use Padding with this AES implementation? 谁能告诉我如何在 java 中同时删除两个元素? - Can Anyone tell me How to delete two elements at same time in java? 谁能告诉我如何使用Selenium Webdiver在Facebook中打印所有朋友列表? - Can anyone tell me how to print all the friend list in facebook using selenium webdiver? 谁能告诉我为什么会这样“ org.hibernate.exception.GenericJDBCException:无法执行查询”? - Can anyone tell me why this is happening “org.hibernate.exception.GenericJDBCException: could not execute query”? 谁能告诉我为什么在第一个方法调用之后这些方法没有运行? -Java - Can anyone tell me why these methods are not running after the first method call? - Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM