I created a Custom View CircleView like this:
public class CircleView extends LinearLayout {
Paint paint1;
public CircleView(Context context) {
super(context);
init();
}
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void init() {
paint1 = new Paint();
paint1.setColor(Color.RED);
}
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas);
canvas.drawCircle(50, 50, 25, paint1);
this.draw(canvas);
}
}
Then I included it on my Activity's layout root <RelativeLayout>
:
<com.turkidroid.test.CircleView
android:id="@+id/circle_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true" />
However, nothing was drawn!
Some Info:
com.turkidroid.test
.onDraw()
method, I tried including super.onDraw()
and commenting it.Actually, after Android 4.0 the Canvas at android. view. View is a hardware accelerated canvas, which means it is implementd by OpenGL, so you do not need to use another way for performance.
Canvas has a method to draw a rectangle, while Paint defines whether to fill that rectangle with a color or leave it empty. Simply put, Canvas defines shapes that you can draw on the screen, while Paint defines the color, style, font, and so forth of each shape you draw.
Your onDraw
method is never called, you need to call setWillNotDraw(false)
on the constructor of your Custom View in order to get onDraw
actually called.
As stated on Android SDK:
If this view doesn't do any drawing on its own, set this flag to allow further optimizations. By default, this flag is not set on View, but could be set on some View subclasses such as
ViewGroup
. Typically, if you overrideonDraw(android.graphics.Canvas)
you should clear this flag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With