我想填補一個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