Android Lollipop新特性-Palette

效果预览


使用方法

Palette可以提取的颜色如下

  • Vibrant (有活力的)
  • Vibrant dark(有活力的 暗色)
  • Vibrant light(有活力的 亮色)
  • Muted (柔和的)
  • Muted dark(柔和的 暗色)
  • Muted light(柔和的 亮色)

我们要想使用Palette,需要导入Palette的兼容库,Gradle 中添加下面依赖。

1
compile 'com.android.support:palette-v7:21.0.+'

实例代码

xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ImageView
android:id="@+id/image"
android:layout_width="72dp"
android:layout_height="72dp" />
<Button
android:id="@+id/vibrant"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="vibrant" />
<Button
android:id="@+id/vibrantLight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="vibrantLight" />
<Button
android:id="@+id/vibrantDark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="vibrantDark" />
<Button
android:id="@+id/muted"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="muted" />
<Button
android:id="@+id/mutedLight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="mutedLight" />
<Button
android:id="@+id/mutedDark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="mutedDark" />
</LinearLayout>

java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.wuxiaolong.apksample;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.graphics.Palette;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView image;
ActionBar mActionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
image.setImageResource(R.mipmap.dog);
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.dog);
mActionBar = getSupportActionBar();
}
public void onClick(View view) {
Palette.from(drawableToBitmap(image.getDrawable())).generate(new LPaletteAsyncListener(view));
}
/**
* 参考博客https://www.bignerdranch.com/blog/extracting-colors-to-a-palette-with-android-lollipop/
*/
class LPaletteAsyncListener implements Palette.PaletteAsyncListener {
View view;
LPaletteAsyncListener(View view) {
this.view = view;
}
@Override
public void onGenerated(Palette palette) {
int paletteColor = 0;
switch (view.getId()) {
case R.id.vibrant:
Palette.Swatch swatch = palette.getVibrantSwatch();
paletteColor = palette.getVibrantColor(0x000000);
// paletteColor = swatch.getRgb();
break;
case R.id.vibrantLight:
paletteColor = palette.getLightVibrantColor(0x000000);
break;
case R.id.vibrantDark:
paletteColor = palette.getDarkVibrantColor(0x000000);
break;
case R.id.muted:
paletteColor = palette.getMutedColor(0x000000);
break;
case R.id.mutedLight:
paletteColor = palette.getLightMutedColor(0x000000);
break;
case R.id.mutedDark:
paletteColor = palette.getDarkMutedColor(0x000000);
break;
}
if (mActionBar != null) {
mActionBar.setBackgroundDrawable(new ColorDrawable(paletteColor));
}
}
}
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
}

或者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static int getPaletteColor(Bitmap bitmap) {
int color = -12417291;
Palette palette = Palette.from(bitmap).generate();
Palette.Swatch vibrant = palette.getVibrantSwatch();
Palette.Swatch vibrantdark = palette.getDarkVibrantSwatch();
Palette.Swatch vibrantlight = palette.getLightVibrantSwatch();
Palette.Swatch Muted = palette.getMutedSwatch();
Palette.Swatch Muteddark = palette.getDarkMutedSwatch();
Palette.Swatch Mutedlight = palette.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;
}



联系作者

我的微信公众号:吴小龙同学,欢迎关注交流,公号回复关键字「1024」有惊喜哦。