public static RippleDrawable createRipple( Palette palette, @FloatRange(from = 0f, to = 1f) float darkAlpha, @FloatRange(from = 0f, to = 1f) float lightAlpha, @ColorInt int fallbackColor, boolean bounded) { int rippleColor = fallbackColor; // try the named swatches in preference order if (palette != null) { if (palette.getVibrantSwatch() != null) { rippleColor = ColorUtils.modifyAlpha(palette.getVibrantSwatch().getRgb(), darkAlpha); } else if (palette.getLightVibrantSwatch() != null) { rippleColor = ColorUtils.modifyAlpha(palette.getLightVibrantSwatch().getRgb(), lightAlpha); } else if (palette.getDarkVibrantSwatch() != null) { rippleColor = ColorUtils.modifyAlpha(palette.getDarkVibrantSwatch().getRgb(), darkAlpha); } else if (palette.getMutedSwatch() != null) { rippleColor = ColorUtils.modifyAlpha(palette.getMutedSwatch().getRgb(), darkAlpha); } else if (palette.getLightMutedSwatch() != null) { rippleColor = ColorUtils.modifyAlpha(palette.getLightMutedSwatch().getRgb(), lightAlpha); } else if (palette.getDarkMutedSwatch() != null) { rippleColor = ColorUtils.modifyAlpha(palette.getDarkMutedSwatch().getRgb(), darkAlpha); } } return new RippleDrawable( ColorStateList.valueOf(rippleColor), null, bounded ? new ColorDrawable(Color.WHITE) : null); }
public static int getPaletteColor(Bitmap bitmap) { int color = -12417291; Palette p = Palette.from(bitmap).generate(); Palette.Swatch vibrant = p.getVibrantSwatch(); Palette.Swatch vibrantdark = p.getDarkVibrantSwatch(); Palette.Swatch vibrantlight = p.getLightVibrantSwatch(); Palette.Swatch Muted = p.getMutedSwatch(); Palette.Swatch Muteddark = p.getDarkMutedSwatch(); Palette.Swatch Mutedlight = p.getLightMutedSwatch(); if (vibrant != null) { color = vibrant.getRgb(); } else if (vibrantdark != null) { color = vibrantdark.getRgb(); } else if (vibrantlight != null) { color = vibrantlight.getRgb(); } else if (Muted != null) { color = Muted.getRgb(); } else if (Muteddark != null) { color = Muteddark.getRgb(); } else if (Mutedlight != null) { color = Mutedlight.getRgb(); } return color; }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { Uri uri = data.getData(); ContentResolver cr = getContentResolver(); try { Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); image.setImageBitmap(bitmap); Palette palette = Palette.from(bitmap).generate(); Palette.Swatch vibrantSwatch = palette.getVibrantSwatch(); if (vibrantSwatch != null) { cardViews[0].setCardBackgroundColor(vibrantSwatch.getRgb()); } else { cardViews[0].setCardBackgroundColor(Color.WHITE); } Palette.Swatch darkVibrantSwatch = palette.getDarkVibrantSwatch(); if (darkVibrantSwatch != null) { cardViews[1].setCardBackgroundColor(darkVibrantSwatch.getRgb()); } else { cardViews[1].setCardBackgroundColor(Color.WHITE); } Palette.Swatch lightVibrantSwatch = palette.getLightVibrantSwatch(); if (lightVibrantSwatch != null) { cardViews[2].setCardBackgroundColor(lightVibrantSwatch.getRgb()); } else { cardViews[2].setCardBackgroundColor(Color.WHITE); } Palette.Swatch mutedSwatch = palette.getMutedSwatch(); if (mutedSwatch != null) { cardViews[3].setCardBackgroundColor(mutedSwatch.getRgb()); } else { cardViews[3].setCardBackgroundColor(Color.WHITE); } Palette.Swatch darkMutedSwatch = palette.getDarkMutedSwatch(); if (darkMutedSwatch != null) { cardViews[4].setCardBackgroundColor(darkMutedSwatch.getRgb()); } else { cardViews[4].setCardBackgroundColor(Color.WHITE); } Palette.Swatch lightMutedSwatch = palette.getLightMutedSwatch(); if (lightMutedSwatch != null) { cardViews[5].setCardBackgroundColor(lightMutedSwatch.getRgb()); } else { cardViews[5].setCardBackgroundColor(Color.WHITE); } } catch (FileNotFoundException e) { e.printStackTrace(); } } }
private static List<Palette.Swatch> getSwatchesList(Palette palette) { List<Palette.Swatch> swatches = new ArrayList<>(); Palette.Swatch vib = palette.getVibrantSwatch(); Palette.Swatch vibLight = palette.getLightVibrantSwatch(); Palette.Swatch vibDark = palette.getDarkVibrantSwatch(); Palette.Swatch muted = palette.getMutedSwatch(); Palette.Swatch mutedLight = palette.getLightMutedSwatch(); Palette.Swatch mutedDark = palette.getDarkMutedSwatch(); swatches.add(vib); swatches.add(vibLight); swatches.add(vibDark); swatches.add(muted); swatches.add(mutedLight); swatches.add(mutedDark); return swatches; }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View v = inflater.inflate(R.layout.fragment_contact_view, container, false); // Use this to indicate the ratio /*Display display = getWindowManager().getDefaultDisplay(); Point point = new Point(); display.getSize(point); int width = point.x; int height = point.y; // This indicate ratio based on Material design RelativeLayout headerSection = (RelativeLayout) v.findViewById(R.id.contact_view_header); // We are using LinearLayout is because in activity_contact_view the root layout is LinearLayout // and this layout contains contact_view_header headerSection.setLayoutParams(new LinearLayout.LayoutParams(width, (int) (width * (9.0 / 16.0))));*/ mContact = ContactList.getInstance().get(mPosition); mContactName = (TextView) v.findViewById(R.id.contact_name); Toolbar toolbar = (Toolbar) v.findViewById(R.id.contact_view_toolbar); // setSupportActionBar(toolbar); // To catch the click on the menu item edit (icon) toolbar.setOnMenuItemClickListener( new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { int id = item.getItemId(); if (id == R.id.contact_view_edit) { getContrat().selectedEditPosition(mPosition); // Before we use Intent here so fragment was couple with activity /* Intent intent = new Intent(getActivity(), ContactEditActivity.class); intent.putExtra(ContactEditActivity.EXTRA, mPosition); startActivity(intent);*/ Log.d(TAG, "Edit is clicked"); } return false; } }); toolbar.inflateMenu(R.menu.menu_contact_view); ListView listView = (ListView) v.findViewById(R.id.contact_view_fields); mAdapter = new FieldsAdapter(mContact); listView.setAdapter(mAdapter); // To use this palette we have to add an external library // compile "com.android.support:palette-v7:21.0.+" in build.gradle (Module:app) // Palette takes an image, analyzes it and finds the prominent colors. // To do this we need a bitmap which is an object that holds image data. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.parrots); Palette palette = Palette.generate(bitmap); // With our Palette now created, we can get a color from it and use that to change the color of // our icons. mColor = palette.getDarkVibrantSwatch().getRgb(); updateUI(); return v; }