Android Development #10: ImageView Touch Effect

Create a utility class called TouchImageView.java

    public class TouchImageView implements View.OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch(event.getAction())  {
                case MotionEvent.ACTION_DOWN: {
                    ImageView view = (ImageView) v;
                    // argb
                    view.getDrawable().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP);
                    view.invalidate();
                    break;
                }
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL: {
                    ImageView view = (ImageView) v;
                    view.getDrawable().clearColorFilter();
                    view.invalidate();
                    break;
                }
            }
            return true;
        }
    }

0x77000000 is a ARGB hex code. Change the last 6 numbers to make different colors. First two numbers are the opacity.

Set it in any ImageView. This will apply a transparent black color effect when there is a touch event on the image. Perfect for GridView layout.

    ImageView mImageView = (ImageView) itemView.findViewById(R.id.img);
    mImageView.setOnTouchListener(new TouchImageView());

Question:
How do you make a ripple effect on touch?


Source

Show Comments

Get the latest posts delivered right to your inbox.