Android的 - 尝试逐步填充圆底部到顶部 [英] Android - Trying to gradually fill a circle bottom to top

查看:218
本文介绍了Android的 - 尝试逐步填充圆底部到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图填补一个ImageView的一个圆圈(比圆的轮廓透明的除外)。

I'm trying to fill a round circle (transparent other than the outline of the circle) in an ImageView.

我有code工作:

public void setPercentage(int p) {
    if (this.percentage != p ) {
   this.percentage = p;
    this.invalidate();
   }
}
@Override public void onDraw(Canvas canvas) {
 Canvas tempCanvas;
        Paint paint;    
        Bitmap bmCircle = null;
        if (this.getWidth() == 0 || this.getHeight() == 0 ) 
            return ; // nothing to do
        mergedLayersBitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888); 

        tempCanvas = new Canvas(mergedLayersBitmap);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setFilterBitmap(false);



        bmCircle = drawCircle(this.getWidth(), this.getHeight());

        tempCanvas.drawBitmap(bmCircle, 0, 0, paint);


        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

        tempCanvas.clipRect(0,0, this.getWidth(), (int) FloatMath.floor(this.getHeight() - this.getHeight() * ( percentage/100)));
        tempCanvas.drawColor(0xFF660000, PorterDuff.Mode.CLEAR);  

        canvas.drawBitmap(mergedLayersBitmap, null, new RectF(0,0, this.getWidth(), this.getHeight()), new Paint());
        canvas.drawBitmap(mergedLayersBitmap, 0, 0, new Paint());

    }
  static Bitmap drawCircle(int w, int h) {
        Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);

        p.setColor(drawColor);    
        c.drawOval(new RectF(0, 0, w, h), p);
        return bm;

    }

它种工作。不过,我有两个问题:我很快耗尽内存和GC去疯狂。我如何可以利用的内存量最少此操作?

It kind of works. However, I have two issues: I run out of memory quickly and the GC goes crazy. How can I utilize the least amount of memory for this operation?

我知道我不应该来的onDraw实例化对象,但我不知道在哪里画呢。谢谢你。

I know I Shouldn't be instantiating objects in onDraw, however I'm not sure where to draw then. Thank you.

推荐答案

伪会是这个样子。

    for each pixel inside CircleBitmap {

        if (pixel.y is < Yboundary && pixelIsInCircle(pixel.x, pixel.y)) {
           CircleBitmap .setPixel(x, y, Color.rgb(45, 127, 0));
        }
    }

这可能是缓慢的,但它会工作,更小的圈子会去得更快。

that may be slow, but it would work, and the smaller the circle the faster it would go.

刚知道的基本知识,位图的宽度和高度,例如256×256,圆的半径,使事情容易让在128,128中心的圆。然后你通过像素像素去,检查像素X和Y,看它是否落在圆内,而Y限制线以下。

just know the basics, bitmap width and height, for example 256x256, the circles radius, and to make things easy make the circle centered at 128,128. then as you go pixel by pixel, check the pixels X and Y to see if it falls inside the circle, and below the Y limit line.

然后只需使用:

    CircleBitmap .setPixel(x, y, Color.rgb(45, 127, 0));

编辑:加快速度,甚至不打扰看着上面在Y极限像素

edit: to speed things up, don't even bother looking at the pixels above the Y limit.

这篇关于Android的 - 尝试逐步填充圆底部到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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