通过Android的像素画像素圈 [英] Android Draw Circle Pixel by Pixel

查看:137
本文介绍了通过Android的像素画像素圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动用我的位图,逐像素一圈,用这个code:

I am trying to draw a circle on my bitmap, pixel by pixel, using this code:

for(int j = ((int)x - 20); j < ((int)x + 20); j++){
    for(int k = ((int)y - 20); k < ((int)y + 20); k++){

        int colorX = bitmap.getPixel((int)j, (int)k);

        if(Color.red(colorX) == 0 && Color.green(colorX) == 0 && Color.blue(colorX) == 0){

            if(Math.sqrt(((j - (( int ) x )) ^ 2 ) + ((k - (( int ) y )) ^ 2) ) <= 20)
                bitmap.setPixel(j, k, Color.YELLOW);
        }
    }

}

但有一个问题,这不是画一个圆圈..它看起来像一个三角形。

But there is a problem, this is not drawing a circle.. it looks like a triangle..

有人能帮助我吗?

非常感谢提前;)

推荐答案

你的错误是一个错误的使用^,它不是一个电运营商

your mistake is a wrong use of ^ , its not a power operator

我在评论中提及了有关XFER模式,看到这个自定义视图:

regarding xfer modes i mentioned in the comment, see this custom view:

class V extends View {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Xfermode mode = new AvoidXfermode(Color.BLACK, 0, AvoidXfermode.Mode.TARGET);

    public V(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int w = getWidth();
        float h = getHeight() / 3f;
        paint.setXfermode(null);
        paint.setColor(Color.RED);
        canvas.drawRect(0, 0, w, h, paint);
        paint.setColor(Color.BLACK);
        canvas.drawRect(0, h, w, 2*h, paint);
        paint.setColor(Color.GREEN);
        canvas.drawRect(0, 2*h, w, 3*h, paint);

        // draw the circle: it draws only in the middle black strip
        paint.setColor(Color.WHITE);
        paint.setXfermode(mode);
        canvas.drawCircle(w/2, 1.5f * h, h, paint);
    }
}

这篇关于通过Android的像素画像素圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆