Skip to content

Instantly share code, notes, and snippets.

@whinc
Created July 31, 2015 09:39
Show Gist options
  • Select an option

  • Save whinc/db5f2fc649ffde51b161 to your computer and use it in GitHub Desktop.

Select an option

Save whinc/db5f2fc649ffde51b161 to your computer and use it in GitHub Desktop.
工具方法,扩展Android中View的可触摸区域
/**
* 扩展View的触摸区域
* @param view 需要扩展触摸区域的View
* @param left 左边扩展范围(单位px,下同)
* @param top 上边扩展范围
* @param right 右边扩展范围
* @param bottom 下边扩展范围
*/
public static void extendTouchArea(final View view, final int left, final int top, final int right, final int bottom) {
if (view == null || view.getParent() == null) {
return;
}
final View parent = (View) view.getParent();
parent.post(new Runnable() {
@Override
public void run() {
Rect touchArea = new Rect();
view.getHitRect(touchArea);
touchArea.left += left;
touchArea.top += top;
touchArea.right += right;
touchArea.bottom += bottom;
parent.setTouchDelegate(new TouchDelegate(touchArea, view));
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment