【问题标题】:How can I make a circular menu icon? Please see details如何制作圆形菜单图标?请看详情
【发布时间】:2016-01-16 07:10:57
【问题描述】:

我正在开发一个 ma​​terial design 应用程序,我希望工具栏上的菜单图标应该显示为 圆形图像 而不是 矩形图像.

截图如下:

查看菜单图标,它显示为矩形,但我希望它是圆形

我该怎么做?

【问题讨论】:

  • 使用圆形图像视图库
  • 你能展示一张圆形菜单按钮的图片吗?
  • @TimCastelijns 但如何使用它来更改菜单图标的布局?
  • 如果图书馆没有帮助您......请在此处查看答案:stackoverflow.com/questions/28166664/…

标签: android material-design android-menu android-icons


【解决方案1】:

有4个步骤:

1.创建菜单项

<menu xmlns:tools="http://schemas.android.com/tools"
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/profile_menu_item"
        android:icon="@drawable/prof"
        app:showAsAction="always"
        tools:ignore="MenuTitle"/>
</menu>

2。检索菜单项

private MenuItem mProfileMenuItem;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.main, menu);
        mProfileMenuItem = menu.findItem(R.id.profile_menu_item);
        return true;
    }

3.将位图裁剪为圆形位图

public Bitmap getCroppedBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
            bitmap.getWidth() / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
    //return _bmp;
    return output;
}

source

4.将 url 图片下载为位图,然后裁剪

new Handler().post(() -> Glide.with(getApplicationContext()).asBitmap().load(url).into(new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
            if (mProfileMenuItem == null) return;
            mProfileMenuItem.setIcon(new BitmapDrawable(getResources(), Utils.getCroppedBitmap(resource)));
        }
    }));

source

【讨论】:

    【解决方案2】:

    您可以通过 SO 推荐的几个不同提供商的教程阅读以下教程

    Android Developer
    Vogella
    AndroidHive

    他们都有很好的例子和源代码来帮助你

    【讨论】:

    • 这不是我想要的。实际上,“图标”将是用户设置的“个人资料图片”。所以,我希望每张照片都应该在那里变成圆形。
    【解决方案3】:

    您可以使用图标生成器工具 (Android Asset Studio) 分两步生成圆形图标。

    1. 为其提供选择了相关选项的图标图像,并使用下面的链接生成一个圆形启动器图标。

    https://romannurik.github.io/AndroidAssetStudio/icons-launcher.html

    1. 然后使用上面生成的圆形启动器图标(xxxhdpi 格式)并将其提供给下面的链接以生成操作栏图标。

    https://romannurik.github.io/AndroidAssetStudio/icons-actionbar.html

    希望对你有所帮助。

    【讨论】:

    • 这不是我想要的。实际上,“图标”将是用户设置的“个人资料图片”。所以,我希望每张照片都应该在那里变成圆形。
    【解决方案4】:

    您可以使用此网站返回圆圈图标:

    Android Asset Studio

    然后将其包含在项目中。

    【讨论】:

      【解决方案5】:

      有一个 android 库可以使照片成为圆形.... https://github.com/hdodenhof/CircleImageView 这是链接....试试看:)

      编辑:
      或者你可以使用这个库: https://github.com/Pkmmte/CircularImageView

      【讨论】:

      • 问题是如何使用这些库来改变菜单图标的布局?
      猜你喜欢
      • 2016-07-31
      • 1970-01-01
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-21
      相关资源
      最近更新 更多