Android examples for Graphics:Color
Creates a gradient of colors.
/******************************************************************************* * Copyright (c) 2011 MadRobot./* ww w. j av a2 s.c o m*/ * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v2.1 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * * Contributors: * Elton Kent - initial API and implementation ******************************************************************************/ //package com.java2s; public class Main { /** * Creates a gradient of colors. This method is highly optimized and only * uses bit-shifting and additions (no multitplication nor devision), but it * will create a new integer array in each call. * * @param startColor * the first color * @param endColor * the last color * @param steps * the number of colors in the gradient, when 2 is given, the * first one will be the startColor and the second one will the * endColor. * @return an int array with the gradient. * */ public static final int[] getGradient(int startColor, int endColor, int steps) { int[] gradient = new int[steps]; getGradient(startColor, endColor, gradient); return gradient; } /** * Creates a gradient of colors. This method is highly optimized and only * uses bit-shifting and additions (no multitplication nor devision). * * @param startColor * The first color * @param endColor * The last color * @param gradient * The array in which the gradient colors are stored. length of * the array is the number of steps used in the gradient * */ public static final void getGradient(final int startColor, final int endColor, final int[] gradient) { int steps = gradient.length; if (steps == 0) { return; } else if (steps == 1) { gradient[0] = startColor; return; } int startAlpha = startColor >>> 24; int startRed = (startColor >>> 16) & 0x00FF; int startGreen = (startColor >>> 8) & 0x0000FF; int startBlue = startColor & 0x00000FF; final int endAlpha = endColor >>> 24; final int endRed = (endColor >>> 16) & 0x00FF; final int endGreen = (endColor >>> 8) & 0x0000FF; final int endBlue = endColor & 0x00000FF; final int stepAlpha = ((endAlpha - startAlpha) << 8) / (steps - 1); final int stepRed = ((endRed - startRed) << 8) / (steps - 1); final int stepGreen = ((endGreen - startGreen) << 8) / (steps - 1); final int stepBlue = ((endBlue - startBlue) << 8) / (steps - 1); startAlpha <<= 8; startRed <<= 8; startGreen <<= 8; startBlue <<= 8; gradient[0] = startColor; for (int i = 1; i < steps; i++) { startAlpha += stepAlpha; startRed += stepRed; startGreen += stepGreen; startBlue += stepBlue; gradient[i] = ((startAlpha << 16) & 0xFF000000) | ((startRed << 8) & 0x00FF0000) | (startGreen & 0x0000FF00) | (startBlue >>> 8); // | (( startBlue >>> 8) & 0x000000FF); } } }