我想填补一个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有实例化对象,但我不知道在哪里画呢。谢谢。
你能不能用同样的'Canvas','mergedLayersBitmap'等,连续调用'的onDraw之间( )'?你仍然可以做,但失去的分配... – 2012-08-11 23:58:51
为什么你不能实际使用与圆的“面具”的进度条?我认为它应该工作,你可以使进度条的方形圆角:) – 2014-02-04 10:14:44