Friday, April 20, 2012

How to Generate Gradient Color through Programatically in Android?


final LinearLayout layout = (LinearLayout) findViewById(R.id.main);
        try {

            int coloura = 4621323;
            int colourb = 1129730;

            int colora[] = new int[4];
            int colorb[] = new int[4];

            colora[0] = (int) ((coloura) >> 16);
            colora[1] = (int) ((coloura) >> 8);
            colora[2] = (int) coloura ;
            colora[3] = 255;

            colorb[0] = (int) ((colourb) >> 16);
            colorb[1] = (int) ((colourb) >> 8);
            colorb[2] = (int) (colourb);
            colorb[3] = 255;

            int a = Color.argb(colora[3], colora[0], colora[1], colora[2]);
            int b = Color.argb(colorb[3], colorb[0], colorb[1], colorb[2]);

            final GradientDrawable gd = new GradientDrawable(
                    GradientDrawable.Orientation.TOP_BOTTOM,
                    new int[] {a, b});
            gd.setGradientCenter(0.f, 1.f);
            gd.setLevel(2);
            layout.setBackgroundDrawable(gd);
        } catch (Exception e) {
            e.printStackTrace();
        }
Color.argb(colora[3], colora[0], colora[1], colora[2]); -->> Alpha (0 to 255), Red, Green and Blue

No comments:

Post a Comment