Palette可以提取的颜色如下: ● Vibrant (有活力的) ● Vibrant dark(有活力的 暗色) ● Vibrant light(有活力的 亮色) ● Muted (柔和的) ● Muted dark(柔和的 暗色) ● Muted light(柔和的 亮色)
导入v7包下的palette: compile 'com.android.support:palette-v7:23.4.0'
● Palette generate(Bitmap bitmap) ● Palette generate(Bitmap bitmap, int numColors) ● generateAsync(Bitmap bitmap, PaletteAsyncListener listener) ● generateAsync(Bitmap bitmap, int numColors, final PaletteAsyncListener listener) 不难看出,生成方法分为generate(同步)和generateAsync(异步)两种,如果图片过大使用generate方法,可能会阻塞主线程,我们更倾向于使用generateAsync的方法,其实内部就是创建了一个AsyncTask。generateAsync方法需要一个PaletteAsyncListener对象用于监听生成完毕的回调。除了必须的Bitmap参数外,还可以传入一个numColors参数指定颜色数,默认是 16。
● Palette.getVibrantSwatch() ● Palette.getDarkVibrantSwatch() ● Palette.getLightVibrantSwatch() ● Palette.getMutedSwatch() ● Palette.getDarkMutedSwatch() ● Palette.getLightMutedSwatch()
● getPopulation(): 样本中的像素数量 ● getRgb(): 颜色的RBG值 ● getHsl(): 颜色的HSL值 ● getBodyTextColor(): 主体文字的颜色值 ● getTitleTextColor(): 标题文字的颜色值 通过 getRgb() 可以得到最终的颜色值并应用到UI中。getBodyTextColor() 和 getTitleTextColor() 可以得到此颜色下文字适合的颜色,这样很方便我们设置文字的颜色,使文字看起来更加舒服。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="darkMutedColor"
android:minHeight="40dp"
android:gravity="center"
android:layout_marginTop="10dp"
android:layout_alignParentTop="true"
/>
<TextView
android:id="@+id/t2"
android:layout_below="@+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="darkVibrantColor"
android:minHeight="40dp"
android:gravity="center"
android:layout_marginTop="10dp"
/>
<TextView
android:id="@+id/t3"
android:layout_below="@+id/t2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="lightVibrantColor"
android:minHeight="40dp"
android:gravity="center"
android:layout_marginTop="10dp"
/>
<TextView
android:layout_below="@+id/t3"
android:id="@+id/t4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="lightMutedColor"
android:minHeight="40dp"
android:gravity="center"
android:layout_marginTop="10dp"
/>
<TextView
android:layout_below="@+id/t4"
android:id="@+id/t5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="mutedColor"
android:minHeight="40dp"
android:gravity="center"
android:layout_marginTop="10dp"
/>
<TextView
android:layout_below="@+id/t5"
android:id="@+id/t6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="vibrantColor"
android:minHeight="40dp"
android:gravity="center"
android:layout_marginTop="10dp"
/>
</RelativeLayout>
</ScrollView>public class MainActivity extends AppCompatActivity {
TextView t1;
TextView t2;
TextView t3;
TextView t4;
TextView t5;
TextView t6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = (TextView) findViewById(R.id.t1);
t2 = (TextView) findViewById(R.id.t2);
t3 = (TextView) findViewById(R.id.t3);
t4 = (TextView) findViewById(R.id.t4);
t5 = (TextView) findViewById(R.id.t5);
t6 = (TextView) findViewById(R.id.t6);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.f);
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
//发生主线程 Palette调色板 总共六种颜色
@Override
public void onGenerated(Palette palette) {
//柔和而暗的颜色
int darkMutedColor = palette.getDarkMutedColor(Color.BLUE);
//鲜艳和暗的颜色
int darkVibrantColor = palette.getDarkVibrantColor(Color.BLUE);
//亮和鲜艳的颜色
int lightVibrantColor = palette.getLightVibrantColor(Color.BLUE);
//亮和柔和的颜色
int lightMutedColor = palette.getLightMutedColor(Color.BLUE);
//柔和颜色
int mutedColor = palette.getMutedColor(Color.BLUE);
int vibrantColor = palette.getVibrantColor(Color.BLUE);
t1.setBackgroundColor(darkMutedColor);
t2.setBackgroundColor(darkVibrantColor);
t3.setBackgroundColor(lightVibrantColor);
t4.setBackgroundColor(lightMutedColor);
t5.setBackgroundColor(mutedColor);
t6.setBackgroundColor(vibrantColor);
}
});
}
}
Palette取出的各种颜色.jpg