简体   繁体   English

如何在android中将BitmapDrawable转换为LayerDrawable

[英]How to cast BitmapDrawable to LayerDrawable in android

This is code snippet. 这是代码段。

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the main; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    MenuItem item = menu.findItem(R.id.action_notifications);
    LayerDrawable icon = (LayerDrawable) item.getIcon();




    // Update LayerDrawable's BadgeDrawable
    Utils2.setBadgeCount(this, icon, mNotificationsCount);
    return true;
}

It is giving error in line 这是错误的

LayerDrawable icon = (LayerDrawable) item.getIcon();

BitmapDrawable cannot be cast to android.graphics.drawable.LayerDrawable BitmapDrawable无法强制转换为android.graphics.drawable.LayerDrawable

How to cast BitmapDrawable to layer LayerDrawable? 如何将BitmapDrawable转换为图层LayerDrawable?

Edit : adding setBadgeCount function. 编辑:添加setBadgeCount函数。

public static void setBadgeCount(Context context, LayerDrawable icon, int count) {

    BadgeDrawable badge;

    // Reuse drawable if possible
    Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge);
    if (reuse != null && reuse instanceof BadgeDrawable) {
        badge = (BadgeDrawable) reuse;
    } else {
        badge = new BadgeDrawable(context);
    }

    badge.setCount(count);
    icon.mutate();
    icon.setDrawableByLayerId(R.id.ic_badge, badge);
}

You can only cast the Drawable you get from item.getIcon() to a LayerDrawable if it actually is a LayerDrawable , ie if the icon attribute in the menu definition references a drawable defined by a layer-list 您只能投出Drawable你得到item.getIcon()LayerDrawable如果它实际上是一个LayerDrawable ,即如果icon菜单定义属性引用一个定义的绘制layer-list

From the the article you mentionned : the menu definition ( menu/menu_home.xml in the article, menu/main.xml in your case) 从你的文章mentionned:菜单定义( menu/menu_home.xml中的文章, menu/main.xml你的情况)

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/action_notifications"
        android:title="Notifications"
        android:icon="@drawable/ic_menu_notifications"
        android:showAsAction="always"/>
</menu>

The icon ( drawable/ic_menu_notifications ) referenced should be a layer-list : 引用的图标( drawable/ic_menu_notifications )应该是layer-list

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item
      android:id="@+id/ic_notification"
      android:drawable="@drawable/ic_action_email"
      android:gravity="center" />
  <!-- set a place holder Drawable so android:drawable isn't null -->
  <item
      android:id="@+id/ic_badge"
      android:drawable="@drawable/ic_action_email" />
</layer-list>

Also one of the layer should have ic_badge as id. 另外一个层应该有ic_badge作为id。 This is the layer used by setBadgeCount() 这是setBadgeCount()使用的setBadgeCount()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:android.graphics.drawable.BitmapDrawable无法转换为android.graphics.drawable.LayerDrawable - java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.LayerDrawable 如何将TransitionDrawable转换为BitmapDrawable? - How to cast TransitionDrawable to BitmapDrawable? 无法在Android中将Drawable转换为LayerDrawable - Unable to cast Drawable to LayerDrawable in android Android:无法将BitmapDrawable强制转换为ClipDrawable - Android: cannot cast BitmapDrawable to a ClipDrawable Android:如何移动BitmapDrawable? - Android: How to move a BitmapDrawable? 如何将GlideBitmapDrawable强制转换为BitmapDrawable - How can cast GlideBitmapDrawable to BitmapDrawable reciprocaly IonDrawable无法转换为android.graphics.drawable.BitmapDrawable - IonDrawable cannot be cast to android.graphics.drawable.BitmapDrawable 如何在Android中使用layerDrawable实现覆盖 - How to achieve overlay with out using layerDrawable in android NinePatchDrawable无法转换为android.graphics.drawable.BitmapDrawable - NinePatchDrawable cannot be cast to android.graphics.drawable.BitmapDrawable Android LayerDrawable:如何以编程方式设置图层坐标? - Android LayerDrawable: How to set layer coordinates programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM