teaching machines

CS 491 Lecture 4 – DroidPopper

September 12, 2013 by . Filed under cs491 mobile, fall 2013, lectures.

Agenda

Code

PopperView.java

package org.twodee.droidpopper;

import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class PopperView extends View {
  private Bitmap[] images;
  private boolean[][] isPopped;

  public PopperView(Context context,
                    AttributeSet attrs) {
    super(context, attrs);
    images = new Bitmap[2];
    images[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.unpopped);
    images[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.popped);
  }

  @Override
  protected void onSizeChanged(int w,
                               int h,
                               int oldw,
                               int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    int nRows = (int) Math.ceil(h / (double) images[0].getHeight());
    int nCols = (int) Math.ceil(w / (double) images[0].getWidth());
    isPopped = new boolean[nRows][nCols];
  }

  @Override
  public void onDraw(Canvas canvas) {
    for (int r = 0; r < isPopped.length; ++r) {
      for (int c = 0; c < isPopped[r].length; ++c) {
        Bitmap image = images[isPopped[r][c] ? 1 : 0];
        canvas.drawBitmap(image, c * image.getWidth(), r * image.getHeight(), null);
      }
    }
  }

  private void popAt(float x, float y) {
    int c = (int) x / images[0].getWidth();
    int r = (int) y / images[0].getHeight();
    if (!isPopped[r][c]) {
      isPopped[r][c] = true;
      final MediaPlayer player = MediaPlayer.create(getContext(), getRandomAudioID());
      player.start();
      player.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
          player.release();
        }
      });
      invalidate();
    }
  }

  private int getRandomAudioID() {
    int[] ids = {R.raw.pop1, R.raw.pop2, R.raw.pop3};
    return ids[new Random().nextInt(ids.length)];
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    for (int i = 0; i < event.getPointerCount(); ++i) {
      float x = event.getX(i);
      float y = event.getY(i);
      popAt(x, y);
    }
    return true;
  }
}

MainActivity.java

package org.twodee.droidpopper;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<org.twodee.droidpopper.PopperView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/popperView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" />

AndroidManifest.xml

<manifest
  xmlns:android="http://schemas.android.com/apk/res/android"
  package="org.twodee.droidpopper"
  android:versionCode="1"
  android:versionName="1.0">

  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
      android:name="org.twodee.droidpopper.MainActivity"
      android:label="Droid Popper">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>

</manifest>

Haiku

The first way seems right
Then you see a second way
And wish for a third