Rand auf Canvas Zeichnen
- Home
- Tutorials
- Android
- Spiele App für Android 2.3.6 Programmieren
- Rand auf Canvas Zeichnen
Zuerst legen wir eine globale Integer-Variable "currentPaintNum" und vier globale Paint-Objekte an...
private int currentColorNum;
private Paint paintRed, paintBlue, paintGreen, paintYellow;
private Paint currentColor;
... und fügen folgenden Code in den Konsturktor ein:
Random rnd = new Random();
currentColorNum = rnd.nextInt(4);
Das soll dafür sorgen, dass die Farbe die der Rand hat zu Beginn zufällig gewählt wird.
Außerdem müssen wir eine Methode erzeugen in der wir die Farben zuordnen die wir als Rand haben möchten.
public void setColors(){
Paint paintRed = new Paint();
paintRed.setARGB(255, 236, 27, 36);
this.paintRed = paintRed;
Paint paintBlue = new Paint();
paintBlue.setARGB(255, 36, 72, 204);
this.paintBlue = paintBlue;
Paint paintGreen = new Paint();
paintGreen.setARGB(255, 34, 177, 76);
this.paintGreen = paintGreen;
Paint paintYellow = new Paint();
paintYellow.setARGB(255, 255, 242, 0);
this.paintYellow = paintYellow;
}
Diese Methode (setColors() ) müssen wir nun noch im Konstruktor aufrufen, damit den Farben beim erzeugen der Activity die gewünschten ARGB - Werte zugeordnet werden.
setColors();
In der Methode drawLines() setzen wir die Ränder links, rechts und unten. dazu sind ein Paint und ein Canvas die Parameter.
public void drawLines(Paint lineColor, Canvas canvas){
int lineWidth = 10;
int screenHeight = getHeight();
int screenWidth = getWidth();
canvas.drawRect(0, 0, lineWidth, getHeight(), lineColor);
canvas.drawRect(0, getHeight() - lineWidth, screenWidth, screenHeight, lineColor);
canvas.drawRect(screenWidth - lineWidth, 0, screenWidth, screenHeight, lineColor);
currentColor = lineColor;
}
Hierbei sollen beim Aufruf dieser Methode drei Vierecke gezeichnet werden. Diese Vierecke entsprechen unserem farbigen Rand.
Nun noch die passende Farbe auf den Canvas malen in dem wir das in onDraw() einfügen:
if (currentColorNum == 0) {
drawLines(paintBlue, canvas);
} else if (currentColorNum == 1) {
drawLines(paintRed, canvas);
} else if (currentColorNum == 2) {
drawLines(paintGreen, canvas);
} else if (currentColorNum == 3) {
drawLines(paintYellow, canvas);
}
Wobei, je nachdem welchen Wert currentPaintNum hat, der Rand in der zu der Nummer passende Farbe gemalt werden soll.
Damit sich die Sprites nicht in den Rand hineinbewegen müssen wir noch Änderungen an der Sprite Klasse vornehmen. Hierbei ändern wir sowohl die mögliche Startposition des Sprites als auch die Stelle an der die Sprites abprallen sollen. Also 10 Pixel vom ursprünglichen Wert entfernt.
package com.panjutorials.lazypudding;
import java.util.Random;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
public class Sprite {
static final private int BMP_COLUMNS = 4;
static final private int BMP_ROWS = 4;
private int[] DIRECTION_TO_SPRITE_SHEET = { 2, 0, 3, 1 };
private int x;
private int y;
private int xSpeed;
private int ySpeed;
private int width;
private int height;
private Bitmap bmp;
private GameView theGameView;
private int currentFrame=0;
public Sprite(GameView theGameView, Bitmap bmp) {
this.theGameView = theGameView;
this.bmp = bmp;
this.width = bmp.getWidth() / BMP_COLUMNS;
this.height = bmp.getHeight() / BMP_ROWS;
Random rnd = new Random();
x=10 + rnd.nextInt(theGameView.getWidth()- width -20);
y=10 + rnd.nextInt(theGameView.getHeight() - height - 20);
ySpeed = rnd.nextInt(10) - 4;
xSpeed = rnd.nextInt(10) - 4;
}
private void bounceOff() {
if (x + 10 > theGameView.getWidth() - width - xSpeed || x + xSpeed < 10) {
xSpeed = -xSpeed;
}
x = x + xSpeed;
if (y + 10> theGameView.getHeight() - height - ySpeed || y + ySpeed < 0) {
ySpeed = -ySpeed;
}
y = y + ySpeed;
currentFrame = ++currentFrame % BMP_COLUMNS;
}
public void onDraw(Canvas canvas) {
bounceOff();
int sourceX = currentFrame * width;
int sourceY = getAnimationRow() * height;
Rect source = new Rect(sourceX, sourceY, sourceX + width, sourceY + height);
Rect destine = new Rect(x, y, x + width, y + height);
canvas.drawBitmap(bmp, source, destine, null);
}
private int getAnimationRow() {
double directionDouble = (Math.atan2(xSpeed, ySpeed) / (Math.PI / 2)+2);
int spriteDir = (int) Math.round(directionDouble) % BMP_ROWS;
return DIRECTION_TO_SPRITE_SHEET[spriteDir];
}
public boolean isTouched(float x2, float y2) {
return x2 > x && x2 < x + width && y2 > y && y2 < y + height;
}
}
Spiele App für Android 2.3.6 Programmieren
-
Android 2.3.6 Spiele App Programmieren
-
Lektion1.1
-
Lektion1.2
-
Lektion1.3
-
Lektion1.4
-
Lektion1.5
-
Lektion1.6
-
Lektion1.7
-
Lektion1.8
-
Lektion1.9
-
Lektion1.10
-
Lektion1.11
-
Lektion1.12
-
Lektion1.13
-
Lektion1.14
-
Lektion1.15
-
Lektion1.16
-
Lektion1.17
-
Lektion1.18
-
Lektion1.19
-
Lektion1.20
-
Lektion1.21
-
Lektion1.22
-
Lektion1.23
-
Lektion1.24
-
Lektion1.25
-
Lektion1.26
-
Lektion1.27
-
Lektion1.28
-
Lektion1.29
-
Lektion1.30
-
Lektion1.31
-
Lektion1.32
-
Lektion1.33
-
Lektion1.34
-
Lektion1.35
-
Lektion1.36
-
Lektion1.37
-
Lektion1.38
-
Lektion1.39
-