add View Click Effect - Android User Interface

Android examples for User Interface:View Click

Description

add View Click Effect

Demo Code


//package com.java2s;

import android.graphics.PorterDuff;
import android.view.MotionEvent;
import android.view.View;

public class Main {
    public static void addClickEffect(View view) {
        view.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    v.getBackground().setColorFilter(0x88FFFFFF,
                            PorterDuff.Mode.SRC_ATOP);
                    v.invalidate();/*  w  ww  .  j  av  a 2s. c o m*/
                    return true;
                }
                case MotionEvent.ACTION_UP: {
                    v.getBackground().clearColorFilter();
                    v.invalidate();
                    v.performClick();
                    return false;
                }
                }
                return false;
            }
        });
    }
}

Related Tutorials