Answer the question
In order to leave comments, you need to log in
[Android] How to draw in Relativelayout?
Hello.
I can't figure out how to draw primitives (lines, shapes, text) in Relativelayout.
According to XML markup I have two Relativelayouts:
1. Goes to activity - layoutAll
2. Goes to layoutAll - layotTop
I want to draw a line/shape/text in layotTop.
In the lessons I found, only drawing in Activity is considered.
Can you suggest an example or template for drawing primitives in layout?
Answer the question
In order to leave comments, you need to log in
If I understood the question correctly, then it is done like this:
public class MyClass extends RelativeLayout {
private RectF mBounds = new RectF(0, 0, 0, 0);
private Paint mPaint = new Paint();
public MyClass(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
setWillNotDraw(false); //разрешаем рисовать в ViewGroup
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setAntiAlias(true);
}
@Override
protected void onSizeChanged(final int width, final int height, final int oldw,
final int oldh) {
mBounds.left = 0;
mBounds.top = 0;
mBounds.right = width;
mBounds.bottom = height;
}
@Override
public void onDraw(final Canvas canvas) {
super.onDraw(canvas);
canvas.drawOval(mBounds, mPaint);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question