2012-08-11 75 views
0

我想填补一个ImageView的一个圆圈(比圆的轮廓透明等)。Android的 - 尝试逐步填充圆底部到顶部

我的代码工作:

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发疯。我该如何利用最少量的内存进行此操作?

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

+0

你能不能用同样的'Canvas','mergedLayersBitmap'等,连续调用'的onDraw之间( )'?你仍然可以做,但失去的分配... – 2012-08-11 23:58:51

+0

为什么你不能实际使用与圆的“面具”的进度条?我认为它应该工作,你可以使进度条的方形圆角:) – 2014-02-04 10:14:44

回答

2

伪会是这个样子。

for each pixel inside CircleBitmap { 

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

这可能会很慢,但它会工作,并且圆越小,它会走得越快。

只知道的基本知识,位图的宽度和高度,例如256×256,圆半径,使事情容易让在128,128为中心的圆。然后在像素逐像素的情况下,检查像素X和Y以查看它是否落在圆圈内,并在Y限制线之下。

就用:

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

编辑:加快速度,甚至不打扰看一下上面的Y边界的像素。