Hallo zusammen,
ich schreib mir geraden einen kleinen Controller in Java für mein Handy.
Meine frage ist nun, warum wir beim Coustom Controller die entgegengestzte richtung angegeben als im Stock Controller?
Im Log sieht man das auch sehr deutlich, denk ich, denn in nicht mal 0,000 sec 50px nach links und rechts ich glaube für den Menschen nicht mach bar.
Code
03-02 07:16:36.563 5033-5033/com.example.facebamm.test D/Controller: Touchdown: 0
03-02 07:16:36.801 5033-5033/com.example.facebamm.test D/Controller: Left:0
03-02 07:16:36.801 5033-5033/com.example.facebamm.test D/Coustom Controller: Right: 0
03-02 07:16:37.023 5033-5033/com.example.facebamm.test D/Controller: Touchcancel: 0
Java: Start-Intent
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Controller controller = new Controller(){
@Override
public boolean onTouch(View v, MotionEvent event) {
return super.onTouch(v, event);
}
@Override
public void left(int index, float oldx, float oldy, float x, float y) {
super.right(index, oldx, oldy, x, y);
Log.d("Coustom Controller", "Left: " + index);
}
@Override
public void right(int index, float oldx, float oldy, float x, float y) {
super.right(index, oldx, oldy, x, y);
Log.d("Coustom Controller", "Right: " + index);
}
};
findViewById(R.id.Container_main).setOnTouchListener(controller);
}
}
Alles anzeigen
Java: Controller
public class Controller implements View.OnTouchListener,touchhelper {
private float sensitivitiesX = touchhelper.sensitivitiesX,
sensitivitiesY = touchhelper.sensitivitiesY;
private long scandelay = touchhelper.scandelay;
private SparseArray<PointF> mActivePointers;
private View view;
public Controller() {
mActivePointers = new SparseArray<>();
setSensitivities(50);
}
public void left(int index, float oldx, float oldy, float x, float y) {
Log.d("Controller", "Right: " + index);
}
public void right(int index, float oldx, float oldy, float x, float y) {
Log.d("Controller", "Left:" + index);
}
public void up(int index, float oldx, float oldy, float x, float y) {
Log.d("Controller", "Up: " + index);
}
public void down(int index, float oldx, float oldy, float x, float y) {
Log.d("Controller", "Down: " + index);
}
public void touchdown(int index, float x, float y) {
Log.d("Controller", "Touchdown: " + index);
}
public void touchcancel(int index, float x, float y) {
Log.d("Controller", "Touchcancel: " + index);
}
public void move(int index, float oldx, float oldy, float x, float y) {
Log.d("Controller", "Move:" + index);
}
public void setSensitivities(float sensitivities) {
sensitivitiesY = sensitivities;
sensitivitiesX = sensitivities;
}
public void setSensitivitiesX(float sensitivitiesX) {
this.sensitivitiesX = sensitivitiesX;
}
public void setSensitivitiesY(float sensitivitiesY) {
this.sensitivitiesY = sensitivitiesY;
}
public void setScandelay(long scandelay) {
this.scandelay = scandelay;
}
public View getView() {
return view;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
this.view = v;
int pointerIndex = event.getActionIndex();
int pointerId = event.getPointerId(pointerIndex);
int action = event.getActionMasked();
switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN : {
PointF f = new PointF();
f.x = event.getX(pointerIndex);
f.y = event.getY(pointerIndex);
mActivePointers.put(pointerId, f);
touchdown(pointerIndex,f.x,f.y);
break;
}
case MotionEvent.ACTION_MOVE: {
for (int size = event.getPointerCount(), i = 0; i < size; i++) {
PointF point = mActivePointers.get(event.getPointerId(i));
if (point != null) {
float accx = event.getX(i),
accy = event.getY(i);
if(Math.abs(point.x - accx) > Math.abs(point.y - accy)) {
if (accx - point.x > sensitivitiesX)
left(i, point.x, point.y, accx, accy);
if (point.x - accx > sensitivitiesX)
right(i, point.x, point.y, accx, accy);
} else {
if (accy - point.y > sensitivitiesY)
down(i, point.x, point.y, accx, accy);
if (point.y - accy > sensitivitiesY)
up(i, point.x, point.y, accx, accy);
}
point.x = accx;
point.y = accy;
}
}
try {
Thread.sleep(scandelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_CANCEL: {
PointF point = mActivePointers.get(pointerId);
touchcancel(pointerIndex,point.x,point.y);
mActivePointers.remove(pointerId);
break;
}
}
return true;
}
}
Alles anzeigen
Java: touchhelper
public interface touchhelper {
float sensitivitiesX = 50,
sensitivitiesY = 50;
long scandelay = 220;
void left(int index, float oldx, float oldy, float x, float y);
void right(int index, float oldx, float oldy, float x, float y);
void up(int index, float oldx, float oldy, float x, float y);
void down(int index, float oldx, float oldy, float x, float y);
void touchdown(int index, float x, float y);
void touchcancel(int index, float x, float y) ;
void move(int index, float oldx, float oldy, float x, float y);
void setSensitivities(float sensitivities);
void setSensitivitiesX(float sensitivitiesX);
void setSensitivitiesY(float sensitivitiesY);
void setScandelay(long scandelay);
View getView() ;
}
Alles anzeigen