Wednesday, August 24, 2011

Example of drawing on View


package com.exercise.AndroidDraw;


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 AndroidDrawActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}

public class MyView extends View {

public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);

int width = getWidth();
int height = getHeight();
int radius;

if (width > height){
radius = height/2;
}else{
radius = width/2;
}

Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);

paint.setColor(Color.RED);
canvas.drawPaint(paint);
paint.setColor(Color.BLUE);
canvas.drawCircle(width/2, height/2, radius, paint);
}

}
}

1 comment:

Timelord said...

Thank you.
I hope this is my first step to gamewriting.