Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Gray Out an Image in android

    • 0
    • 2
    • 2
    • 1
    • 1
    • 0
    • 0
    • 0
    • 3.99k
    Comment on it

    Gray Out an Image in android


    In android, Sometimes, we have to gray out image to show it disable or to show it differently.


    So I share my code to gray out any ImageView. We just do it by using pixels.

    private ImageView instagramIcon;
    instagramIcon =(ImageView)rootView.findViewById(R.id.instagram_image);

    public static Bitmap grayOutImage(Bitmap src) {
            // constant factors
            final double G_RED = 0.299;
            final double G_GREEN = 0.587;
            final double G_BLUE = 0.114;

    // create output bitmap Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig()); // pixel information int A, R, G, B; int pixel; // get image size int width = src.getWidth(); int height = src.getHeight(); // scan through every single pixel for(int x = 0; x < width; ++x) { for(int y = 0; y < height; ++y) { // get one pixel color pixel = src.getPixel(x, y); // retrieve color of all channels A = Color.alpha(pixel); R = Color.red(pixel); G = Color.green(pixel); B = Color.blue(pixel); // take conversion up to one single value R = G = B = (int)(G_RED * R + G_GREEN * G + G_BLUE * B); // set new pixel color to output bitmap bmOut.setPixel(x, y, Color.argb(A, R, G, B)); } } // return final image return bmOut; }

    and apply this like


    instagramIcon.setImageBitmap(grayScaleImage(BitmapFactory.decodeResource(getResources(),R.drawable.instagram_icon)));

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: