Draw a circle on the view (android)

I start with the first attempts to write an Android application. I would like to imagine the Monte Carlo approximation for pi. Therefore, at first I want to draw a circle in appearance, but I do not get its work! I tried to create my own CircleView class, which extends the โ€œViewโ€ and overwrites the onDraw (..) method, as described here: How to draw a circle on a canvas in Android?

This is my CircleView class.

public class CircleView extends View { public CircleView(Context context) { super(context); } protected void onDraw(Canvas canvas){ super.onDraw(canvas); Paint paint = new Paint(); paint.setColor(150); canvas.drawCircle(50,50,20,paint); } } 

I inserted CircleView in LinearLayout with the following XML

 <com.tak3r07.montecarlopi.CircleView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/circleView" android:layout_weight="1"/> 

(Btw Android Studio tells me in the XML view on the right side: "Rendering problems. The custom CircleView does not use View 2 or 3 argument constructors, XML attributes will not work.")

The application simply crashes with the following log: http://pastebin.com/Gv1GaHtX

Can someone say what I did wrong?

I thought this setting would create activity with a view that displays a circle.

Hello

Edit: The crash is fixed by adding 2 and 3 parameter constructor to CircleView (see https://stackoverflow.com/a/2129605/289684/ ... )

But now I still donโ€™t see any circle in activity

+7
source share
2 answers

A few observations:

When determining the point and radius of a circle, you must consider the width and height assigned to your view.

You must consider the indentation assigned to your view so that you do not draw this reserved part.

You should avoid highlighting objects inside your onDraw method, as this is caused by a large number.

In order for your view to be indicated in the XML layout, you need to provide a constructor that takes a context and a set of attributes. AttributeSet is the mechanism by which your XML attributes are passed to your view.

Try:

 package com.tak3r07.montecarlopi; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; public class CircleView extends View { private static final int DEFAULT_CIRCLE_COLOR = Color.RED; private int circleColor = DEFAULT_CIRCLE_COLOR; private Paint paint; public CircleView(Context context) { super(context); init(context, null); } public CircleView(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } private void init(Context context, AttributeSet attrs) { paint = new Paint(); paint.setAntiAlias(true); } public void setCircleColor(int circleColor) { this.circleColor = circleColor; invalidate(); } public int getCircleColor() { return circleColor; } protected void onDraw(Canvas canvas) { super.onDraw(canvas); int w = getWidth(); int h = getHeight(); int pl = getPaddingLeft(); int pr = getPaddingRight(); int pt = getPaddingTop(); int pb = getPaddingBottom(); int usableWidth = w - (pl + pr); int usableHeight = h - (pt + pb); int radius = Math.min(usableWidth, usableHeight) / 2; int cx = pl + (usableWidth / 2); int cy = pt + (usableHeight / 2); paint.setColor(circleColor); canvas.drawCircle(cx, cy, radius, paint); } } 
+13
source

You can create a circular layout, and inside this view, each child should be rounded:

 public class CircleView extends FrameLayout { private Bitmap maskBitmap; private Paint paint, maskPaint; public CircleView(Context context) { super(context); init(); } public CircleView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CircleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public CircleView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(); } private void init() { paint = new Paint(Paint.ANTI_ALIAS_FLAG); maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); setWillNotDraw(false); } @Override public void draw(Canvas canvas) { Bitmap offscreenBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); Canvas offscreenCanvas = new Canvas(offscreenBitmap); super.draw(offscreenCanvas); if (maskBitmap == null) { maskBitmap = createMask(getWidth(), getHeight()); } offscreenCanvas.drawBitmap(maskBitmap, 0f, 0f, maskPaint); canvas.drawBitmap(offscreenBitmap, 0f, 0f, paint); } private Bitmap createMask(int width, int height) { Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8); Canvas canvas = new Canvas(mask); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.WHITE); canvas.drawRect(0, 0, width, height, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); canvas.drawRoundRect(new RectF(0, 0, width, height), width/2f, height/2f, paint); return mask; } } 
0
source

Source: https://habr.com/ru/post/975606/


All Articles