当在安卓的活动中为ImageView
添加自适应图标时,它似乎采用了与原始设备制造商设计相同的版本。在我的例子中,现在是四舍五入的版本。但我想在我的主活动中将其显示为图标,因此希望使用例如圆角的正方形版本。如果这是可能的,我如何实现这一点?如果这是不可能的,我可以创建一个新的资源,但它需要使用ic_launcher_background
和ic_launcher_foreground
,这样图标就不会在多个地方定义。
这是我的ic_launcher.xml
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
这是我的ImageView
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
发布于 2020-08-11 20:07:20
在直接讲述解决方案之前,您应该知道系统如何在图像视图中绘制自适应图标。它主要包含三个步骤。
使用蒙版绘制透明区域background.
如果您将自适应图标放入ImageView
,则此步骤将自动完成。因此,上面将有一个系统定义的掩码。所以你的问题是如何在那里绘制一个定制的蒙版。然后让我们看看如何实现这一点。
Drawable rawDrawable = getResources().getDrawable(R.mipmap.ic_launcher, null);
Drawable foreground = rawDrawable.getForeground();
Drawable background = rawDrawable.getBackground();
Bitmap bitmap = Bitmap.createBitmap(bitmapSize, bitmapSize, Bitmap.Config.ARGB_8888);
Canvas
。 Canvas canvas = new Canvas(bitmap);
background.setBounds(0, 0, bitmapSize, bitmapSize);
background.draw(canvas);
foreground.setBounds(0, 0, bitmapSize, bitmapSize);
foreground.draw(canvas);
Bitmap maskBitmap = Bitmap.createBitmap(bitmapSize, bitmapSize, Bitmap.Config.ARGB_8888);
Canvas maskCanvas = new Canvas(maskBitmap);
Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
xferPaint.setStyle(Paint.Style.FILL_AND_STROKE);
xferPaint.setColor(Color.RED);
maskCanvas.drawRoundRect(0,0,bitmapSize, bitmapSize, 12, 12, xferPaint);
xferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawBitmap(maskBitmap, 0, 0, xferPaint);
然后
ImageView
, ((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);
https://stackoverflow.com/questions/62613293
复制相似问题