首页 文章

将可绘制资源图像转换为位图

提问于
浏览
158

我正在尝试使用带有位图图像的 Notification.Builder.setLargeIcon(bitmap) . 我有我想在可绘制文件夹中使用的图像,那么如何将其转换为位图?

6 回答

  • 6
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);
    

    Context 可以是您当前的 Activity .

  • 0

    首先创建位图图像

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

    现在在Notification Builder Icon中设置位图....

    Notification.Builder.setLargeIcon(bmp);
    
  • 9

    res/drawable 文件夹中,

    1. 创建一个新的 Drawable Resources .

    2. 输入文件名 .

    将在 res/drawable 文件夹中创建一个新文件 .

    在新创建的文件中替换此代码,并将 ic_action_back 替换为您的可绘制文件名 .

    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/ic_action_back"
        android:tint="@color/color_primary_text" />
    

    现在,您可以将它与资源ID一起使用, R.id.filename .

  • 13
    Drawable myDrawable = getResources().getDrawable(R.drawable.logo);
    Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();
    

    由于API 22 getResources().getDrawable() 已弃用,因此我们可以使用以下解决方案 .

    Drawable vectorDrawable = VectorDrawableCompat.create(getResources(), R.drawable.logo,  getContext().getTheme());
    Bitmap myLogo = ((BitmapDrawable) vectorDrawable).getBitmap();
    
  • 43

    你可能意味着 Notification.Builder.setLargeIcon(Bitmap) ,对吗? :)

    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
    notBuilder.setLargeIcon(largeIcon);
    

    这是将资源图像转换为Android Bitmap 的好方法 .

  • 376

    这是另一种将Drawable资源转换为android中的Bitmap的方法:

    Drawable drawable = getResources().getDrawable(R.drawable.input);
    Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
    

相关问题