Reputation: 3573
I have a RelativeLayout that I want to contain 4 concentric circles (a radar effect, if you will). I'd like to be able to hide and display the RelativeLayout, so I'm wondering how exactly to use Canvas and Paint to draw a circle to the RelativeLayout. If someone could explain the lifecycle of how it works, that would be very helpful. Right now I've got:
setContentView(R.layout.applayout);
myRelLayout = (RelativeLayout) findViewById(R.id.RLRadar);
Canvas canvas = new Canvas();
Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setColor(0xFF000000);
circlePaint.setStyle(Style.STROKE);
canvas.drawCircle((float) centerX, (float) centerY, (float) (maxRadius / 4), circlePaint);
Am I on the right track? Do I need to convert the canvas to some sort of ImageView and add that to the RelativeLayout? Or am I completely off base here?
Thanks for any help!
Edit: Here's the working code.
// Add the radar to the RadarRL
Picture picture = new Picture();
Canvas canvas = picture.beginRecording(screenWidth, screenHeight);
// Draw on the canvas
Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setColor(0xFF000000);
circlePaint.setStyle(Style.STROKE);
circlePaint.setStrokeWidth((float) 5.0);
canvas.drawCircle((float) centerX, (float) centerY, (float) (maxRadius / 4), circlePaint);
canvas.drawCircle((float) centerX, (float) centerY, (float) ((3 * maxRadius) / 4), circlePaint);
canvas.drawCircle((float) centerX, (float) centerY, (float) (maxRadius / 2), circlePaint);
canvas.drawCircle((float) centerX, (float) centerY, (float) (maxRadius), circlePaint);
picture.endRecording();
Upvotes: 1
Views: 13279
Reputation: 680
If you don't want to create a custom view, you can use setBackgroundDrawable. For example, using a Picture and a PictureDrawable:
// Create the picture.
Picture picture = new Picture();
Canvas canvas = beginRecording(WIDTH, HEIGHT);
// Draw on the canvas.
picture.endRecording();
// Now set it as the background drawable.
PictureDrawable drawable = new PictureDrawable(picture);
relativeLayout = (RelativeLayout) findViewById(R.id.RLRadar);
relativeLayout.setBackgroundDrawable(drawable);
Upvotes: 2
Reputation: 73484
You have to create a Custom View and override the View.draw(Canvas) method. You can then use the canvas parameter to draw the radar effect. The view can then be placed in any layout.
Upvotes: 2