Java tutorial
//package com.java2s; /* * Copyright (C) Stichting Akvo (Akvo Foundation) * * This file is part of Akvo Caddisfly. * * Akvo Caddisfly is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Akvo Caddisfly is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Akvo Caddisfly. If not, see <http://www.gnu.org/licenses/>. */ import android.graphics.Color; public class Main { /** * Get the color that lies in between two colors * * @param startColor The first color * @param endColor The last color * @param n Number of steps between the two colors * @param i The index at which the color is to be calculated * @return The newly generated color */ public static int getGradientColor(int startColor, int endColor, int n, int i) { return Color.rgb(interpolate(Color.red(startColor), Color.red(endColor), n, i), interpolate(Color.green(startColor), Color.green(endColor), n, i), interpolate(Color.blue(startColor), Color.blue(endColor), n, i)); } /** * Get the color component that lies between the two color component points * * @param start The first color component value * @param end The last color component value * @param n Number of steps between the two colors * @param i The index at which the color is to be calculated * @return The calculated color component */ private static int interpolate(int start, int end, int n, int i) { return (int) ((float) start + ((((float) end - (float) start) / n) * i)); } }