Exercise Monday

Goal: Your goal today is to explore touch events, gestures, and property animations in Android. Begin by implementing those covered in this weekend's homework, then branch and explore what other gestures and animations are available!

Warm Up


  • What is an interface? What functionality do they offer?
  • How does an interface differ from a class?
  • How do we implement interfaces?
  • How are gestures enabled or disabled?
  • How do we use our user interface to indicate to a user what gestures are available?
  • How is a touchscreen gesture processed? Which portion of the application is responsible? What other components are involved?

Code


MyRestaurants Continued

Follow along with the weekend homework to implement the drag and drop, swipe to delete, and animation functionality into our ongoing MyRestaurants project.

"Bells & Whistles" App of Choice

Let’s continue to explore animations and the various touch events recognized in Android. To do so, create an app of your choosing that responds to the following touch events. Each event should trigger a different property animation, including):

Potential project ideas could include:

  • Interactive to-do list that allows you to order events based on priority, swipe or fling to delete, long-click to update, etc.
  • A Bop-It!-inspired application, or soundboard-style app that offers a variety of fun noises based on different gesture interactions.
  • Implementing gesture capability into one of your favorite existing Android projects.
  • Anything else you and your partner may think of. Get creative!

Here are some additional resources to get you started:

Further Exploration

If you and your partner are able to successfully implement each of the touch gestures listed above, give shake gestures a shot! To get you started, see the below is example that prints a log message when the phone is shaken:

public class MainActivity extends AppCompatActivity implements SensorEventListener {
    private SensorManager mSensorManager;
    private Sensor mSensor;
    private long lastUpdate = 0;
    private float last_x, last_y, last_z;
    private static final int SHAKE_THRESHOLD = 500;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_restaurants);

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mSensorManager.registerListener(this, mSensor, mSensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        Sensor sensor = event.sensor;

        if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];

            long curTime = System.currentTimeMillis();

            if ((curTime - lastUpdate) > 100 ) {
                long diffTime = (curTime - lastUpdate);
                lastUpdate = curTime;

                float speed = Math.abs(x + y + z - last_x - last_y - last_z)/diffTime * 10000;

                if(speed > SHAKE_THRESHOLD) {
                    Log.d("SensorEventListener", "shaking");

                last_x = x;
                last_y = y;
                last_z = z;

            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}

Peer Code Review


  • Does the application include functional gesture listeners?
  • Is it clear what gestures is required to interact with the application? Why or why not?
  • Is the code well-refactored and easy to follow?
  • Would you use this application? Why or why not?
  • What other features might you like to see in this app?