development

Android에서 TouchDelegate를 사용하여보기의 클릭 대상 크기를 늘리는 방법에 대한 예가 있습니까?

big-blog 2020. 10. 5. 08:13
반응형

Android에서 TouchDelegate를 사용하여보기의 클릭 대상 크기를 늘리는 방법에 대한 예가 있습니까?


내 이해는보기가 너무 작아서 쉽게 만질 수없는 경우 TouchDelegate를 사용하여 해당보기의 클릭 가능한 영역을 늘려야한다는 것입니다.

그러나 Google에서 사용 예를 검색하면 여러 사람이 질문을하지만 답변은 거의 없습니다.

클릭 가능한 영역을 모든 방향으로 4 픽셀 늘릴 수 있도록 뷰에 대한 터치 델리게이트를 설정하는 적절한 방법을 아는 사람이 있습니까?


Google의 친구에게 물어 보니 TouchDelegate를 사용하는 방법을 알아낼 수있었습니다. 우리가 생각 해낸 것은 다음과 같습니다.

final View parent = (View) delegate.getParent();
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();
        delegate.getHitRect(r);
        r.top -= 4;
        r.bottom += 4;
        parent.setTouchDelegate( new TouchDelegate( r , delegate));
    }
});

이 블로그 게시물 에서 주로 그림을 그리는 한 화면에서 여러보기 (확인란)를 사용하여이 작업을 수행 할 수있었습니다 . 기본적으로 emmby의 솔루션을 사용하여 각 버튼과 해당 부모에 개별적으로 적용합니다.

public static void expandTouchArea(final View bigView, final View smallView, final int extraPadding) {
    bigView.post(new Runnable() {
        @Override
        public void run() {
            Rect rect = new Rect();
            smallView.getHitRect(rect);
            rect.top -= extraPadding;
            rect.left -= extraPadding;
            rect.right += extraPadding;
            rect.bottom += extraPadding;
            bigView.setTouchDelegate(new TouchDelegate(rect, smallView));
        }
   });
}

제 경우에는 체크 박스가 위에 겹쳐진 이미지 뷰의 gridview가 있었고 다음과 같이 메서드를 호출했습니다.

CheckBox mCheckBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
final ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);

// Increase checkbox clickable area
expandTouchArea(imageView, mCheckBox, 100);

나를 위해 잘 작동합니다.


이 솔루션은 @BrendanWeinstein이 의견에 게시했습니다.

를 보내는 대신 (확장하는 경우)의 메서드를 TouchDelegate재정 할 수 있습니다 .getHitRect(Rect)View

public class MyView extends View {  //NOTE: any other View can be used here

    /* a lot of required methods */

    @Override
    public void getHitRect(Rect r) {
        super.getHitRect(r); //get hit Rect of current View

        if(r == null) {
            return;
        }

        /* Manipulate with rect as you wish */
        r.top -= 10;
    }
}

emmby의 approch는 나를 위해 작동하지 않았지만 약간의 변경 후에 작동했습니다.

private void initApplyButtonOnClick() {
    mApplyButton.setOnClickListener(onApplyClickListener);
    final View parent = (View)mApplyButton.getParent();
    parent.post(new Runnable() {

        @Override
        public void run() {
            final Rect hitRect = new Rect();
            parent.getHitRect(hitRect);
            hitRect.right = hitRect.right - hitRect.left;
            hitRect.bottom = hitRect.bottom - hitRect.top;
            hitRect.top = 0;
            hitRect.left = 0;
            parent.setTouchDelegate(new TouchDelegate(hitRect , mApplyButton));         
        }
    });
}

누군가의 시간을 절약 할 수 있습니다.


@Mason Lee의 의견에 따르면 이것은 내 문제를 해결했습니다. 내 프로젝트에는 상대적인 레이아웃과 하나의 버튼이 있습니다. 그래서 부모는-> 레이아웃이고 자식은-> 버튼입니다.

다음은 Google 링크 예제 Google 코드입니다.

그의 매우 귀중한 답변을 삭제하는 경우 여기에 그의 답변을 넣었습니다.

최근에 TouchDelegate를 사용하는 방법에 대해 질문을 받았습니다. 나는 이것에 대해 약간 녹슬었고 그것에 대한 좋은 문서를 찾을 수 없었습니다. 여기에 약간의 시행 착오를 거쳐 작성한 코드가 있습니다. touch_delegate_view는 id가 touch_delegate_root 인 간단한 RelativeLayout입니다. 레이아웃의 단일 자식 인 delegated_button 버튼으로 정의했습니다. 이 예에서는 버튼의 클릭 가능한 영역을 버튼 상단의 200 픽셀 위로 확장합니다.

public class TouchDelegateSample extends Activity {

  Button mButton;   
  @Override   
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.touch_delegate_view);
    mButton = (Button)findViewById(R.id.delegated_button);
    View parent = findViewById(R.id.touch_delegate_root);

    // post a runnable to the parent view's message queue so its run after
    // the view is drawn
    parent.post(new Runnable() {
      @Override
      public void run() {
        Rect delegateArea = new Rect();
        Button delegate = TouchDelegateSample.this.mButton;
        delegate.getHitRect(delegateArea);
        delegateArea.top -= 200;
        TouchDelegate expandedArea = new TouchDelegate(delegateArea, delegate);
        // give the delegate to an ancestor of the view we're delegating the
        // area to
        if (View.class.isInstance(delegate.getParent())) {
          ((View)delegate.getParent()).setTouchDelegate(expandedArea);
        }
      }
    });   
  } 
}

건배, Justin Android 팀 @ Google

나로부터 몇 마디 : 왼쪽으로 확장하려면 마이너스로 값을 제공하고 객체의 오른쪽으로 확장하려면 플러스로 값을 제공합니다. 이것은 상단과 하단에서 동일하게 작동합니다.


특정 구성 요소 (Button)에 Padding을 제공하는 것이 더 나은 아이디어가 아닙니까?


파티에 조금 늦었지만 많은 조사 끝에 지금 다음을 사용하고 있습니다.

/**
 * Expand the given child View's touchable area by the given padding, by
 * setting a TouchDelegate on the given ancestor View whenever its layout
 * changes.
 */*emphasized text*
public static void expandTouchArea(final View ancestorView,
    final View childView, final Rect padding) {

    ancestorView.getViewTreeObserver().addOnGlobalLayoutListener(
        new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                TouchDelegate delegate = null;

                if (childView.isShown()) {
                    // Get hitRect in parent's coordinates
                    Rect hitRect = new Rect();
                    childView.getHitRect(hitRect);

                    // Translate to ancestor's coordinates
                    int ancestorLoc[] = new int[2];
                    ancestorView.getLocationInWindow(ancestorLoc);

                    int parentLoc[] = new int[2];
                    ((View)childView.getParent()).getLocationInWindow(
                        parentLoc);

                    int xOffset = parentLoc[0] - ancestorLoc[0];
                    hitRect.left += xOffset;
                    hitRect.right += xOffset;

                    int yOffset = parentLoc[1] - ancestorLoc[1];
                    hitRect.top += yOffset;
                    hitRect.bottom += yOffset;

                    // Add padding
                    hitRect.top -= padding.top;
                    hitRect.bottom += padding.bottom;
                    hitRect.left -= padding.left;
                    hitRect.right += padding.right;

                    delegate = new TouchDelegate(hitRect, childView);
                }

                ancestorView.setTouchDelegate(delegate);
            }
        });
}

이것은 TouchDelegate가 상위 뷰뿐만 아니라 모든 조상 뷰에서 설정 될 수 있도록 허용하기 때문에 허용 된 솔루션보다 낫습니다.

허용 된 솔루션과 달리, 상위 뷰의 레이아웃이 변경 될 때마다 TouchDelegate도 업데이트합니다.


프로그래밍 방식으로 수행하지 않으려면 이미지 주변에 투명한 영역을 만드십시오. 이미지를 단추 (보기)의 배경으로 사용하는 경우.

회색 영역은 투명하여 터치 영역을 늘릴 수 있습니다.

enter image description here


In most cases, you can wrap the view that requires a larger touch area in another headless view (artificial transparent view) and add padding/margin to the wrapper view and attach the click/touch even to the wrapper view instead of the original view that has to have a larger touch area.


To expand the touch area generically with pretty few restrictions use the following code.

It lets you expand the touch area of the given view within the given ancestor view by the given expansion in pixels. You can choose any ancestor as long as the given view is in the ancestors layout tree.

    public static void expandTouchArea(final View view, final ViewGroup ancestor, final int expansion) {
    ancestor.post(new Runnable() {
        public void run() {
            Rect bounds = getRelativeBounds(view, ancestor);
            Rect expandedBounds = expand(bounds, expansion);
            // LOG.debug("Expanding touch area of {} within {} from {} by {}px to {}", view, ancestor, bounds, expansion, expandedBounds);
            ancestor.setTouchDelegate(new TouchDelegate(expandedBounds, view));
        }

        private Rect getRelativeBounds(View view, ViewGroup ancestor) {
            Point relativeLocation = getRelativeLocation(view, ancestor);
            return new Rect(relativeLocation.x, relativeLocation.y,
                            relativeLocation.x + view.getWidth(),
                            relativeLocation.y + view.getHeight());
        }

        private Point getRelativeLocation(View view, ViewGroup ancestor) {
            Point absoluteAncestorLocation = getAbsoluteLocation(ancestor);
            Point absoluteViewLocation = getAbsoluteLocation(view);
            return new Point(absoluteViewLocation.x - absoluteAncestorLocation.x,
                             absoluteViewLocation.y - absoluteAncestorLocation.y);
        }

        private Point getAbsoluteLocation(View view) {
            int[] absoluteLocation = new int[2];
            view.getLocationOnScreen(absoluteLocation);
            return new Point(absoluteLocation[0], absoluteLocation[1]);
        }

        private Rect expand(Rect rect, int by) {
            Rect expandedRect = new Rect(rect);
            expandedRect.left -= by;
            expandedRect.top -= by;
            expandedRect.right += by;
            expandedRect.bottom += by;
            return expandedRect;
        }
    });
}

Restrictions that apply:

  • The touch area can not exceed the bounds of the view's ancestor since the ancestor must be able to catch the touch event in order to forward it to the view.
  • Only one TouchDelegate can be set to a ViewGroup. If you want to work with multiple touch delegates, choose different ancestors or use a composing touch delegate like explained in How To Use Multiple TouchDelegate.

Because I didn't like the idea of waiting for the layout pass just to get the new size of the TouchDelegate's rectangle, I went for a different solution:

public class TouchSizeIncreaser extends FrameLayout {
    public TouchSizeIncreaser(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final View child = getChildAt(0);
        if(child != null) {
            child.onTouchEvent(event);
        }
        return true;
    }
}

And then, in a layout:

<ch.tutti.ui.util.TouchSizeIncreaser
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>
</ch.tutti.ui.util.TouchSizeIncreaser>

The idea is that TouchSizeIncreaser FrameLayout will wrap the Spinner (could be any child View) and forward all the touch events captured in it's hit rect to the child View. It works for clicks, the spinner opens even if clicked outside its bounds, not sure what are the implications for other more complex cases.

참고URL : https://stackoverflow.com/questions/1343222/is-there-an-example-of-how-to-use-a-touchdelegate-in-android-to-increase-the-siz

반응형