创建一个可绘制通知栏 [英] Create a notification Bar with Drawable

查看:207
本文介绍了创建一个可绘制通知栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建使用可绘制对象的自定义通知栏,但它鸵鸟政策收到可绘制在参数中,只有位图

问题:

我需要得到其他应用程序的图标在我的通知设置。我尝试使用下面的code:

  getPackageManager()getApplicationIcon(的packageName); //返回一个对象可绘制//问题
Notification.Builder建设者=新Notification.Builder(背景);
builder.setLargeIcon(位图) //我不有这一个,只是一个可绘制对象。

正如你所看到的。我怎么可以使用这个对象在我的通知栏放?

的结果预期:

创建的另一个应用程序,名字的图标和行动这一个通知栏。

解决方案:

谢谢,pietmau和kadrei。

请使用低于此code:

 公共静态位图drawableToBitmap(绘制对象绘制){
    如果(绘制的instanceof BitmapDrawable){
        返回((BitmapDrawable)绘制).getBitmap();
    }    INT宽度= drawable.getIntrinsicWidth();
    宽度=宽度GT; 0?宽度:1;
    INT高度= drawable.getIntrinsicHeight();
    高度=身高> 0?身高:1;    位图的位图= Bitmap.createBitmap(宽度,高度,Config.ARGB_8888);
    帆布帆布=新的Canvas(位图);
    drawable.setBounds(0,0,canvas.getWidth(),canvas.getHeight());
    drawable.draw(画布);    返回位图;
}

和完成这一个:

  Notification.Builder建设者=新Notification.Builder(背景);可绘制图标= getPackageManager()。getApplicationIcon(
                的packageName);
位图bitmapIcon = drawableToBitmap(图标);builder.setIcon(android.R.drawable.stat_sys_download_done).//必要的汇入作业把资源在这里。如果您鸵鸟政策投入任何资源,然后通知栏不显示。
setLargeIcon(bitmapIcon);通知通知= builder.getNotification();
notificationManager.notify(yourId,通知);


解决方案

  =位图BitmapFactory.de codeResource(context.getResources()
                                       R.drawable.drawable);

编辑编辑修改

试试这个话:

 公共静态位图drawableToBitmap(绘制对象绘制){
       如果(绘制的instanceof BitmapDrawable){
           返回((BitmapDrawable)绘制).getBitmap();
       }       INT宽度= drawable.getIntrinsicWidth();
       宽度=宽度GT; 0?宽度:1;
       INT高度= drawable.getIntrinsicHeight();
       高度=身高> 0?身高:1;       位图的位图= Bitmap.createBitmap(宽度,高度,Config.ARGB_8888);
       帆布帆布=新的Canvas(位图);
       drawable.setBounds(0,0,canvas.getWidth(),canvas.getHeight());
       drawable.draw(画布);       返回位图;
    }

I am trying to create a custom Notification bar using a Drawable Object, but it don´t receive Drawable in the parameter, only Bitmap.

The Problem:

I need to get the icon of another application to set in my Notification. I tried use the code below:

getPackageManager().getApplicationIcon(packageName);//Return a Drawable Object

//Problem
Notification.Builder builder = new Notification.Builder(context);
builder.setLargeIcon(Bitmap); //I don´t have this one, only a Drawable object.

As you can see. How I can use this Object to put in my Notification bar ?

Result expected:

Create a Notification Bar with the icon of another application, name and the Action to this one.

SOLUTION:

Thanks, pietmau and kadrei.

Please use this code below:

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    int width = drawable.getIntrinsicWidth();
    width = width > 0 ? width : 1;
    int height = drawable.getIntrinsicHeight();
    height = height > 0 ? height : 1;

    Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

and complete with this one:

Notification.Builder builder = new Notification.Builder(context);

Drawable icon = getPackageManager().getApplicationIcon(
                packageName);
Bitmap bitmapIcon = drawableToBitmap(icon);

builder.setIcon(android.R.drawable.stat_sys_download_done).//It´s necessary put a resource here. If you don´t put any resource, then the Notification Bar is not show.
setLargeIcon(bitmapIcon);

Notification notification = builder.getNotification();
notificationManager.notify(yourId, notification);

解决方案

 Bitmap= BitmapFactory.decodeResource(context.getResources(),
                                       R.drawable.drawable);

EDIT EDIT EDIT

Try this then:

    public static Bitmap drawableToBitmap (Drawable drawable) {
       if (drawable instanceof BitmapDrawable) {
           return ((BitmapDrawable)drawable).getBitmap();
       }

       int width = drawable.getIntrinsicWidth();
       width = width > 0 ? width : 1;
       int height = drawable.getIntrinsicHeight();
       height = height > 0 ? height : 1;

       Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
       Canvas canvas = new Canvas(bitmap); 
       drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
       drawable.draw(canvas);

       return bitmap;
    }

这篇关于创建一个可绘制通知栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆