-1

I would like to make an ImageView ? AND WHEN i touch it it change background, when i drag finger far of it it become normal i don't know how to do, thanks

 imgButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            cmp++;
            txt2.setText(" " + cmp);
            //Quant on touche l'image :
            if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
                imgButton.getDrawable().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP);
            }
            // si Je bouge le dois sur l'image et ....
            if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
                //... et que le doit quitte l'image
                if (event.getX() < 0 || event.getX() > v.getWidth() || event.getY() < 0 || event.getY() > v.getHeight()) {
                    imgButton.getDrawable().setColorFilter(0x00000001, PorterDuff.Mode.SRC_ATOP);
                }
                if (event.getX() > 0 && event.getX() < v.getWidth() && event.getY() > 0 && event.getY() < v.getHeight()) {
                    // ... et que le doit revient vers L'image
                    imgButton.getDrawable().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP);
                }
            }
            // Si je lève le doits et...
            if (event.getActionMasked() == MotionEvent.ACTION_UP) {
                // .... sur l'image :
                if (event.getX() > 0 && event.getX() < v.getWidth() && event.getY() > 0 && event.getY() < v.getHeight()) {
                    //Finger back to the view
                    imgButton.getDrawable().setColorFilter(0x00000001, PorterDuff.Mode.SRC_ATOP);
                    Intent i = new Intent(MainActivity.this, game.class);
                    startActivity(i);
                }
            }
            return true;
        }
    });
Otmàane Fikri
  • 109
  • 3
  • 14
  • 1
    Unclear. Why don't you use the usual click listener, instead? – Phantômaxx Dec 19 '15 at 15:47
  • that's what i want to do : i have Image View and when i touch it, it takes effect like changing ColorFilter (to inform user that the button is touched) and when i drag finger far of the image it becomes normal (to inform user that the image is not being touched ) and again when i drag finger near to the imgView it take again effects, – Otmàane Fikri Dec 19 '15 at 16:25
  • It seems to me that you indeed want a state list selector, on a normal click. No "drag, close, far, touch, non touch, ...". Just clicked or non clicked. With visual feedback. – Phantômaxx Dec 19 '15 at 16:53
  • I think that is it. i want to make the imageView behavior with pressing and touching like in buttons of application we see on playsore, How to do that, Sorry for bad english :D – Otmàane Fikri Dec 19 '15 at 17:00
  • The answer from @SachinS is nearly good. Just that I would have used separate file for the items and used a default case for the not pressed state, rather than using pressed="false". Reference: http://developer.android.com/intl/es/guide/topics/resources/drawable-resource.html#StateList – Phantômaxx Dec 19 '15 at 17:06

2 Answers2

0

Create an xml file under drawable folder namedchange.xml of your application and put below codes.Modify it with your color codes.

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true">
      <color android:color="#00FF00"/>
   </item>
   <item android:state_pressed="false">
      <color android:color="#00FFFF"/>
   </item>
</selector>

and add the below attribute to your ImageView of your layout

android:background="@drawable/change"
SachinS
  • 2,223
  • 1
  • 15
  • 25
  • Done , But now what should i do in the MainActivity.java , it is the first time i use this method so what should i do in code ? – Otmàane Fikri Dec 19 '15 at 17:23
  • remove the setOnTouchListener,no need for that – SachinS Dec 19 '15 at 17:33
  • what about the answer? – SachinS Dec 20 '15 at 12:58
  • When i click on the image nothing changes, i created change.xml and write the code u have proposed, and added attribute < android:background="@drawable/change" > in imageView in content_main.xml , and in java code i just let two function super.oncreate and setcontentview(content_main). But Finally i got what i wanted by just Code Java , see the question , i have modified the code there, but i'm still asking why your Answer does not work for me ? – Otmàane Fikri Dec 21 '15 at 00:37
  • I had to add Attribute android:clickable="true" that what was missed Thank you all ;) – Otmàane Fikri Dec 21 '15 at 04:01
0

You should use getActionMasked() rather than getAction().

getAction() returns the action with pointer information while getActionMasked() only returns the simple action, which makes the action comparison work correctly.

Then catch the right action in the onTouch method.

public boolean onTouch(View v, MotionEvent event) {
    if(event.getActionMasked()==MotionEvent.ACTION_DOWN) {
        //Finger down
        //Change the color
    }
    if(event.getActionMasked()==MotionEvent.ACTION_MOVE) {
        if(event.getX()<0 || event.getX()>v.getWidth() || event.getY()<0 || event.getY()>v.getHeight()){
            //Finger exit the view
            //Change the color back
        }
    }
    if(event.getActionMasked()==MotionEvent.ACTION_UP) {
        //Finger up
        //Change the color back
    }
    return true;
}

The returned value should be true in most cases. See this article.

Community
  • 1
  • 1
Jeffrey Chen
  • 1,777
  • 1
  • 18
  • 29
  • Thank you that was helpful ;) But what if i'm still touching and go back to image view and make action Up on it, logically the event Onclick must be triggered how to do this – Otmàane Fikri Dec 19 '15 at 22:14
  • In action up, add an if statement to check whether finger is in the view (just like I do in action move). Then use View's callOnClick() method to trigger onClick event manually. – Jeffrey Chen Dec 20 '15 at 04:23