Added tich delegation to the map button, to make them easier to click

Change-Id: Ia43fa2a13af68c5e81f6023a8948d4d851479f21
Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-03-09 10:48:05 -05:00
parent 9835514d29
commit cd1a00ac52
4 changed files with 113 additions and 38 deletions

View File

@@ -0,0 +1,82 @@
/**
* DelegatedImageButton.java
* @date Mar 9, 2012
* @author ricky barrette
* @author Twenty Codes, LLC
*/
package com.TwentyCodes.android.FindMyCarLib.UI;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.TouchDelegate;
import android.view.View;
import android.view.View.OnAttachStateChangeListener;
import android.widget.LinearLayout;
/**
* This button is a simple image button that extends it's touch hit rect
* @author ricky barrette
*/
public class DelegatedImageButton extends LinearLayout implements OnAttachStateChangeListener {
/**
* @param context
* @author ricky barrette
*/
public DelegatedImageButton(Context context) {
super(context);
this.addOnAttachStateChangeListener(this);
}
/**
* @param context
* @param attrs
* @author ricky barrette
*/
public DelegatedImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
this.addOnAttachStateChangeListener(this);
}
/**
* @param context
* @param attrs
* @param defStyle
* @author ricky barrette
*/
public DelegatedImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.addOnAttachStateChangeListener(this);
}
public void initDelegate(){
final View parent = (View) this.getParent();
parent.post(new Runnable() {
@Override
public void run() {
Rect bounds = new Rect();
DelegatedImageButton.this.getHitRect(bounds);
bounds.top -= 40;
bounds.left -= 40;
bounds.bottom += 40;
bounds.right += 40;
TouchDelegate touchDelegate = new TouchDelegate(bounds, DelegatedImageButton.this);
if (View.class.isInstance(DelegatedImageButton.this.getParent())) {
((View) DelegatedImageButton.this.getParent()).setTouchDelegate(touchDelegate);
}
}
});
}
@Override
public void onViewAttachedToWindow(View v) {
initDelegate();
}
@Override
public void onViewDetachedFromWindow(View v) {
// UNUSED
}
}