ViewUtil工具类
设置当前是否需要隐藏
设置View是否可用
扩大View的触摸和点击响应范围,最大不超过其父View范围
/******************************************
* 类描述:操作View的工具类
*
* @author: yangshaoning
******************************************/
public final class ViewUtil {
private ViewUtil() {
}
/**
* 设置当前是否需要隐藏
*
* @param view
* @param visibility
*/
public static void setViewVisibility(View view, int visibility) {
if (view == null) {
return;
}
if (view.getVisibility() == visibility) {
return;
}
view.setVisibility(visibility);
}
/**
* 设置View是否可用
*
* @param view
* @param isEnable
*/
public static void setEnabled(View view, boolean isEnable) {
if (view == null) {
return;
}
if (view.isEnabled() == isEnable) {
return;
}
view.setEnabled(isEnable);
}
/**
* 扩大View的触摸和点击响应范围,最大不超过其父View范围
*
* @param view
* @param top
* @param bottom
* @param left
* @param right
*/
public static void expandViewTouchDelegate(final View view, final int top,
final int bottom, final int left, final int right) {
((View) view.getParent()).post(new Runnable() {
@Override
public void run() {
Rect bounds = new Rect();
view.setEnabled(true);
view.getHitRect(bounds);
bounds.top -= top;
bounds.bottom += bottom;
bounds.left -= left;
bounds.right += right;
TouchDelegate touchDelegate = new TouchDelegate(bounds, view);
if (View.class.isInstance(view.getParent())) {
((View) view.getParent()).setTouchDelegate(touchDelegate);
}
}
});
}
}