Java tutorial
//package com.java2s; /* * Copyright 2014 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.graphics.Rect; import android.graphics.RectF; public class Main { private static Rect calculateTapArea(float x, float y, int width, int height, float coefficient) { float focusAreaSize = 200; int areaSize = Float.valueOf(focusAreaSize * coefficient).intValue(); int centerX = (int) ((x / width) * 2000 - 1000); int centerY = (int) ((y / height) * 2000 - 1000); int left = clamp(centerX - (areaSize / 2), -1000, 1000); int top = clamp(centerY - (areaSize / 2), -1000, 1000); RectF rectF = new RectF(left, top, left + areaSize, top + areaSize); return new Rect(Math.round(rectF.left), Math.round(rectF.top), Math.round(rectF.right), Math.round(rectF.bottom)); } private static int clamp(int x, int min, int max) { if (x > max) { return max; } if (x < min) { return min; } return x; } }