calculate Tap Area for View - Android User Interface

Android examples for User Interface:View Size

Description

calculate Tap Area for View

Demo Code


//package com.java2s;
import android.graphics.Rect;

import android.view.View;

public class Main {
    public static Rect calculateTapArea(View v, float oldx, float oldy,
            float coefficient) {

        float x = oldy;
        float y = v.getHeight() - oldx;

        float focusAreaSize = 300;

        int areaSize = Float.valueOf(focusAreaSize * coefficient)
                .intValue();//from  www.  ja va  2 s  . co  m
        int centerX = (int) (x / v.getWidth() * 2000 - 1000);
        int centerY = (int) (y / v.getHeight() * 2000 - 1000);

        int left = clamp(centerX - areaSize / 2, -1000, 1000);
        int right = clamp(left + areaSize, -1000, 1000);
        int top = clamp(centerY - areaSize / 2, -1000, 1000);
        int bottom = clamp(top + areaSize, -1000, 1000);

        return new Rect(left, top, right, bottom);
    }

    private static int clamp(int x, int min, int max) {
        if (x > max) {
            return max;
        }
        if (x < min) {
            return min;
        }
        return x;
    }
}

Related Tutorials