Android Open Source - crystalball Main Activity






From Project

Back to project page crystalball.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project crystalball listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.es0329.crystalball;
/*from  w w w  .j a v a 2  s .  c  om*/
import java.util.Random;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.es0329.crystalball.ShakeDetector.OnShakeListener;

public class MainActivity extends Activity {
  private AnimationDrawable ballAnimation;
  private AlphaAnimation labelAnimation;
  private CrystalBall crystal;
  private MediaPlayer player;

  private SensorManager sensorManager;
  private ShakeDetector shakeDetector;
  private Sensor accelerometer;

  private ImageView image;
  private TextView label;

  private int[] colors;

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

    crystal = new CrystalBall(getApplicationContext());

    image = (ImageView) findViewById(R.id.image);
    label = (TextView) findViewById(R.id.label);

    colors = getResources().getIntArray(R.array.colors);

    image.setImageResource(R.drawable.ball_animation);
    image.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View arg0) {
        handlePrediction();
      }
    });

    ballAnimation = (AnimationDrawable) image.getDrawable();

    labelAnimation = new AlphaAnimation(0, 1);
    labelAnimation.setDuration(4000);
    labelAnimation.setFillAfter(true);
    label.setAnimation(labelAnimation);

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    accelerometer = sensorManager
        .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    shakeDetector = new ShakeDetector(new OnShakeListener() {

      @Override
      public void onShake() {
        handlePrediction();
      }
    });
  }

  @Override
  protected void onResume() {
    super.onResume();
    sensorManager.registerListener(shakeDetector, accelerometer,
        SensorManager.SENSOR_DELAY_UI);
  }

  @Override
  protected void onPause() {
    super.onPause();
    sensorManager.unregisterListener(shakeDetector);
  }

  private void animateImage() {

    if (ballAnimation.isRunning()) {
      ballAnimation.stop();
    }
    ballAnimation.start();
  }

  private void animateLabel() {
    label.startAnimation(labelAnimation);

    int randomIndex = new Random().nextInt(colors.length);
    label.setTextColor(colors[randomIndex]);
    label.setText(crystal.makePrediction());
  }

  private void playSound() {
    player = MediaPlayer
        .create(getApplicationContext(), R.raw.crystal_ball);

    if (player.isPlaying()) {
      player.stop();
    }
    player.start();

    player.setOnCompletionListener(new OnCompletionListener() {

      @Override
      public void onCompletion(MediaPlayer mp) {
        mp.release();
      }
    });
  }

  private void handlePrediction() {
    animateImage();
    animateLabel();
    playSound();
  }
}




Java Source Code List

com.es0329.crystalball.CrystalBall.java
com.es0329.crystalball.MainActivity.java
com.es0329.crystalball.ShakeDetector.java