Created
March 9, 2017 14:48
-
-
Save pcevikogullari/e6c1a9767b68f9ce8268356e411c5000 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Pamir on 09/03/2017. | |
*/ | |
public class MyLinearLayout extends LinearLayout { | |
private final float SCALE_PRESSED = 0.9f; | |
private final float SCALE_NORMAL = 1.0f; | |
private OnMyClickListener myOnClickListener; | |
public MyLinearLayout(Context context) { | |
super(context); | |
init(); | |
} | |
public MyLinearLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
private void init() { | |
setFocusable(true); | |
setFocusableInTouchMode(true); // Needed to call onFocusChanged() | |
setBackgroundColor(Color.BLACK); | |
setClickable(true); | |
} | |
public void setOnMyClickListener(OnMyClickListener myOnClickListener) { | |
this.myOnClickListener = myOnClickListener; | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
if (event.getAction() == MotionEvent.ACTION_DOWN) { | |
setScale(SCALE_PRESSED); | |
} else if (event.getAction() == MotionEvent.ACTION_UP) { | |
setScale(SCALE_NORMAL); | |
} | |
gestureDetector.onTouchEvent(event); | |
return super.onTouchEvent(event); | |
} | |
public void setScale(float scale) { | |
setScaleX(scale); | |
setScaleY(scale); | |
} | |
private GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() { | |
@Override | |
public boolean onSingleTapConfirmed(MotionEvent e) { | |
if (myOnClickListener != null) { | |
myOnClickListener.onClick(); | |
} | |
Log.v("MyButton", "TapConfirmed"); | |
return super.onSingleTapConfirmed(e); | |
} | |
}; | |
private GestureDetector gestureDetector = new GestureDetector(getContext(), gestureListener); | |
public interface OnMyClickListener { | |
void onClick(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Scales down when takes focus, turn back to normal state when released.