|
You last visited: Today at 02:28
Advertisement
Touch Position in einem Radius abfragen?
Discussion on Touch Position in einem Radius abfragen? within the General Coding forum part of the Coders Den category.
12/31/2014, 19:50
|
#16
|
elite*gold: 0
Join Date: Feb 2013
Posts: 1,137
Received Thanks: 869
|
War nur ne kleine Anmerkung meinerseits - wird hier nicht benötigt.
Hier mal grafisch veranschaulicht:
|
|
|
12/31/2014, 19:52
|
#17
|
elite*gold: 31
Join Date: Jan 2014
Posts: 310
Received Thanks: 55
|
Normalerweise ist der Ursprung glaube ich in der unteren linken Ecke, also musst du nur prüfen ob x >= qx && x <= qx + qw && y >= qy && y <= ql. qx/qy ist die Position des Quadrats und qw/ql die Breite und die Länge.
|
|
|
12/31/2014, 19:55
|
#18
|
elite*gold: 260
Join Date: Sep 2012
Posts: 206
Received Thanks: 13
|
Beim der Erstellung des Quadrates benutze ich die Methode "canvas.drawRect(...)" wie dem auch sei, ich gebe dort als aller erstes den Punkt oben links an, könnte ich daraus schließen, dass dies der Ursprung ist?
Ich habe es nun heute ausprobiert jedoch funktioniert es nicht, woran könnte dies liegen? Sobald ich das bestimmte Quadrat berühre, passiert nichts.
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;
}
}
|
|
|
01/01/2015, 20:13
|
#19
|
elite*gold: 260
Join Date: Sep 2012
Posts: 206
Received Thanks: 13
|
Wäre es nicht 'logischer' wenn man die Formel in etwa so darstellt?
Code:
if(tX >= uX && tY >= uY && tX <= eX && tY <= tY){
...
}
tX/tY = touch X/Y
uX/uY = Ursprung X/Y
eX/eY = quadrat end X/Y
Ich habe es nun hinbekommen, falls sich irgendwie für den Code interessiert, poste ich ihn hier mal hinein:
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 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#
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 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;
// 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) {
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);
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){
// if(cV<6){cV++;}else{cV=0;}
// colorChange();
//}
if(tX >= uX1 && tY >= uY1 && tX <= eX1 && tY <= eY1){
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;
}
}
|
|
|
|
|
Similar Threads
|
Wie realisiere ich 2 $_GET Abfragen in einem Link?
10/28/2013 - Web Development - 2 Replies
^this.
|
Casino - falsche base position (beim warpen landet man auf einem berg) o.o
05/14/2012 - Metin2 Private Server - 8 Replies
Wie schon gesagt ich hab die Casino map, die ursprünglich von Nw3 bekannt ist versucht einzufügen wobei folgendes heraus kamhttp://img3.fotos-hochladen.net/uploads/bild392 1cywf9dila.jpg
wenn ihr eine Lösung für mein Problem findet ist euch das thx schon sicher ;)
Ps: Sufu hab ich leider nichts gefunden! ;)
|
PSC - Guthaben abfragen geht auf einem PC nicht
07/25/2011 - Technical Support - 2 Replies
Gutem Tag, wie die Überschrift schon sagt, kann ich das Guthaben einer PSC auf meinem Hauptrechner nicht kontrollieren immer wenn ich es versuche bekomme ich folgende Fehlermeldung:
Bei der Eingabe des PIN-Codes oder Passworts ist ein Fehler aufgetreten. Bitte korrigiere deine Eingabe. Hast du ein Passwort für diese paysafecard vergeben, kontaktiere bitte unser Service-Team.
------------------------------------------------
Wenn ich das ganze von meinem anderen Rechner ausprobiere...
|
Position von einem spieler
03/01/2008 - Kal Online - 9 Replies
ist es vielleicht möglich das man den namen von einem charakter eingibt und dann weiß wo er ist??
danke für alle antworten
euer
Beginner84:)
|
All times are GMT +2. The time now is 02:28.
|
|