Description: In this post I'm gonna show you how to use android's support library v7 palette for extracting color from bitmap. Its can have variety of use-cases like making the background of the profile image somewhat similar to profile image or changing the whole UI as per the bitmap into the screen. Really Awesome !!!
Github project here
Lets get started.
Step 1: Add this into build.gradle.
Step 2: Pass the bitmap into palette to get List<Palette.Swatch> as shown below.
Step 3: Here we have used CountDownTimer to display the RGB of the bitmap into the background of parent layout after the interval of 1sec. As shown below
Steps 4: Lets combine everything :-)
Output:
Github project here
Lets get started.
Step 1: Add this into build.gradle.
compile 'com.android.support:palette-v7:+'
Step 2: Pass the bitmap into palette to get List<Palette.Swatch> as shown below.
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() { @Override public void onGenerated(Palette palette) { swatchesList = palette.getSwatches(); } } });
Step 3: Here we have used CountDownTimer to display the RGB of the bitmap into the background of parent layout after the interval of 1sec. As shown below
mainContainer.setBackground(AppUtils.createGradient(swatch.getRgb()));
Steps 4: Lets combine everything :-)
public class MainActivity extends AppCompatActivity implements INotifyTimer { private static final String TAG = MainActivity.class.getName(); private List<Palette.Swatch> swatchesList; private CountDownTimerPalette countDownTimer; private int size ; private RelativeLayout mainContainer; private ImageView circularImageView; @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); extractColorFromBitmap(); } private void init() { mainContainer = (RelativeLayout)findViewById(R.id.mainContainer); circularImageView = (ImageView)findViewById(R.id.circularImageView); } private void extractColorFromBitmap() { Bitmap bitmap = AppUtils.getCircleBitmap(BitmapFactory.decodeResource(MainActivity.this.getResources(), R.mipmap.capture_palette)); circularImageView.setImageBitmap(bitmap); Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() { @Override public void onGenerated(Palette palette) { swatchesList = palette.getSwatches(); if (swatchesList != null && swatchesList.size() > 0) { countDownTimer = new CountDownTimerPalette(1000 * swatchesList.size(), 1000, MainActivity.this); countDownTimer.start(); } } }); } @Override public void onTick(int tick) { Palette.Swatch swatch = swatchesList.get(tick - 1); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { mainContainer.setBackground(AppUtils.createGradient(swatch.getRgb())); }else { mainContainer.setBackgroundDrawable(AppUtils.createGradient(swatch.getRgb())); } } @Override public void onFinish() { countDownTimer.start(); } @Override protected void onDestroy() { super.onDestroy(); if (countDownTimer != null){ countDownTimer.cancel(); } } }
Output: