[英]converting drawable resource image into bitmap
我正在嘗試使用采用 bitmap 圖像的Notification.Builder.setLargeIcon(bitmap)
。 我的可繪制文件夾中有我想使用的圖像,那么如何將其轉換為 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()
已棄用,因此我們可以使用以下解決方案。
Drawable vectorDrawable = VectorDrawableCompat.create(getResources(), R.drawable.logo, getContext().getTheme());
Bitmap myLogo = ((BitmapDrawable) vectorDrawable).getBitmap();
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);
Context
可以是您當前的Activity
。
這是在android中將Drawable資源轉換為Bitmap的另一種方法:
Drawable drawable = getResources().getDrawable(R.drawable.input);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
首先創建位圖圖像
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image);
現在在通知生成器圖標中設置位圖....
Notification.Builder.setLargeIcon(bmp);
在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
。
如果有人正在尋找大圖標的 Kotlin 版本,你可以使用這個
val largeIcon = BitmapFactory.decodeResource(context.resources, R.drawable.my_large_icon)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.