get Gradient Bitmap - Android Graphics

Android examples for Graphics:Bitmap Effect

Description

get Gradient Bitmap

Demo Code


import android.graphics.Bitmap;
import android.graphics.Color;

public class Main {
  public static Bitmap getGradientBitmap(int width, int height, int color) {
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    int alpha = Color.alpha(color);
    int red = Color.red(color);
    int green = Color.green(color);
    int blue = Color.blue(color);

    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    for (int y = 0; y < height; y++) {
      int gradientAlpha = (int) ((float) alpha * (float) (height - y) * (float) (height - y) / (float) height
          / (float) height);
      for (int x = 0; x < width; x++) {
        pixels[x + y * width] = Color.argb(gradientAlpha, red, green, blue);
      }/*from  w  w  w .  ja va 2  s.com*/
    }

    bitmap.setPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    return bitmap;
  }
}

Related Tutorials