CS 145 Lecture 36 – Android
Agenda
- what ?s
- event-driven programming
- bubble wrap popper
- Android’s view system
- callbacks
- resource management
- touch events
Code
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<view
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="org.twodee.popper2.PopperView"
android:id="@+id/view"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
MainActivity.java
package org.twodee.popper2;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
PopperView.java
package org.twodee.popper2;
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;
import java.util.Random;
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(getResources(), R.drawable.unpopped);
images[1] = BitmapFactory.decodeResource(getResources(), R.drawable.popped);
}
@Override
protected void onSizeChanged(int w,
int h,
int oldw,
int oldh) {
int nColumns = (int) Math.ceil(w / (double) images[0].getWidth());
int nRows = (int) Math.ceil(h / (double) images[0].getHeight());
isPopped = new boolean[nRows][nColumns];
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int r = 0; r < isPopped.length; ++r) {
for (int c = 0; c < isPopped[r].length; ++c) {
if (isPopped[r][c]) {
canvas.drawBitmap(images[1], c * images[0].getWidth(), r * images[0].getHeight(), null);
} else {
canvas.drawBitmap(images[0], c * images[0].getWidth(), r * images[0].getHeight(), null);
}
}
}
}
private void popAt(float x, float y) {
int r = (int) y / images[0].getHeight();
int c = (int) x / images[0].getWidth();
if (!isPopped[r][c]) {
isPopped[r][c] = true;
playSound();
invalidate();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
for (int i = 0; i < event.getPointerCount(); ++i) {
popAt(event.getX(i), event.getY(i));
}
return true;
}
private void playSound() {
final MediaPlayer player = MediaPlayer.create(getContext(), getRandomAudioID());
player.start();
player.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
player.release();
}
});
}
private int getRandomAudioID() {
int[] ids = {R.raw.pop1, R.raw.pop2, R.raw.pop3};
return ids[new Random().nextInt(ids.length)];
}
}
Haiku
on the merits of technology:
I don’t have more time
Instead, I do a lot more
Thanks, technology!
I don’t have more time
Instead, I do a lot more
Thanks, technology!