问题描述
我正在开发材质设计应用程序,我希望工具栏上的菜单图标应显示为圆形图像,而不是矩形图像 。
这是屏幕截图:
看菜单图标 ,它显示为矩形 ,但我希望它是圆形的 。
我怎样才能做到这一点?
1楼
有一个供Android使用的库,可将照片制作成圆形.... 是链接....尝试一下:)
编辑:
或者您可以使用此库: :
2楼
您可以使用此网站返回圆圈图标:
然后将其包含在项目中。
3楼
您可以使用图标生成器工具(Android Asset Studio)分两个步骤生成圆形图标。
- 向其提供选择了相关选项的图标图像,并使用下面的链接生成圆形启动器图标。
- 然后,使用上面生成的圆形启动器图标(xxxhdpi格式)并将其提供给下面的链接以生成操作栏图标。
希望这对您有所帮助。
4楼
您可以按照以下建议从以下几个不同的提供者那里学习本教程:
他们都有很好的例子和源代码来帮助您
5楼
共有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;
}
4.下载网址图片作为位图,然后裁剪
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)));
}
}));