36
33

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Androidで画像に合ういい感じの色を見つけてくれるPalette Libraryを使う

Last updated at Posted at 2014-11-30

新しくSupport Libraryに追加されたPalette Libraryを使ってみました。
Palette LibraryはBitmapを渡すと、その画像に合う背景色、タイトルカラー、テキストカラーを返してくれます。
スクリーンショット 2014-11-30 18.31.38.png

導入

Gradleを使っている場合はbuild.gradleにPalette Libraryに追加します。

compile 'com.android.support:palette-v7:21.0.0'

使用方法

Palette.generateとPalette.generateAsyncの2種類がありますが、適している方を使います。

Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
    public void onGenerated(Palette palette) {
        if  (palette != null) {
            Palette.Swatch vibrantSwatch = palette.getVibrantSwatch();
            if (vibrantSwatch != null) {
                mVibrantContaienr.setBackgroundColor(vibrantSwatch.getRgb());
                mVibrantTitleView.setTextColor(vibrantSwatch.getTitleTextColor());
                mVibrantText.setTextColor(vibrantSwatch.getBodyTextColor());
            }
        }
    }
});

本当に渡すだけでいい感じの色を選んでくれます。
SwatchはVibrant(鮮やか)とMuted(落ち着いた)、さらにそれぞれのLightとDarkがあります。
デザインに合うものを選べばいいと思います。
また、色情報が見つからない場合、Swatchにnullが返ってくるので、返ってこなかった場合のことも考えておく必要はありそうです。

内容がないクソみたいなサンプルも作ってあるので、よかったら見てみてください。
https://github.com/kazutoyo/PaletteSample

APKのバイナリもつくってあるので実機で試せます。
[APK]

36
33
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
33

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?