安卓5.0新特性之Palette

根据图片来决定标题的颜色和标题栏的背景色,这样视觉上更具有冲击力和新鲜感,而不像统一色调那样呆板。

Palette这个类能提取以下突出的颜色:

Vibrant(充满活力的)

Vibrant dark(充满活力的黑)

Vibrant light(充满活力的亮)

Muted(柔和的)

Muted dark(柔和的黑)

Muted lighr(柔和的亮)

使用方法:传递Bitmap对象给Palette.generate()静态方法。如果不适用线程,则调用Palette.generateAsync()方法并且提供一个监听器去替代。

Palette类中使用getter方法来从检索突出的颜色,比如 Palette.getVibrantColor

接下来是步骤:

在安卓过程中导入V7包

如下代码:

mTextView = (TextView)findViewById(R.id.textView1);
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.test);
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
Palette.Swatch vibrant = palette.getVibrantSwatch();
if(vibrant != null){
mTextView.setBackgroundColor(vibrant.getRgb());
mTextView.setTextColor(vibrant.getTitleTextColor());
}
}
});
}

我是在布局中添加了背景图片 test.jpg 如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/test"
tools:context="${relativePackage}.${activityClass}" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="根据背景图片改变文字颜色" />

</RelativeLayout>

原文地址:https://www.cnblogs.com/yuanting/p/4761079.html