Android:Notification之SmallIcon制作

 android5.0以上版本对Notification进行了改进,对于通知栏上的小图标不再支持五颜六色的png图像了,仅支持只有alpha通道的png图,还得只能用白色绘制。

  可以使用photoshop创建此种图像:

 

 

  • 步骤一:新建幅背景内容透明的RGB图像,并用白色画笔勾勒出图标的形状,
  • 步骤二:切换到通道面板中新建一个通道(默认就为alpha通道),然后关闭RGB通道,勾选Alpha1通道,
  • 步骤三:点击菜单栏文件-导出png图像,

这样导出的png图标可在Android5.0+版本显示,不会变成一个白框。勾勒图标时无论什么颜色,只打开alpha通道导出的png图在通知栏均为白色。


 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"/>

</RelativeLayout>

MainActivity.java

package com.example.sheep.firestone;

import android.app.Notification;
import android.app.NotificationManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button button;
    private Notification notification;
    private NotificationManager notificationManager;
    private final int n_id = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button= this.<Button>findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notificationManager.notify(n_id,notification);
            }
        });

        Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.timg003);
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder nb=new Notification.Builder(this);
        nb.setSmallIcon(R.drawable.smallicon)
                .setLargeIcon(bitmap)
                .setContentTitle("System Alert")
                .setContentText("Code 001")
                .setPriority(Notification.PRIORITY_MAX);
        notification=nb.build();

    }
}


参考

https://blog.csdn.net/cqconelin/article/details/79583517 

猜你喜欢

转载自blog.csdn.net/sandalphon4869/article/details/87922153
今日推荐