Snippets

Mathias N Picker

Created by Mathias N
	private enum SelectedIcon
	{
		DEFAULT, BEDTIME, ALARM
	}

	private SelectedIcon mSelectionState = SelectedIcon.DEFAULT;

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		posX = (int) event.getX();
		posY = (int) event.getY();
		double angleTouch = Math.toDegrees(Math.atan2(posY - center, posX - center)) + 90;
		if (angleTouch < -0.1) {
			angleTouch = 360 + angleTouch;
		}

		//Reset status to nothing selected on pointer up
		if (event.getActionMasked() == MotionEvent.ACTION_UP)	//ACTION_POINTER_UP if considering multi-touch
		{
			mSelectionState =  SelectedIcon.DEFAULT;
			return true;
		}
		else if ( mSelectionState == SelectedIcon.DEFAULT)	//Nothing selected yet
		{
			//Check if we are within selection distance of anything
			if ((Math.abs(angleTouch - angleBedtime) < 15 || Math.abs(angleTouch - angleBedtime) > 340)) {

				mSelectionState = SelectedIcon.BEDTIME;

			} else if ((Math.abs(angleTouch - angleAlarmtime) < 15 || Math.abs(angleTouch - angleAlarmtime) > 340)) {

				mSelectionState = SelectedIcon.ALARM;
			}
		}

		//If the pointer has been within selection distance of an icon this session,
		//move it accordingly. 
		switch(mSelectionState)
		{
			case BEDTIME:
			{
				angleBedtimeIcon = (float) Math.atan2(posX - center, posY - center);
				break;
			}

			case ALARM:
			{
				angleAlarmIcon = (float) Math.atan2(posX - center, posY - center);
				break;
			}
			default:
			{
				//Nothing
			}

		}

		return true;
	}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.