Apr 11, 2012

Draw round-rect on canvas, drawRoundRect()

drawRoundRect (RectF rect, float rx, float ry, Paint paint)
Draw the specified round-rect using the specified paint. The roundrect will be filled or framed based on the Style in the paint.
  • rect - The rectangular bounds of the roundRect to be drawn
  • rx - The x-radius of the oval used to round the corners
  • ry - The y-radius of the oval used to round the corners
  • paint - The paint used to draw the roundRect

Draw round-rect on canvas, drawRoundRect()

Create a new class MyView extends View. Override the onDraw(Canvas canvas) method to draw round-rect on Canvas.
package com.AndroidMyCanvas;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

Paint paint;
Path path;

public MyView(Context context) {

public MyView(Context context, AttributeSet attrs) {
super(context, attrs);

public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

private void init(){
paint = new Paint();


protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub

final RectF rect = new RectF();

rect.set(50, 50, 150, 150);
canvas.drawRoundRect(rect, 10, 10, paint);

rect.set(200, 150, 450, 350);
canvas.drawRoundRect(rect, 30, 30, paint);

rect.set(200, 400, 450, 600);
canvas.drawRoundRect(rect, 50, 100, paint);



Modify main Java activity to setContentView() using our custom View, MyView.
package com.AndroidMyCanvas;

import android.app.Activity;
import android.os.Bundle;

public class AndroidMyCanvasActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
setContentView(new MyView(this));

No comments:

Post a Comment

Infolinks In Text Ads