Code:
package com.example.guy_folk.quadrat;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.View;
public class QuadratView extends View {
private int cV = 0;
private int xMin = 0; // This view's bounds
private int xMax;
private int yMin = 0;
private int yMax;
private float ballRadius = 50;
private float bX1 = ballRadius - 50;
private float bY1 = ballRadius - 50;
private float bX2 = ballRadius + 70;
private float bY2 = ballRadius - 50;
private float bX3 = ballRadius - 170;
private float bY3 = ballRadius - 50;
private float bX4 = ballRadius - 50;
private float bY4 = ballRadius + 70;
private float bX5 = ballRadius - 50;
private float bY5 = ballRadius - 170;
private RectF bB1, bB2, bB3, bB4, bB5;
private Paint p1,p2,p3,p4,p5; // The paint (e.g. style, color) used for drawing
float scrw = getWidth()/2;
float scrh = getHeight()/2;
float qX1 = scrw;
float qY1 = scrh;
float qX2 = scrw+120;
float qY2 = scrh;
float qX3 = scrw-120;
float qY3 = scrh;
float qX4 = scrw;
float qY4 = scrh+120;
float qX5 = scrw;
float qY5 = scrh-120;
float qH = 100;
float qW = 100;
// Constructor
public QuadratView(Context context) {
super(context);
this.setFocusableInTouchMode(true);
bB1 = new RectF();
bB2 = new RectF();
bB3 = new RectF();
bB4 = new RectF();
bB5 = new RectF();
p1 = new Paint();
p2 = new Paint();
p3 = new Paint();
p4 = new Paint();
p5 = new Paint();
p1.setColor(Color.MAGENTA);
p2.setColor(Color.MAGENTA);
p3.setColor(Color.MAGENTA);
p4.setColor(Color.MAGENTA);
p5.setColor(Color.MAGENTA);
}
// Called back to draw the view. Also called by invalidate().
@Override
protected void onDraw(Canvas canvas) {
float scrw = getWidth()/2;
float scrh = getHeight()/2;
//ballX = ballX + scrw;
//ballY = ballY + scrh;
bB1.set((bX1-ballRadius)+scrw, (bY1-ballRadius)+scrh, (bX1+ballRadius)+scrw, (bY1+ballRadius)+scrh);
bB2.set((bX2-ballRadius)+scrw, (bY2-ballRadius)+scrh, (bX2+ballRadius)+scrw, (bY2+ballRadius)+scrh);
bB3.set((bX3-ballRadius)+scrw, (bY3-ballRadius)+scrh, (bX3+ballRadius)+scrw, (bY3+ballRadius)+scrh);
bB4.set((bX4-ballRadius)+scrw, (bY4-ballRadius)+scrh, (bX4+ballRadius)+scrw, (bY4+ballRadius)+scrh);
bB5.set((bX5-ballRadius)+scrw, (bY5-ballRadius)+scrh, (bX5+ballRadius)+scrw, (bY5+ballRadius)+scrh);
canvas.drawRect(bB1,p1);
canvas.drawRect(bB2,p2);
canvas.drawRect(bB3,p3);
canvas.drawRect(bB4,p4);
canvas.drawRect(bB5,p5);
update();
// Delay
try {
Thread.sleep(30);
} catch (InterruptedException e) { }
invalidate(); // Force a re-draw
}
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
float tX = event.getX();
float tY = event.getY();
if(tX >= qX1 && tX <= qX1 + qW && tY >= qY1 && tY <= qH){
cV++;
colorChange();
}
//if(cV<6){cV++;}else{ cV=0; }
//colorChange();
break;
case MotionEvent.ACTION_MOVE:
// finger moves on the screen
break;
case MotionEvent.ACTION_UP:
// finger leaves the screen
break;
}
// tell the system that we handled the event and no further processing is required
return true;
}
public void colorChange(){
switch(cV){
case 0:
p1.setColor(Color.MAGENTA);
break;
case 1:
p1.setColor(Color.BLUE);
break;
case 2:
p1.setColor(Color.RED);
break;
case 3:
p1.setColor(Color.CYAN);
break;
case 4:
p1.setColor(Color.YELLOW);
break;
case 5:
p1.setColor(Color.WHITE);
break;
}
}
// Detect collision and update the position of the ball.
private void update() {
}
// Called back when the view is first created or its size changes.
@Override
public void onSizeChanged(int w, int h, int oldW, int oldH) {
// Set the movement bounds for the ball
xMax = w-1;
yMax = h-1;
}
}