increase Hit Rect By - Android User Interface

Android examples for User Interface:Key Event

Description

increase Hit Rect By

Demo Code


//package com.java2s;

import android.graphics.Rect;

import android.util.DisplayMetrics;
import android.view.TouchDelegate;
import android.view.View;

public class Main {

    public static void increaseHitRectBy(final int amount,
            final View delegate) {
        increaseHitRectBy(amount, amount, amount, amount, delegate);
    }//from  w w w  .java2  s  .c  o  m

    /**
     * @param top
     * @param left
     * @param bottom
     * @param right
     * @param delegate
     */
    public static void increaseHitRectBy(final int top, final int left,
            final int bottom, final int right, final View delegate) {
        final View parent = (View) delegate.getParent();
        if (parent != null && delegate.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 float densityDpi = delegate.getContext()
                            .getResources().getDisplayMetrics().densityDpi;
                    final Rect r = new Rect();
                    delegate.getHitRect(r);
                    r.top -= transformToDensityPixel(top, densityDpi);
                    r.left -= transformToDensityPixel(left, densityDpi);
                    r.bottom += transformToDensityPixel(bottom, densityDpi);
                    r.right += transformToDensityPixel(right, densityDpi);
                    parent.setTouchDelegate(new TouchDelegate(r, delegate));
                }
            });
        }
    }

    public static int transformToDensityPixel(int regularPixel,
            DisplayMetrics displayMetrics) {
        return transformToDensityPixel(regularPixel,
                displayMetrics.densityDpi);
    }

    public static int transformToDensityPixel(int regularPixel,
            float densityDpi) {
        return (int) (regularPixel * densityDpi);
    }
}

Related Tutorials