Create a notification Bar with Drawable

大城市里の小女人 提交于 2019-12-13 00:26:07

问题


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);

回答1:


Maybe this helps:

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;
}

Source: https://stackoverflow.com/a/9390776/1955332




回答2:


 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;
    }


来源:https://stackoverflow.com/questions/15097560/create-a-notification-bar-with-drawable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!