Saturday, October 22, 2011

Android 2D Graphics Example

This example shows how to use onDraw() method and create a simple drawing program. The only significant files are Draw activity and the DrawView.

MainActivity.java
public class MainActivity extends Activity { 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);      
        DrawView drawView = new DrawView(this);
        setContentView(drawView);
        drawView.requestFocus();
    }
}


public class DrawView extends View {
    Paint paint = new Paint();
    Context c;
    public DrawView(Context context) {
        super(context);
        c= context;    
        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);
    }

    @Override
   public void onDraw(Canvas canvas) {
            canvas.drawCircle(100, 100, 50, paint);
    }
}
}