【问题标题】:android draw circle badge on top on bitmapandroid在位图顶部绘制圆形徽章
【发布时间】:2013-10-29 09:04:31
【问题描述】:

我想在应用程序的菜单图标上方添加一个带有待处理事件数量的徽章,但正如您在下图中看到的那样,我遇到了一些问题。我无法使圆圈不透明,我不想看到圆圈后面的灰线,并且新图像在顶部被剪切

我想在操作栏的徽标图标(主页图标)上使用它,而不是在操作栏的任何其他菜单项中使用,因此自定义操作视图不是一个选项

    Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId);
    bm = bm.copy(bm.getConfig(), true);

    Paint circlePaint = new Paint();
    circlePaint.setAntiAlias(true);
    circlePaint.setColor(Color.RED);
    circlePaint.setStyle(Paint.Style.FILL);
    //circlePaint.setAlpha(255);

    Paint textPaint = new Paint(); 
    textPaint.setStyle(Style.FILL);  
    textPaint.setColor(Color.WHITE); 
    textPaint.setTextSize(23); 
    textPaint.setFakeBoldText(true);

    Canvas canvas = new Canvas(bm);
    canvas.drawCircle(bm.getWidth()/6, bm.getHeight()/5, 13, circlePaint);
    canvas.drawText(text, bm.getWidth()/7 + 5, bm.getHeight()/3, textPaint);

    return new BitmapDrawable(context.getResources(), bm);

任何帮助将不胜感激!

谢谢

【问题讨论】:

标签: android canvas bitmap badge


【解决方案1】:

你正在寻找的东西叫做Android View badger,你应该检查这个可爱的TUTORIALthis ...我相信它会帮助你很多...享受:) :)

【讨论】:

  • 我想在操作栏的徽标图标(主页图标)上使用它,而不是在操作栏的任何其他菜单项中使用它。我尝试使用 View badger 但没有显示任何内容
  • 上面的链接说目前不支持徽章操作栏项目。他需要操作栏项目上的图标。
  • +1 用于 ViewBadger“教程”链接(实际上是 Git hub 存储库)。不错的图书馆。
  • github 链接不采用 menuItem 上下文。
【解决方案2】:

我认为你应该使用相对布局来做这些事情,而不是从画布中绘制。

这里是nice tutorial 如何使用相对布局

编辑:

对于ActionBar

【讨论】:

【解决方案3】:

在 res/menu 中创建一个菜单项并将 actionLayout 设置为您的布局

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/badge"
        android:actionLayout="@layout/actionbar_badge_layout"
        android:icon="@drawable/icn_menu_posts"
        android:showAsAction="always">
    </item>
</menu>

和你的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="48dp"
    android:layout_height="fill_parent"
    android:layout_gravity="right" >

    <!-- Menu Item Image -->
    <ImageView
        android:layout_width="48dp"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:src="@drawable/bkg_actionbar_notify_off" />

    <!-- Badge Count -->    
    <TextView
        android:id="@+id/actionbar_notifcation_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:padding="@dimen/padding_small"
        android:text="99"
        android:textColor="@color/holo_orange_dark" />

</RelativeLayout>

然后在你的活动的 onCreateOptionsMenu 中做这样的事情......

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.badge, menu);

    RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
    TextView tv = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
    tv.setText("12");
}

也可以查看answer

【讨论】:

  • 我忘了说它不是操作栏中的普通菜单项,我想将徽章添加到主页图标
猜你喜欢
  • 2014-05-16
  • 2015-03-31
  • 1970-01-01
  • 2012-05-04
  • 2014-09-02
  • 2017-07-29
  • 2012-02-14
  • 2021-03-08
  • 1970-01-01
相关资源
最近更新 更多