singleTap and doubleTap event for listener

This commit is contained in:
edgar 2017-01-22 12:49:47 -08:00
parent 63a60959c3
commit 3aee7d10d3
2 changed files with 62 additions and 8 deletions

View file

@ -38,4 +38,10 @@ public class MainActivity extends AppCompatActivity implements JoyStick.JoyStick
break; break;
} }
} }
@Override
public void onTap() {}
@Override
public void onDoubleTap() {}
} }

View file

@ -25,13 +25,14 @@ import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.RectF; import android.graphics.RectF;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
/** /**
* Created by edgarramirez on 10/30/15. * Created by edgarramirez on 10/30/15.
*/ */
public class JoyStick extends View { public class JoyStick extends View implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
public static final int DIRECTION_CENTER = -1; public static final int DIRECTION_CENTER = -1;
public static final int DIRECTION_LEFT = 0; public static final int DIRECTION_LEFT = 0;
@ -50,6 +51,8 @@ public class JoyStick extends View {
private JoyStickListener listener; private JoyStickListener listener;
private Paint paint; private Paint paint;
private RectF temp;
private GestureDetector gestureDetector;
private int direction = DIRECTION_CENTER; private int direction = DIRECTION_CENTER;
private int type = TYPE_8_AXIS; private int type = TYPE_8_AXIS;
private float centerX; private float centerX;
@ -60,7 +63,6 @@ public class JoyStick extends View {
private float buttonRadius; private float buttonRadius;
private double power = 0; private double power = 0;
private double angle = 0; private double angle = 0;
private RectF temp;
//Background Color //Background Color
private int padColor; private int padColor;
@ -82,19 +84,16 @@ public class JoyStick extends View {
public interface JoyStickListener { public interface JoyStickListener {
void onMove(JoyStick joyStick, double angle, double power, int direction); void onMove(JoyStick joyStick, double angle, double power, int direction);
void onTap();
void onDoubleTap();
} }
public JoyStick(Context context) { public JoyStick(Context context) {
super(context); this(context, null);
init(context, null);
} }
public JoyStick(Context context, AttributeSet attrs) { public JoyStick(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
paint = new Paint(); paint = new Paint();
paint.setStyle(Paint.Style.FILL); paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true); paint.setAntiAlias(true);
@ -102,6 +101,10 @@ public class JoyStick extends View {
temp = new RectF(); temp = new RectF();
gestureDetector = new GestureDetector(context, this);
gestureDetector.setIsLongpressEnabled(false);
gestureDetector.setOnDoubleTapListener(this);
padColor = Color.WHITE; padColor = Color.WHITE;
buttonColor = Color.RED; buttonColor = Color.RED;
@ -165,6 +168,8 @@ public class JoyStick extends View {
@Override @Override
public boolean onTouchEvent(MotionEvent event) { public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
switch (event.getAction()) { switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
@ -206,6 +211,49 @@ public class JoyStick extends View {
return true; return true;
} }
@Override
public boolean onDown(MotionEvent motionEvent) {
return true;
}
@Override
public void onShowPress(MotionEvent motionEvent) {}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
@Override
public void onLongPress(MotionEvent motionEvent) {}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
if (listener != null) listener.onTap();
return false;
}
@Override
public boolean onDoubleTap(MotionEvent motionEvent) {
if (listener != null) listener.onDoubleTap();
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent motionEvent) {
return false;
}
private static int calculateDirection(double degrees) { private static int calculateDirection(double degrees) {
if ((degrees >= 0 && degrees < 22.5) || (degrees < 0 && degrees > -22.5)) return DIRECTION_LEFT; if ((degrees >= 0 && degrees < 22.5) || (degrees < 0 && degrees > -22.5)) return DIRECTION_LEFT;
else if (degrees >= 22.5 && degrees < 67.5) return DIRECTION_LEFT_UP; else if (degrees >= 22.5 && degrees < 67.5) return DIRECTION_LEFT_UP;