Java tutorial
//package com.java2s; /* * Copyright 2013-2014 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Amazon Software License (the "License"). You may not use * this file except in compliance with the License. A copy of the License is * located at * * http://aws.amazon.com/asl/ * * This Software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, express or implied. See the License for the * specific language governing permissions and limitations under the License. */ import android.view.InputDevice.MotionRange; public class Main { private static int joystickScale(float in, MotionRange range, int zone) { if (range == null) { return 0; // no range means we don't have this control } float min = range.getMin(); float max = range.getMax(); if (in < min) { in = min; } if (in > max) { in = max; } if (Math.abs(in) >= range.getFlat()) { if (in >= 0) { min = range.getFlat(); return (int) (((in - min) / (max - min)) * (32767 - zone)) + zone; } else { max = -range.getFlat(); return (int) (((in - min) / (max - min)) * (32768 - zone) - 32768.0); } } else { // scaling up TO the zone; this is the nominally ZERO zone min = -range.getFlat(); max = range.getFlat(); return (int) (((in - min) / (max - min)) * zone * 2 - zone); } } }