下面列出了怎么用android.view.TouchDelegate的API类实例代码及写法,或者点击链接到github查看源代码。
private void expandTapArea(final View container, final View child) {
final int padding = getResources().getDimensionPixelSize(R.dimen.contact_selection_actions_tap_area);
container.post(new Runnable() {
@Override
public void run() {
Rect rect = new Rect();
child.getHitRect(rect);
rect.top -= padding;
rect.left -= padding;
rect.right += padding;
rect.bottom += padding;
container.setTouchDelegate(new TouchDelegate(rect, child));
}
});
}
private void expandTapArea(final View container, final View child) {
final int padding = getResources().getDimensionPixelSize(R.dimen.contact_selection_actions_tap_area);
container.post(new Runnable() {
@Override
public void run() {
Rect rect = new Rect();
child.getHitRect(rect);
rect.top -= padding;
rect.left -= padding;
rect.right += padding;
rect.bottom += padding;
container.setTouchDelegate(new TouchDelegate(rect, child));
}
});
}
/**
* Adds a touch delegate to the views parent if the parent is of type View.
* This method is posted back to the parent so that the hit area is applied *after* the parent lays out its children
*
* This is useful if you need to expand the hit area (eg: for onClick's to trigger) but don't want to, or can't, increase
* the size/padding of the view.
*
* @param toThisView the View you want to increase the touch (hit) area for
* @param leftPX the additional amount (negative increases, positive decreases) of the hit area
* @param topPX the additional amount (negative increases, positive decreases) of the hit area
* @param rightPX the additional amount (positive increases, negative decreases) of the hit area
* @param bottomPX the additional amount (positive increases, negative decreases) of the hit area
*/
public static void increaseTouchArea(
final View toThisView,
final int leftPX,
final int topPX,
final int rightPX,
final int bottomPX
) {
if (toThisView == null || !View.class.isInstance(toThisView.getParent())) {
return;
}
final View statementParent = (View) toThisView.getParent();
statementParent.post(new Runnable() {
@Override public void run() {
Rect delegateArea = new Rect();
toThisView.getHitRect(delegateArea);
delegateArea.left += leftPX;
delegateArea.top += topPX;
delegateArea.right += rightPX;
delegateArea.bottom += bottomPX;
statementParent.setTouchDelegate(new TouchDelegate(delegateArea, toThisView));
}
});
}
/**
* 还原View的触摸和点击响应范围,最小不小于View自身范围
*/
public static void restoreViewTouchDelegate(final View view) {
if (view == null) {
return;
}
if (view.getParent() != null) {
((View) view.getParent()).postDelayed(new Runnable() {
@Override
public void run() {
Rect bounds = new Rect();
bounds.setEmpty();
TouchDelegate touchDelegate = new TouchDelegate(bounds, view);
if (View.class.isInstance(view.getParent())) {
((View) view.getParent()).setTouchDelegate(touchDelegate);
}
}
}, 300);
}
}
/**
* 扩展点击区域的范围
*
* @param view 需要扩展的元素,此元素必需要有父级元素
* @param expendSize 需要扩展的尺寸(以sp为单位的)
*/
public static void expendTouchArea(final View view, final int expendSize) {
if (view != null) {
final View parentView = (View) view.getParent();
parentView.post(new Runnable() {
@Override
public void run() {
Rect rect = new Rect();
view.getHitRect(rect); // 如果太早执行本函数,会获取rect失败,因为此时UI界面尚未开始绘制,无法获得正确的坐标
rect.left -= expendSize;
rect.top -= expendSize;
rect.right += expendSize;
rect.bottom += expendSize;
parentView.setTouchDelegate(new TouchDelegate(rect, view));
}
});
}
}
/**
* 扩展点击区域的范围
*
* @param view 需要扩展的元素,此元素必需要有父级元素
* @param expendSize 需要扩展的尺寸(以sp为单位的)
*/
public static void expendTouchArea(final View view, final int expendSize) {
if (view != null) {
final View parentView = (View) view.getParent();
parentView.post(new Runnable() {
@Override
public void run() {
Rect rect = new Rect();
view.getHitRect(rect); //如果太早执行本函数,会获取rect失败,因为此时UI界面尚未开始绘制,无法获得正确的坐标
rect.left -= expendSize;
rect.top -= expendSize;
rect.right += expendSize;
rect.bottom += expendSize;
parentView.setTouchDelegate(new TouchDelegate(rect, view));
}
});
}
}
private void doSetTouchDelegate() {
final TextView middleBtn = mMiddleButton;
post(new Runnable() {
@Override
public void run() {
Rect rect = new Rect();
rect.left = (middleBtn.getWidth() / 4);
rect.right = (3 * middleBtn.getWidth() / 4);
rect.top = 0;
rect.bottom = middleBtn.getHeight();
middleBtn.setTouchDelegate(new TouchDelegate(rect, /*
* TopBarView.
* this
*/
mMiddleSub));
}
});
}
/**
* 扩展点击区域的范围
*
* @param view 需要扩展的元素,此元素必需要有父级元素
* @param expendSize 需要扩展的尺寸(以sp为单位的)
*/
public static void expendTouchArea(final View view, final int expendSize) {
if (view != null) {
final View parentView = (View) view.getParent();
parentView.post(new Runnable() {
@Override
public void run() {
Rect rect = new Rect();
view.getHitRect(rect); //如果太早执行本函数,会获取rect失败,因为此时UI界面尚未开始绘制,无法获得正确的坐标
rect.left -= expendSize;
rect.top -= expendSize;
rect.right += expendSize;
rect.bottom += expendSize;
parentView.setTouchDelegate(new TouchDelegate(rect, view));
}
});
}
}
public static void expandClickArea(@NonNull final View view,
final int expandSizeTop,
final int expandSizeLeft,
final int expandSizeRight,
final int expandSizeBottom) {
final View parentView = (View) view.getParent();
if (parentView == null) {
Log.e("ClickUtils", "expandClickArea must have parent view.");
return;
}
parentView.post(new Runnable() {
@Override
public void run() {
final Rect rect = new Rect();
view.getHitRect(rect);
rect.top -= expandSizeTop;
rect.bottom += expandSizeBottom;
rect.left -= expandSizeLeft;
rect.right += expandSizeRight;
parentView.setTouchDelegate(new TouchDelegate(rect, view));
}
});
}
/**
* General-purpose function to increase the TouchDelegate size up to the minimum size needed for
* accessibility, and centered around the existing center of the view.
*
* @param viewToDelegate The view whose touchable area needs to be increased by setting a
* TouchDelegate on its parent with a larger rect.
*/
public static void setTouchDelegateToMinAccessibleSize(final View viewToDelegate) {
viewToDelegate.post(
new Runnable() {
@Override
public void run() {
if (viewToDelegate == null) {
return;
}
int a11ySize =
viewToDelegate
.getContext()
.getResources()
.getDimensionPixelSize(R.dimen.accessibility_touch_target_min_size);
Rect rect = new Rect();
viewToDelegate.getHitRect(rect);
resizeRect(a11ySize, rect);
((View) viewToDelegate.getParent())
.setTouchDelegate(new TouchDelegate(rect, viewToDelegate));
}
});
}
private void expandTapArea(final View container, final View child) {
final int padding = getResources().getDimensionPixelSize(R.dimen.contact_selection_actions_tap_area);
container.post(new Runnable() {
@Override
public void run() {
Rect rect = new Rect();
child.getHitRect(rect);
rect.top -= padding;
rect.left -= padding;
rect.right += padding;
rect.bottom += padding;
container.setTouchDelegate(new TouchDelegate(rect, child));
}
});
}
/**
* 扩大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);
}
}
});
}
private void expandTapArea(final View container, final View child, final int padding) {
container.post(new Runnable() {
@Override
public void run() {
Rect rect = new Rect();
child.getHitRect(rect);
rect.top -= padding;
rect.left -= padding;
rect.right += padding;
rect.bottom += padding;
container.setTouchDelegate(new TouchDelegate(rect, child));
}
});
}
/**
* Increases the hit rect of a view. This should be used when an icon is small and cannot be easily tapped on.
* Source: http://stackoverflow.com/a/1343796/5210
*
* @param top The amount of dp's to be added to the top for hit purposes.
* @param left The amount of dp's to be added to the left for hit purposes.
* @param bottom The amount of dp's to be added to the bottom for hit purposes.
* @param right The amount of dp's to be added to the right for hit purposes.
*/
public T increaseHitRect(final int top, final int left, final int bottom, final int right) {
final View parent = (View) view.getParent();
if (parent != null && view.getContext() != null) {
parent.post(new Runnable() {
// Post in the parent's message queue to make sure the parent
// lays out its children before we call getHitRect()
public void run() {
final Rect r = new Rect();
view.getHitRect(r);
r.top -= dip2pixel(top);
r.left -= dip2pixel(left);
r.bottom += dip2pixel(bottom);
r.right += dip2pixel(right);
parent.setTouchDelegate(new TouchDelegate(r, view));
}
});
}
return self();
}
@Override
protected void onBindView(View view)
{
super.onBindView(view);
mSpinner = (Spinner) view.findViewById(R.id.spinnerWidget);
final ViewParent parent = mSpinner.getParent();
if(parent instanceof View)
{
final Rect rect = new Rect(0, 0, ((View) parent).getWidth(),
((View) parent).getHeight());
((View) parent).setTouchDelegate(new TouchDelegate(rect, (View) parent));
}
updateSpinner();
mSpinner.setOnItemSelectedListener(this);
}
/**
* 扩大View的触摸和点击响应范围,最大不超过其父View范围(ABSListView无效)
* 延迟300毫秒执行,使该方法可以在onCreate里面使用
*/
public static void expandViewTouchDelegate(final View view, final int top, final int bottom, final int left, final int right) {
if (view == null) {
return;
}
if (view.getParent() != null) {
((View) view.getParent()).postDelayed(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;
Log.d("rect", "rect - top" + bounds.top + " - right" + bounds.right);
TouchDelegate touchDelegate = new TouchDelegate(bounds, view);
if (View.class.isInstance(view.getParent())) {
((View) view.getParent()).setTouchDelegate(touchDelegate);
}
}
}, 300);
}
}
public static void expandTouchArea(final View parent, final View view, final int dp) {
float scale = parent.getContext().getResources().getDisplayMetrics().density;
int padding = (int) (scale * dp);
parent.post(() -> {
Rect rect = new Rect();
view.getHitRect(rect);
rect.top -= padding;
rect.left -= padding;
rect.right += padding;
rect.bottom += padding;
parent.setTouchDelegate(new TouchDelegate(rect, view));
});
}
private void extendTouchAreaToMatchParent(int id)
{
final View button = findViewById(id);
final View parent = (View) button.getParent();
parent.post(new Runnable()
{
@Override
public void run()
{
Rect parentRect = new Rect();
parent.getHitRect(parentRect);
Rect buttonRect = new Rect();
button.getHitRect(buttonRect);
int widthDiff = parentRect.width() - buttonRect.width();
int heightDiff = parentRect.height() - buttonRect.height();
buttonRect.left -= widthDiff / 2;
buttonRect.right += widthDiff / 2;
buttonRect.top -= heightDiff / 2;
buttonRect.bottom += heightDiff / 2;
parent.setTouchDelegate(new TouchDelegate(buttonRect, button));
}
});
}
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
if (!mEnabled) return false;
TouchDelegate delegate = null;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
for (int i = 0; i < mTouchDelegates.size(); i++) {
TouchDelegate touchDelegate = mTouchDelegates.get(i);
if (touchDelegate.onTouchEvent(event)) {
mCurrentTouchDelegate = touchDelegate;
return true;
}
}
break;
case MotionEvent.ACTION_MOVE:
delegate = mCurrentTouchDelegate;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
delegate = mCurrentTouchDelegate;
mCurrentTouchDelegate = null;
break;
}
return delegate != null && delegate.onTouchEvent(event);
}
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
if (!mEnabled) return false;
TouchDelegate delegate = null;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
for (int i = 0; i < mTouchDelegates.size(); i++) {
TouchDelegate touchDelegate = mTouchDelegates.get(i);
if (touchDelegate.onTouchEvent(event)) {
mCurrentTouchDelegate = touchDelegate;
return true;
}
}
break;
case MotionEvent.ACTION_MOVE:
delegate = mCurrentTouchDelegate;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
delegate = mCurrentTouchDelegate;
mCurrentTouchDelegate = null;
break;
default: // Do Nothing
break;
}
return delegate != null && delegate.onTouchEvent(event);
}
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
if (!mEnabled) return false;
TouchDelegate delegate = null;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
for (int i = 0; i < mTouchDelegates.size(); i++) {
TouchDelegate touchDelegate = mTouchDelegates.get(i);
if (touchDelegate.onTouchEvent(event)) {
mCurrentTouchDelegate = touchDelegate;
return true;
}
}
break;
case MotionEvent.ACTION_MOVE:
delegate = mCurrentTouchDelegate;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
delegate = mCurrentTouchDelegate;
mCurrentTouchDelegate = null;
break;
}
return delegate != null && delegate.onTouchEvent(event);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
delegateContainer = (TouchDelegateViewGroup) view.findViewById(R.id.delegate_container);
rtn = (RadioButton) view.findViewById(R.id.rtn);
Rect rect = new Rect(500, 500, 800, 800);
delegateContainer.setDelegateAreaColor(Color.RED, rect);
delegateContainer.setTouchDelegate(new TouchDelegate(rect, rtn));
return view;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Go through the list and see if any of the delegates could claim this event.
// Note: Assumes non-overlapping touchDelegates.
boolean result = false;
// Check against all the touchDelegates in the list -- this could be an
// ACTION_MOVE or ACTION_UP that impacts a view that isn't at the (x,y) of an event.
for (TouchDelegate touchDelegate : delegateList) {
result = touchDelegate.onTouchEvent(event) || result;
}
return result;
}
@Nullable
public TouchDelegate makeTouchDelegate() {
Resources resources = getResources();
Rect delegateArea = new Rect();
getHitRect(delegateArea);
delegateArea.left += resources.getDimensionPixelSize(R.dimen.toggle_btn_delegate_add);
delegateArea.right += resources.getDimensionPixelOffset(R.dimen.toggle_btn_delegate_add);
return new TouchDelegate(delegateArea, this);
}
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
if (!mEnabled) return false;
TouchDelegate delegate = null;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
for (int i = 0; i < mTouchDelegates.size(); i++) {
TouchDelegate touchDelegate = mTouchDelegates.get(i);
if (touchDelegate.onTouchEvent(event)) {
mCurrentTouchDelegate = touchDelegate;
return true;
}
}
break;
case MotionEvent.ACTION_MOVE:
delegate = mCurrentTouchDelegate;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
delegate = mCurrentTouchDelegate;
mCurrentTouchDelegate = null;
break;
}
return delegate != null && delegate.onTouchEvent(event);
}
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
if (!mEnabled) return false;
TouchDelegate delegate = null;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
for (int i = 0; i < mTouchDelegates.size(); i++) {
TouchDelegate touchDelegate = mTouchDelegates.get(i);
if (touchDelegate.onTouchEvent(event)) {
mCurrentTouchDelegate = touchDelegate;
return true;
}
}
break;
case MotionEvent.ACTION_MOVE:
delegate = mCurrentTouchDelegate;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
delegate = mCurrentTouchDelegate;
mCurrentTouchDelegate = null;
break;
}
return delegate != null && delegate.onTouchEvent(event);
}
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
if (!mEnabled) return false;
TouchDelegate delegate = null;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
for (int i = 0; i < mTouchDelegates.size(); i++) {
TouchDelegate touchDelegate = mTouchDelegates.get(i);
if (touchDelegate.onTouchEvent(event)) {
mCurrentTouchDelegate = touchDelegate;
return true;
}
}
break;
case MotionEvent.ACTION_MOVE:
delegate = mCurrentTouchDelegate;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
delegate = mCurrentTouchDelegate;
mCurrentTouchDelegate = null;
break;
}
return delegate != null && delegate.onTouchEvent(event);
}
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
if (!mEnabled) return false;
TouchDelegate delegate = null;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
for (int i = 0; i < mTouchDelegates.size(); i++) {
TouchDelegate touchDelegate = mTouchDelegates.get(i);
if (touchDelegate.onTouchEvent(event)) {
mCurrentTouchDelegate = touchDelegate;
return true;
}
}
break;
case MotionEvent.ACTION_MOVE:
delegate = mCurrentTouchDelegate;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
delegate = mCurrentTouchDelegate;
mCurrentTouchDelegate = null;
break;
}
return delegate != null && delegate.onTouchEvent(event);
}
private void a(View view, View view1)
{
int i1 = (int)(30F * ChartUtil.getDensity());
Rect rect = new Rect();
view1.getHitRect(rect);
rect.left = rect.left - i1;
rect.top = rect.top - i1;
rect.right = i1 + rect.right;
rect.bottom = i1 + rect.bottom;
view.setTouchDelegate(new TouchDelegate(rect, view1));
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
ViewParent vp = getParent();
if (vp instanceof ViewGroup && getVisibility() == View.VISIBLE) {
ViewGroup vg = (ViewGroup) vp;
Rect bounds = new Rect(vg.getWidth() - w - 2 * mTouchAddition, 0, vg.getWidth(), h);
TouchDelegate delegate = new TouchDelegate(bounds, this);
vg.setTouchDelegate(delegate);
}
}