android开发分享将可绘制的资源图像转换成位图

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

    你可能是指Notification.Builder.setLargeIcon(Bitmap) ,对不对? ?

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

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

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

    由于API 22 getResources().getDrawable()已被弃用,所以我们可以使用以下解决scheme。

     Drawable vectorDrawable = ResourcesCompat.getDrawable(context.getResources(), R.drawable.logo, null); Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap(); 

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

    Context可以是您当前的Activity

    这里是另一种在android中将Drawable资源转换成位图的方法:

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

    首先创build位图图像

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

    现在在通知生成器图标中设置位图…

     Notification.Builder.setLargeIcon(bmp); 

    res/drawable文件夹中,

    1.创build一个新的Drawable Resources

    2.input文件名称。

    res/drawable文件夹中将创build一个新文件。

    在新创build的文件中replace此代码,并用您的可绘制文件名replaceic_action_back

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

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

      以上就是android开发分享将可绘制的资源图像转换成位图相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/516865.html

      (0)
      上一篇 2020年12月3日
      下一篇 2020年12月3日

      精彩推荐