public SampleView(Context context) { super(context); setFocusable(true); final int N = 100; int[] data8888 = new int[N]; short[] data565 = new short[N]; short[] data4444 = new short[N]; makeRamp(premultiplyColor(Color.RED), premultiplyColor(Color.GREEN), N, data8888, data565, data4444); mBitmap1 = Bitmap.createBitmap(N, N, Bitmap.Config.ARGB_8888); mBitmap2 = Bitmap.createBitmap(N, N, Bitmap.Config.RGB_565); mBitmap3 = Bitmap.createBitmap(N, N, Bitmap.Config.ARGB_4444); mBitmap1.copyPixelsFromBuffer(makeBuffer(data8888, N)); mBitmap2.copyPixelsFromBuffer(makeBuffer(data565, N)); mBitmap3.copyPixelsFromBuffer(makeBuffer(data4444, N)); }
/** * Turn a color int into a premultiplied device color */ private static int premultiplyColor(int c) { int r = Color.red(c); int g = Color.green(c); int b = Color.blue(c); int a = Color.alpha(c); // now apply the alpha to r, g, b r = mul255(r, a); g = mul255(g, a); b = mul255(b, a); // now pack it in the correct order return pack8888(r, g, b, a); }
private static void makeRamp(int from, int to, int n, int[] ramp8888, short[] ramp565, short[] ramp4444) { int r = getR32(from) << 23; int g = getG32(from) << 23; int b = getB32(from) << 23; int a = getA32(from) << 23; // now compute our step amounts per componenet (biased by 23 bits) int dr = ((getR32(to) << 23) - r) / (n - 1); int dg = ((getG32(to) << 23) - g) / (n - 1); int db = ((getB32(to) << 23) - b) / (n - 1); int da = ((getA32(to) << 23) - a) / (n - 1); for (int i = 0; i < n; i++) { ramp8888[i] = pack8888(r >> 23, g >> 23, b >> 23, a >> 23); ramp565[i] = pack565(r >> (23+3), g >> (23+2), b >> (23+3)); ramp4444[i] = pack4444(r >> (23+4), g >> (23+4), b >> (23+4), a >> (23+4)); r += dr; g += dg; b += db; a += da; } }
private static void makeRamp(int from, int to, int n, int[] ramp8888, short[] ramp565, short[] ramp4444) { int r = getR32(from) << 23; int g = getG32(from) << 23; int b = getB32(from) << 23; int a = getA32(from) << 23; // now compute our step amounts per componenet (biased by 23 bits) int dr = ((getR32(to) << 23) - r) / (n - 1); int dg = ((getG32(to) << 23) - g) / (n - 1); int db = ((getB32(to) << 23) - b) / (n - 1); int da = ((getA32(to) << 23) - a) / (n - 1); for (int i = 0; i < n; i++) { ramp8888[i] = pack8888(r >> 23, g >> 23, b >> 23, a >> 23); ramp565[i] = pack565(r >> (23+3), g >> (23+2), b >> (23+3)); ramp4444[i] = pack4444(r >> (23+4), g >> (23+4), b >> (23+4), a >> (23+4)); r += dr; g += dg; b += db; a += da; } }
public SampleView(Context context) { super(context); setFocusable(true); final int N = 100; int[] data8888 = new int[N]; short[] data565 = new short[N]; short[] data4444 = new short[N]; makeRamp(premultiplyColor(Color.RED), premultiplyColor(Color.GREEN), N, data8888, data565, data4444); mBitmap1 = Bitmap.createBitmap(N, N, Bitmap.Config.ARGB_8888); mBitmap2 = Bitmap.createBitmap(N, N, Bitmap.Config.RGB_565); mBitmap3 = Bitmap.createBitmap(N, N, Bitmap.Config.ARGB_4444); mBitmap1.copyPixelsFromBuffer(makeBuffer(data8888, N)); mBitmap2.copyPixelsFromBuffer(makeBuffer(data565, N)); mBitmap3.copyPixelsFromBuffer(makeBuffer(data4444, N)); }
/** * Turn a color int into a premultiplied device color */ private static int premultiplyColor(int c) { int r = Color.red(c); int g = Color.green(c); int b = Color.blue(c); int a = Color.alpha(c); // now apply the alpha to r, g, b r = mul255(r, a); g = mul255(g, a); b = mul255(b, a); // now pack it in the correct order return pack8888(r, g, b, a); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new SampleView(this)); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new SampleView(this)); }