I'm trying to draw custom background to my CustomView which extends LinearLayout.
Here is my onDraw() method:
int mPositionX = 0;
int mPositionY = 0;
final int viewWidth = getMeasuredWidth();
final int viewHeight = getMeasuredHeight();
mPath.moveTo(mPositionX, mPositionY);
mPositionX = viewWidth;
mPath.lineTo(mPositionX, mPositionY);
mPositionY = viewHeight;
mPath.lineTo(mPositionX, mPositionY);
//draw bottom triangles
for(int i = 0; mPositionX >= 0; i++){
mPositionX -= HALF_BORDER_STEP;
mPositionY = i%2 == 0 ? mPositionY - BORDER_HEIGHT : mPositionY + BORDER_HEIGHT;
mPath.lineTo(mPositionX, mPositionY);
}
// move to left bottom corner
mPositionX = 0;
mPath.lineTo(mPositionX,mPositionY);
mPath.close();
canvas.clipPath(mPath);
canvas.drawPath(mPath, mCheckPaint);
super.draw(canvas);
I have 2 problems.
First of all my custom background does not shown if i won't set any background, so i did android:background="@color/transparent" in my xml.
Second problem is that, after i add some views via .addView(View view)
, my background disappeares.
I have tried to call invalidate()
but still nothing.
What do i do wrong?
To solve my problem i should have added setWillNotDraw(false);
which means that my View does drawing on its own. Android Dev Docs. After this I missed one more method calling: mPath.reset();
just before redrawin in my onDraw(Canvas canvas);
That's it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.