How to use Palette API to extract colors from Image or Vector drawable Image Fully detailed tutorial
The usages example of Palette API - Android Studio |
Hello Guys, Today I going to share an amazing tutorial on
The old constructor instances:
Bitmap bmp; //(Keep In mind that, You need decode the bitmap or vector first to get its color from palette, and this is just an example)
Palette palette = Palette.generate(bmp);
is deprecated from android API 29, So app target SDK 29 or Minimum SDK 29 we need an alternative version of this constructor.
Don't worry, I am here to help!
The Palette.generate(Bitmap) deprecated,The alternative code is here!
Now time to write the right code for palette.generate(Bitmap) & for color extraction
The Code:
The Code:
//Starts the code from here!
//decode the bitmap so that palette can use this bitmap!
Bitmap bmp = BitmapFactory.decodeResource(/*Your Activity name.this, but I used MainActivity.this for instances*/ MainActivity.this.getApplicationContext().getResources(), R.drawable.your_drawable_image_name_here);
//now time to input the image for palette so that we can get the color from this image
Palette palette = Palette.from(bmp).generate();
//Now time to get the int of the color so that we can set the color direct for use
//I Have used the Light Vibrant Color, there are another mode too that you can get
int colorLight = palette.getLightVibrantColor(0x000000);
//For instance, I use this color for my toolbar
toolbar.setBackgroundColor(colorLight);
//below I going to share process of how you can get the same result for Vector drawables
//Continue read it!
See this screenshot for understanding the extract colors from bitmap using palette |
For Vector Image, We'll use another trick!
//the code for extracts vector drawable starts from here.
//Get the Vector drwable from res folder
Drawable drawable = ContextCompat.getDrawable(MainActivity.this, R.drawable.your_vector_drawable_name_here);
//Now time to code so that we can decode & make the vector drawble to an regular Bitmap Image
//The below code will do this job
Bitmap bitmap = Bitmap.createBitmap(Objects.requireNonNull(d).getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
//now time to input the image for palette so that we can get the color from this image
Palette palette = Palette.from(bitmap).generate();
//Now time to get the int of the color so that we can set the color direct for use
//I Have used the Light Vibrant Color, there are another mode too that you can get
int colorLight = palette.getLightVibrantColor(0x000000);
//For instance, I use this color for my toolbar
toolbar.setBackgroundColor(colorLight);
Example code for extracting colors from vector drawable using palette |
WoW damn easy to Implement, Am I Right??
You need not worry about compatibility! This code is compatible with the minimum SDK version of this library!
Comments
Post a Comment
Wow nice post!