Created
April 1, 2017 07:03
-
-
Save Suleiman19/6171922a2abb41314a44349b57d876cc to your computer and use it in GitHub Desktop.
Palette API that fetches a dynamic color from a Bitmap and also includes fallback colors on failure.
This file contains hidden or 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
try { | |
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.header); | |
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() { | |
@SuppressWarnings("ResourceType") | |
@Override | |
public void onGenerated(Palette palette) { | |
int vibrantColor = palette.getVibrantColor(R.color.primary_500); | |
int vibrantDarkColor = palette.getDarkVibrantColor(R.color.primary_700); | |
collapsingToolbarLayout.setContentScrimColor(vibrantColor); | |
collapsingToolbarLayout.setStatusBarScrimColor(vibrantDarkColor); | |
} | |
}); | |
} catch (Exception e) { | |
// if Bitmap fetch fails, fallback to primary colors | |
Log.e(TAG, "onCreate: failed to create bitmap from background", e.fillInStackTrace()); | |
collapsingToolbarLayout.setContentScrimColor( | |
ContextCompat.getColor(this, R.color.primary_500) | |
); | |
collapsingToolbarLayout.setStatusBarScrimColor( | |
ContextCompat.getColor(this, R.color.primary_700) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment