Mamy już stworzony Activity wg naszych potrzeb, chcemy narysować rysunek wektorowy 2D, jak się do tego zabrać?

w sekcji OnCreate dla naszego Activity dodajemy na końcu:

1
  setContentView(new Obraz2d(this));

Musimy teraz wenątrz naszej klasy Activity utworzyć klasę Obraz2d (będzie to klasa w klasie) na przykład taką:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 public class Obraz2d extends View {
        public Obraz2d(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }

        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);
            int x = getWidth();
            int y = getHeight();
            int radius;
            radius = 120;
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.BLACK);
            canvas.drawPaint(paint);
            // Kolory można ustawiać na kilka sposobów, np. w wersji HTML
            paint.setColor(Color.parseColor("#FFFF00"));
            //Rysujemy koło, tekst itp:
            canvas.drawCircle(x / 2, y / 2, radius, paint);
            paint.setColor(Color.rgb(0,255,255));
            canvas.drawText("Słońce nad morzem! :)",x/2 - 50,10, paint);
            paint.setColor(Color.BLUE);

            canvas.drawRect(0,y-20,x,y,paint);
        }
    }