问题描述
我正在尝试创建一个自定义视图,它的工作很简单:有一个由弧形路径显示的位图 - 从 0deg 到 360deg.度数随着某些 FPS 的变化而变化.
I'm trying to create a custom View which works simple: there is a Bitmap which is revealed by arc path - from 0deg to 360deg. Degrees are changing with some FPS.
所以我使用重写的 onDraw()
方法制作了一个自定义视图:
So I made a custom View with overridden onDraw()
method:
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
arcPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
canvas.drawArc(arcRectF, -90, currentAngleSweep, true, arcPaint);
arcPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, circleSourceRect, circleDestRect, arcPaint);
}
arcPaint
初始化如下:
arcPaint = new Paint();
arcPaint.setAntiAlias(true);
arcPaint.setColor(Color.RED); // Color doesn't matter
现在,一切都画得很好,但是......整个视图的背景都是黑色的.
Now, everything draws great, but... the background is BLACK in whole View.
如果我设置 canvas.drawColor(..., PorterDuff.Mode.DST)
并省略 canvas.drawBitmap()
- 在透明背景上正确绘制圆弧.
If I set canvas.drawColor(..., PorterDuff.Mode.DST)
and omit canvas.drawBitmap()
- the arc is drawn properly on transparent background.
我的问题是 - 如何设置 PorterDuff
模式以使其具有透明度?
My question is - how to set PorterDuff
modes to make it work with transparency?
当然 bitmap
是带有 alpha 通道的 32 位 PNG.
Of course bitmap
is 32-bit PNG with alpha channel.
推荐答案
PorterDuff.Mode.CLEAR
不适用于硬件加速.只需设置
PorterDuff.Mode.CLEAR
doesn't work with hardware acceleration. Just set
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
非常适合我.
这篇关于在画布上绘图 - PorterDuff.Mode.CLEAR 绘制黑色!为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!