gpt4 book ai didi

android - 在填充的 android Canvas 上绘制一个透明圆圈

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:53:43 25 4
gpt4 key购买 nike

enter image description here

我正在尝试做一些非常简单的事情(见上文)。我希望 Canvas 的所有像素都是纯色,但填充中心圆的像素除外。我已经阅读了数百篇有关此主题的堆栈溢出文章,并尝试了数百种方法,包括设置 PorterDuff.Mode。这是我当前的 MyView 的 onDraw() 扩展 View :

  protected void onDraw(Canvas canvas) {
int w = canvas.getWidth();
int h = canvas.getHeight();

Paint framePaint = new Paint();
framePaint.setStyle(Paint.Style.FILL);
framePaint.setColor(Color.BLUE);
canvas.drawRect(0, 0, w, h, framePaint);

Paint transparentPaint = new Paint();
transparentPaint.setColor(Color.TRANSPARENT);
transparentPaint.setAntiAlias(true);
canvas.drawCircle(w / 2, h / 2, (w + h) / 4, transparentPaint);
}

我是不是误会了什么,为什么我不能用透明颜料在现有像素上绘画。当我这样做时,像素保持不变。当我使用 PorterDuff 时,像素变黑。请帮忙。

最佳答案

试试这个:

public class TransparentCircle extends View {

Bitmap bm;
Canvas cv;
Paint eraser;

public TransparentCircle(Context context) {
super(context);
Init();
}

public TransparentCircle(Context context, AttributeSet attrs) {
super(context, attrs);
Init();
}

public TransparentCircle(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
Init();
}

private void Init(){

eraser = new Paint();
eraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
eraser.setAntiAlias(true);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {

if (w != oldw || h != oldh) {
bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
cv = new Canvas(bm);
}
super.onSizeChanged(w, h, oldw, oldh);
}

@Override
protected void onDraw(Canvas canvas) {

int w = getWidth();
int h = getHeight();
int radius = w > h ? h / 2 : w / 2;

bm.eraseColor(Color.TRANSPARENT);
cv.drawColor(Color.BLUE);
cv.drawCircle(w / 2, h / 2, radius, eraser);
canvas.drawBitmap(bm, 0, 0, null);
super.onDraw(canvas);
}
}

关于android - 在填充的 android Canvas 上绘制一个透明圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20589267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com