Slovenia
Slovenia

Reputation: 160

How to make picture in Canvas like button?

How to make picture in a Canvas onTouch, like a button. Just to be press and nothing else. I can insert picture in Canvas, now how can I make her onTouch? Can somebody pls give me easy exsample? Dont be mad, cuz this is probably stupid and easy question

Thank you

Upvotes: 0

Views: 195

Answers (2)

Hai Bo Wang
Hai Bo Wang

Reputation: 642

public boolean handleTouch(MotionEvent event)
    {
        float newX = event.getX();
        float newY = event.getY();
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:

                if (isInRect((int) newX, (int) newY))
                {


                    refreshView();

                }
                   //....
          }
 }

Upvotes: 1

blessanm86
blessanm86

Reputation: 31789

You cannot add an onClick or onTouch listener to something you've drawn with the canvas. These listeners can only be applied to views.

Another method will be to apply the onTouchListener to the view, and find the touch position to find whether the touch was on the pictures position.

Upvotes: 0

Related Questions