Touch-Position Abfragen in einem Quadrat?

HighGuy

HighGuy

Neues Mitglied
0
Guten Tag liebe Community,
dies ist mein erster Post und ich würde mich auch gerne erstmal vorstellen.
Ich bin 17 Jahre alt, heiße Joshi und komme aus Hamburg.

Nun komme ich mal zu meinem Anliegen, ich probiere mich in der letzten Zeit ein wenig in 'Grafischer Darstellung', ich habe eine kleine App geschrieben in welcher 5 Quadrate in einem Kreuz abgebildet sind.
Ich möchte gerne bewirken, dass wenn ich auf eines dieser Quadrate klicke, dass dann etwas passiert, zum Beispiel sich die Farbe ändert.
Mein Problem an dieser Stelle ist, dass ich nicht genau weiß wieso meine Abfrage für das "anklicken" nicht funktioniert, habe es momentan nur für ein einziges Quadrat für den Test.

Oder gibt es noch eine andere Methode dies zu abzufragen?

Hier der Code:
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;
    }
}


MfG Joshi


PS: Frohes neues Jahr euch allen!!!
 
Zuletzt bearbeitet:
1.

Code:
if(tX >= qX1 && tX <= qX1 + qW && tY >= qY1 && tY <=[B][COLOR="Red"]qY1+[/COLOR][/B] qH)

2.

@Override vor dem onTouch()!!!
 
Ich habe es nun schon selber hinbekommen, trotzdem vielen dank schonmal für die hilfreiche Antwort!!!!

Hier der Code:
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.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;

public class QuadratView extends View {
    private int cV = 0;
    private int qV = 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, bBR;
    private Paint p1,p2,p3,p4,p5,pR;           // The paint (e.g. style, color) used for drawing#

    DisplayMetrics metrics = this.getResources().getDisplayMetrics();
    float scrw = metrics.widthPixels/2;
    float scrh = metrics.heightPixels/2;

    float uX1 = (bX1-ballRadius)+scrw;
    float uY1 = (bY1-ballRadius)+scrh;
    float uX2 = (bX2-ballRadius)+scrw;
    float uY2 = (bY2-ballRadius)+scrh;
    float uX3 = (bX3-ballRadius)+scrw;
    float uY3 = (bY3-ballRadius)+scrh;
    float uX4 = (bX4-ballRadius)+scrw;
    float uY4 = (bY4-ballRadius)+scrh;
    float uX5 = (bX5-ballRadius)+scrw;
    float uY5 = (bY5-ballRadius)+scrh;
    float uXR = 10;
    float uYR = 10;

    float eX1 = (bX1+ballRadius)+scrw;
    float eY1 = (bY1+ballRadius)+scrh;
    float eX2 = (bX2+ballRadius)+scrw;
    float eY2 = (bY2+ballRadius)+scrh;
    float eX3 = (bX3+ballRadius)+scrw;
    float eY3 = (bY3+ballRadius)+scrh;
    float eX4 = (bX4+ballRadius)+scrw;
    float eY4 = (bY4+ballRadius)+scrh;
    float eX5 = (bX5+ballRadius)+scrw;
    float eY5 = (bY5+ballRadius)+scrh;
    float eXR = 50;
    float eYR = 50;

    // 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();
        bBR = new RectF();
        p1 = new Paint();
        p2 = new Paint();
        p3 = new Paint();
        p4 = new Paint();
        p5 = new Paint();
        pR = new Paint();
        p1.setColor(Color.MAGENTA);
        p2.setColor(Color.MAGENTA);
        p3.setColor(Color.MAGENTA);
        p4.setColor(Color.MAGENTA);
        p5.setColor(Color.MAGENTA);
        pR.setColor(Color.BLUE);
    }

    // Called back to draw the view. Also called by invalidate().
    @Override
    protected void onDraw(Canvas canvas) {
        scrw = getWidth()/2;
        scrh = getHeight()/2;

        bB1.set(uX1, uY1, eX1, eY1);
        bB2.set(uX2, uY2, eX2, eY2);
        bB3.set(uX3, uY3, eX3, eY3);
        bB4.set(uX4, uY4, eX4, eY4);
        bB5.set(uX5, uY5, eX5, eY5);
        bBR.set(5,5,50,50);

        canvas.drawRect(bB1,p1);
        canvas.drawRect(bB2,p2);
        canvas.drawRect(bB3,p3);
        canvas.drawRect(bB4,p4);
        canvas.drawRect(bB5,p5);
        canvas.drawRect(bBR,pR);

        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){
                //    if(cV<6){cV++;}else{cV=0;}
                //    colorChange();
                //}
                if(tX >= uX1 && tY >= uY1 && tX <= eX1 && tY <= eY1){
                    qV = 1;
                    colorChange();
                }else if(tX >= uX2 && tY >= uY2 && tX <= eX2 && tY <= eY2){
                    qV = 2;
                    colorChange();
                }else if(tX >= uX3 && tY >= uY3 && tX <= eX3 && tY <= eY3) {
                    qV = 3;
                    colorChange();
                }else if(tX >= uX4 && tY >= uY4 && tX <= eX4 && tY <= eY4){
                    qV = 4;
                    colorChange();
                }else if(tX >= uX5 && tY >= uY5 && tX <= eX5 && tY <= eY5){
                    qV = 5;
                    colorChange();
                }else if(tX >= uXR && tY >= uYR && tX <= eXR && tY <= eYR){
                    qV = 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(qV){
            case 0:
                p1.setColor(Color.MAGENTA);
                p2.setColor(Color.MAGENTA);
                p3.setColor(Color.MAGENTA);
                p4.setColor(Color.MAGENTA);
                p5.setColor(Color.MAGENTA);
            break;
            case 1:
                p1.setColor(Color.GREEN);
            break;
            case 2:
                p2.setColor(Color.GREEN);
            break;
            case 3:
                p3.setColor(Color.GREEN);
            break;
            case 4:
                p4.setColor(Color.GREEN);
            break;
            case 5:
                p5.setColor(Color.GREEN);
            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;
    }
}
 
ganz schön viele variablen
 
Ja...mein "schreibstil" ist nicht sonderlich "übersichtlich" oder "kurz gehalten" jedoch reicht es für mich bzw. den Anfang =)

Ich hätte jedoch noch eine weitere Frage:
Wie ist es möglich, drei Grafiken(Bsp: 3 Quadrate,Ellipsen)
um ein 4 Kreisen zu lassen?

Mit einem einzigem geht dies einfach mit Sin/Cos
Code:
V = 0

protected void onDraw(Canvas canvas){
V++;
scw = getWidth()
sch = getHeight()

canvas.drawRect(Math.sin(V)+scw, Math.cos(V)+sch, Math.sin(V)+scw, Math.cos(V)+sch)

}

Zumindest wenn ich mich richtig entsinne.
Aber schwierig wird es mit mehreren Objekten, wie erreiche ich, dass sie sich in gleichem Abstand von einander, wie in einem Orbit um das vierte Objekt bewegen/rotieren?
 
Zuletzt bearbeitet:

Ähnliche Themen

B
Antworten
3
Aufrufe
1.314
swa00
swa00
OnkelLon
Antworten
13
Aufrufe
2.011
OnkelLon
OnkelLon
Zurück
Oben Unten