Draw primitive shapes such as rectangle, circle etc in ANDROID, A simple example.

By | February 12, 2011

The following example shows how to draw a rectangle using the Paint Class in ANDROID. Just copy and paste the following code to your file and you are done.

package Draw2.pack;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;

public class Draw2 extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(new myView(this));
  }

  private class myView extends View{

      public myView(Context context) {
          super(context);
      }

      @Override
      protected void onDraw(Canvas canvas) {

          Paint myPaint = new Paint();
          myPaint.setColor(Color.GREEN);
          myPaint.setStyle(Paint.Style.STROKE);
          myPaint.setStrokeWidth(3);
          canvas.drawRect(10, 10, 100, 100, myPaint);
      }
  }
}
Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Leave a Reply

Your email address will not be published. Required fields are marked *