Android 自定义通知Notification 适配不同背景颜色

自定义通知

自定义通知首先明确一点是要用RemoteViews来设置布局

PendingIntent remotePending=PendingIntent.getActivity(MainActivity.this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

RemoteViews contentView=new RemoteViews(getPackageName(), R.layout.remoteview);
contentView.setTextViewText(R.id.share_content, "这是自定义的view");
contentView.setOnClickPendingIntent(R.id.share_facebook, remotePending);
contentView.setOnClickPendingIntent(R.id.share_twitter, remotePending);

RemoteViews bigContentView=new RemoteViews(getPackageName(), R.layout.bigcontentview);
bigContentView.setTextViewText(R.id.share_content, "这是自定义的view");
bigContentView.setOnClickPendingIntent(R.id.share_facebook, remotePending);
bigContentView.setOnClickPendingIntent(R.id.share_twitter, remotePending);

  1. RemoteViews适配
我们在使用remoteViews的时候,最怕就是遇到这种情况:因为通知栏的背景色在不同rom上的效果不同,文本颜色就不好确定,你写成白的,那么在白色背景上就有问题,写成黑的,在黑色背景上就有问题。怎么办?之前在网上找的通过style进行处理的方式不可行,我在这里结合 hackware大神提供的方法来获取通知的背景色

public static boolean isDarkNotificationTheme(Context context) {
    return !isSimilarColor(Color.BLACK, getNotificationColor(context));
}

/**
 * 获取通知栏颜色
 * @param context
 * @return
 */
public static int getNotificationColor(Context context) {
    NotificationCompat.Builder builder=new NotificationCompat.Builder(context);
    Notification notification=builder.build();
    int layoutId=notification.contentView.getLayoutId();
    ViewGroup viewGroup= (ViewGroup) LayoutInflater.from(context).inflate(layoutId, null, false);
    if (viewGroup.findViewById(android.R.id.title)!=null) {
        return ((TextView) viewGroup.findViewById(android.R.id.title)).getCurrentTextColor();
    }
    return findColor(viewGroup);
}

private static boolean isSimilarColor(int baseColor, int color) {
    int simpleBaseColor=baseColor|0xff000000;
    int simpleColor=color|0xff000000;
    int baseRed=Color.red(simpleBaseColor)-Color.red(simpleColor);
    int baseGreen=Color.green(simpleBaseColor)-Color.green(simpleColor);
    int baseBlue=Color.blue(simpleBaseColor)-Color.blue(simpleColor);
    double value=Math.sqrt(baseRed*baseRed+baseGreen*baseGreen+baseBlue*baseBlue);
    if (value<180.0) {
        return true;
    }
    return false;
}
private static int findColor(ViewGroup viewGroupSource) {
    int color=Color.TRANSPARENT;
    LinkedList<ViewGroup> viewGroups=new LinkedList<>();
    viewGroups.add(viewGroupSource);
    while (viewGroups.size()>0) {
        ViewGroup viewGroup1=viewGroups.getFirst();
        for (int i = 0; i < viewGroup1.getChildCount(); i++) {
            if (viewGroup1.getChildAt(i) instanceof ViewGroup) {
                viewGroups.add((ViewGroup) viewGroup1.getChildAt(i));
            }
            else if (viewGroup1.getChildAt(i) instanceof TextView) {
                if (((TextView) viewGroup1.getChildAt(i)).getCurrentTextColor()!=-1) {
                    color=((TextView) viewGroup1.getChildAt(i)).getCurrentTextColor();
                }
            }
        }
        viewGroups.remove(viewGroup1);
    }
    return color;
}

这里有几个注意点

  • TextView textView= (TextView) viewGroup.findViewById(android.R.id.title);可能在有的手机上获取textview为空,所以我想notification的文本区域,系统默认肯定有一个值的,那我就直接遍历找到这个值即可

这样你就可以通过
contentView.setInt(R.id.share_content, "setTextColor", NotificationUtils.isDarkNotificationTheme(MainActivity.this)==true?Color.WHITE:Color.BLACK);

实现颜色替换的功能,setInt、setString的功能是通过反射进行操作的。

本文参考自:http://www.jianshu.com/p/141fc999ac10




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值