Created
July 26, 2019 07:36
-
-
Save RanjitPati/ced1c0d1205aba0525a891757a32b355 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myapplicationpalettetest; | |
import android.graphics.Bitmap; | |
import android.graphics.drawable.BitmapDrawable; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.graphics.Palette; | |
import android.widget.Button; | |
import android.widget.ImageView; | |
public class MainActivity extends AppCompatActivity { | |
private ImageView mainImage; | |
private Button vibrant; | |
private Button vibrantDark; | |
private Button vibrantLight; | |
private Button muted; | |
private Button mutedDark; | |
private Button mutedLight; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
//let's initialize the ImageView and get the Bitmap from the Image to use further | |
mainImage = findViewById(R.id.image); | |
//now initialize all Buttons | |
vibrant = findViewById(R.id.btnVibrant); | |
vibrantDark = findViewById(R.id.btnVibrantDark); | |
vibrantLight = findViewById(R.id.btnVibrantLight); | |
muted = findViewById(R.id.btnMuted); | |
mutedDark = findViewById(R.id.btnMutedDark); | |
mutedLight = findViewById(R.id.btnMutedLight); | |
//let's get the Drawable from the Image | |
//we will get the Bitmap Drawable, from which we can easily get the Bitmap | |
BitmapDrawable mBitmapDrawable = (BitmapDrawable) mainImage.getDrawable(); | |
//Now get the Bitmap to use further | |
Bitmap mBitmap = mBitmapDrawable.getBitmap(); | |
//Syncronous way | |
Palette palette = Palette.from(mBitmap).generate(); | |
//Asyncronous way to generate the Palette | |
Palette.from(mBitmap).generate(new Palette.PaletteAsyncListener() { | |
@Override | |
public void onGenerated(@Nullable Palette palette) { | |
//Now we can use the Palette object | |
//first get each color swatch and | |
Palette.Swatch vibrantSwatch = palette.getVibrantSwatch(); | |
Palette.Swatch darkVibrantSwatch = palette.getDarkVibrantSwatch(); | |
Palette.Swatch lightVibrantSwatch = palette.getLightVibrantSwatch(); | |
Palette.Swatch mutedSwatch = palette.getMutedSwatch(); | |
Palette.Swatch mutedDarkSwatch = palette.getDarkMutedSwatch(); | |
Palette.Swatch mutedLightSwatch = palette.getLightMutedSwatch(); | |
//now pass each swatch to the method to set the bg and text color to each Button | |
setButtonSwatch(vibrant, vibrantSwatch); | |
setButtonSwatch(vibrantDark, darkVibrantSwatch); | |
setButtonSwatch(vibrantLight, lightVibrantSwatch); | |
setButtonSwatch(muted, mutedSwatch); | |
setButtonSwatch(mutedDark, mutedDarkSwatch); | |
setButtonSwatch(mutedLight, mutedLightSwatch); | |
} | |
}); | |
} | |
//create a method with the parameters as TextView and Swatches | |
private void setButtonSwatch(Button button, Palette.Swatch swatch) { | |
if (swatch != null) { | |
// Set the background color of a layout based on the vibrant color | |
button.setBackgroundColor(swatch.getRgb()); | |
button.setTextColor(swatch.getTitleTextColor()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment