Android expands view Click Range

Source: Internet
Author: User

The Android4.0 design provides for an effective touchable UI element standard of 48DP, converted to a physical size of about 9 mm. 7~10 mm, which is an area where the user's fingers can be accurately and comfortably touched.

As shown, your UI element may be less than 48DP, the icon will only have 32DP, the button only has 40DP, but their actual operational focus area should preferably reach 48DP size.

In order to get a good touch interaction for a small UI area, there are two situations that are encountered depending on the view's characteristics:

1. If ImageView, set its padding value, the touch area will be expanded outward;

2. If the button, set its padding value, can touch the area unchanged, inside the content display area inward compression;

Case 1 control, can be set directly its padding value to achieve the purpose, such as android:padding= "10DP"

The control of Case 2, can use Touchdelegate to dynamically modify its touch area to achieve the effect of enlarging the click Range.

[Java]View Plaincopy
  1. /**
  2. * Expand the touch and click Response range of the view to a maximum of no more than its parent view range
  3. *
  4. * @param view
  5. * @param top
  6. * @param Bottom
  7. * @param left
  8. * @param right
  9. */
  10. public static void Expandviewtouchdelegate (final View view, final int top,
  11. final int bottom, final int left, final int right) {
  12. (View) view.getparent ()). Post (new Runnable () {
  13. @Override
  14. public Void Run () {
  15. Rect bounds = new rect ();
  16. View.setenabled (true);
  17. View.gethitrect (bounds);
  18. Bounds.top-= top;
  19. Bounds.bottom + = bottom;
  20. Bounds.left-= left;
  21. Bounds.right + = right;
  22. Touchdelegate touchdelegate = new Touchdelegate (bounds, view);
  23. if (View. Class.isinstance (View.getparent ())) {
  24. (View) view.getparent ()). Settouchdelegate (Touchdelegate);
  25. }
  26. }
  27. });
  28. }


Two points to take note of this approach:

1. If the custom touch range of the view exceeds the size of the parent, the part that is exceeded is not valid.
2, a parent can only set a view of the touchdelegate, set multiple when only the last set to take effect.

If you need to restore the view's touch range:

[Java]View Plaincopy
  1. /**
  2. * Restore view's touch and click Response Range, minimum not less than the view itself range
  3. *
  4. * @param view
  5. */
  6. public static void Restoreviewtouchdelegate (final view view) {
  7. (View) view.getparent ()). Post (new Runnable () {
  8. @Override
  9. public Void Run () {
  10. Rect bounds = new rect ();
  11. Bounds.setempty ();
  12. Touchdelegate touchdelegate = new Touchdelegate (bounds, view);
  13. if (View. Class.isinstance (View.getparent ())) {
  14. (View) view.getparent ()). Settouchdelegate (Touchdelegate);
  15. }
  16. }
  17. });
  18. }


Using Touchdelegate to expand the touch response range of a view is a flexible approach that can sometimes be used in conjunction with setting up padding.

Update

======

In the late practical development, the reason for using post runnable to set the size of the delegate area is that if the view Division is drawn in the activity's OnCreate () or fragment Oncreateview (), At this point the UI interface has not started drawing and cannot get the correct coordinates;

If you apply this method to draw each Itemview in the ListView's GetView (), the settings for delegate are partially invalidated because the ListView drawing is special and may not be able to get the correct coordinates for a view that is not yet drawn. The solution is listed in the following reference readings.

Reference reading: The way to customize view, and some other cases of processing:

1. Android uses touchdelegate to increase the touch range of the view http://blog.csdn.net/sgwhp/article/details/10963383

2. "ListView Tips & Tricks #5: Enlarged touchable areas" http://cyrilmottier.com/2012/02/16/ listview-tips-tricks-5-enlarged-touchable-areas/

3. "Extend touchable areas #Android" Https://plus.google.com/u/0/+JulienDodokal/posts/8zoV3RQvReS

Android expands view Click Range

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.