How to create Clickable Rectangles?

江枫思渺然 提交于 2019-12-13 04:42:37

问题


Im drawing an indoor map using DrawRect method. I want to identify each rectangle by the user's OnTouch Event.How can i make it happen?

I want to know if i can use the drawn Rectangles as independent objects Where i can identify them uniquely.

UPDATED

I tried this. But it gives me errors.How can i create a new intent withing the for loop?? :(

ArrayList<Rect> rectangles = new ArrayList<Rect>();
 {rectangles.add(new Rect(232,78,419,238));
 rectangles.add(new Rect(285,27,524,239));
 rectangles.add(new Rect(418,79,524,239));
 rectangles.add(new Rect(181,79,233,215));
 rectangles.add(new Rect(232,237,524,315));}

 String Selected_rect = null;


public boolean onTouchEvent(MotionEvent event) {
int touchX = (int) event.getX();
int touchY = (int) event.getY();
switch(event.getAction()){

case MotionEvent.ACTION_DOWN:
        System.out.println("Touching down!");
        for(int i =0; i< rectangles.size();i++){

                if(rectangles.get(i).contains(touchX,touchY)){
                    System.out.println("Touched Rectangle, start activity.");
                    rectangles.get(i).describeContents ();
                    Selected_rect = String.valueOf(rectangles.get(i));
                                       }
//                    Intent inte = new Intent("android.intent.action.DetectBlock");
//                    startActivity(inte);
            }

        break;
case MotionEvent.ACTION_UP:
        System.out.println("Touching up!");
        break;
case MotionEvent.ACTION_MOVE:
        System.out.println("Sliding your finger around on the screen.");
        break;
}
  return true;
}

My Intent method body

private void startActivity(Intent inte) {
    // TODO Auto-generated method stub
     startActivity(inte);
}

回答1:


The canvas will not remember about the shapes you draw on it. You have to keep track of them separatedly. You should fill a list (or some other data structure) with Rect objects ("model" objects), or other custom objects holding the coordinates, dimensions and whatever you need, of the rectangles, and check on touch if the coordinates of the touch are inside of any of these rectangles. E.G. method contains(int x, int y) of Android's Rect will help. In your draw method you then use also these objects.




回答2:


I realize this question was asked a while back but i noticed your updated question...

I'm guessing the problem your having is actually your trying to run "startActivity" from a View class.

Firstly if you make a new static void on your activity class like so

public static void Rectclicked(Context c) {
        Intent inte = new Intent(c, SecondActivity.class);
        c.startActivity(inte);
    }

then back on your View class, in your TouchEvent add this into the for loop if a rectangle has been clicked

MainActivity.Rectclicked(getContext());



回答3:


Im not sure if this has been answered but i am building something similar. For the construction of the rects i have just manually added each and then used an Rect.intersect() to see if the constructed rects cross the touchpoint. Thats a vague answer but i figure you figured it out, let me know if you didn't:D



来源:https://stackoverflow.com/questions/12026517/how-to-create-clickable-rectangles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!