项目中要做成以下的效果,本地应用直接使用包名和颜色值遍历找对应,三方应用要去提取app的icon颜色作为背景,首先想到了Android5.0新特性相关的palette。
基本使用:异步获取,同步获得会很卡。
1.过时的写法。
//异步获得bitmap图片的颜色值
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
Palette.Swatch muted = palette.getMutedSwatch();//柔和的
if (muted != null) {
holder.app_bg.setBackgroundColor(vibrant.getRgb());
}
}
});
2.替代的写法
//异步获得bitmap图片颜色值
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
Palette.Swatch muted = palette.getMutedSwatch();//柔和的
if (muted != null) {
holder.app_bg.setBackgroundColor(vibrant.getRgb());
}
}
});
由于在Android设备中,对图像的处理有可能是耗时操作,因此,Palette类通过异步接口onGenerated回调的方法
来获得Bitmap的颜色值。Palette类获得的颜色值有以下几种类型:
- Palette.Swatch a = palette.getVibrantSwatch();//有活力
- Palette.Swatch b = palette.getDarkVibrantSwatch();//有活力 暗色
- Palette.Swatch c = palette.getLightVibrantSwatch();//有活力 亮色
- Palette.Swatch d = palette.getMutedSwatch();//柔和
- Palette.Swatch e = palette.getDarkMutedSwatch();//柔和 暗色
- Palette.Swatch f = palette.getLightMutedSwatch();//柔和 亮色
我们从以上颜色中可以获取到如下颜色值:
- int color1 = a.getBodyTextColor();//内容颜色
- int color2 = a.getTitleTextColor();//标题颜色
- int color3 = a.getRgb();//rgb颜色
项目中使用如下:
1.圆角的shape:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#bbb" />
<corners android:topLeftRadius="6dp"
android:topRightRadius="6dp"
android:bottomRightRadius="6dp"
android:bottomLeftRadius="6dp"/>
</shape>
2.使用shape
<RelativeLayout
android:id="@+id/id_rlayout_adapter_app_bg"
android:layout_width="246dip"
android:layout_height="160dip"
android:elevation="4dip"
android:background="@drawable/layout_border">
...
</RelativeLayout>
3.使用palette,并改变shape的颜色。
//修改shape的背景色
final GradientDrawable p = (GradientDrawable) holder.app_bg.getBackground();
final Bitmap bitmap = ShapeUtil.drawableToBitmap(icon);
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
Palette.Swatch muted = palette.getMutedSwatch();
if (muted != null) {
p.setColor(muted.getRgb());
} else {
//当取不到时又取了一次,如果还没有的化,就是默认的#bbb了
Palette.Swatch lightMuted = palette.getLightMutedSwatch();
if (lightMuted != null) {
p.setColor(lightMuted.getRgb());
}
}
}
});
把drawable转为bitmap后图像变了,由于下面的控件还要用drawable对象,下次再刷新notifyDataSetChanged()的时候形状发生了变化。解决方法是复制一份出来。
shapeUtil.java
package com.lenovo.nova.settings.utils;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
/**
* Created by songwenju on 16-1-21.
*/
public class ShapeUtil {
/**
* drawable转bitmap
* @param drawable
* @return
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
// 取 drawable 的长宽
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
// 取 drawable 的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 建立对应 bitmap
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
// 建立对应 bitmap 的画布
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
// 把 drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}
/**
* drawable 复制
*
* @param drawable
* @return
*/
public static Drawable getNewDrawable(Drawable drawable) {
if (drawable != null) {
return drawable.getConstantState().newDrawable();
}else {
throw new NullPointerException("the arg drawable is null");
}
}
}