Android Open Source - crystalball Main Activity






From Project

Back to project page crystalball.

License

The source code is released under:

GNU Lesser General Public License

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.culebracut.crystalball;
//from  w w w  .j  a  v  a2  s .  c  o m
//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.util.Log;
import android.view.Menu;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.culebracut.crystalball.ShakeDetector.OnShakeListener;
import com.example.crystalball.R;

public class MainActivity extends Activity {
  
  public static final String TAG = MainActivity.class.getSimpleName();
  private TextView mAnswerLabel;
  //private Button mGetAnswerButton;
  private ImageView mCrystalBallImage;
  private CrystalBall mCrystalBall = new CrystalBall();
  private SensorManager mSensorManager;
  private Sensor mAccelerometer;
  private ShakeDetector mShakeDetector;

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

    // Assign the Views from the layout file
    // final TextView answerLabel = (TextView) findViewById(R.id.textView1);
    mAnswerLabel = (TextView) findViewById(R.id.textView1);
    // Button getAnswerButton = (Button)findViewById(R.id.button1);
    //mGetAnswerButton = (Button) findViewById(R.id.button1);
    mCrystalBallImage = (ImageView) findViewById(R.id.imageView1);
    
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mShakeDetector = new ShakeDetector(new OnShakeListener() {
      
      @Override
      public void onShake() {
        handleNewAnswer();
        
      }
    });
    
    
    Log.d(TAG,"We're looging from the onCreate() method!");
    
    /*mGetAnswerButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View arg0) {
        // TODO Auto-generated method stub
        String[] answers = { "It is certain", "It is decidedly so",
            "Without a doubt", "Yes definitely",
            "You may rely on it", "As I see it, yes",
            "Most likely", "Outlook good", "Yes",
            "Signs point to yes", "Reply hazy try again",
            "Ask again later", "Better not tell you now",
            "Cannot predict now", "Concentrate and ask again",
            "Don't count on it", "My reply is no",
            "My sources say no", "Outlook not so good",
            "Very doubtful" };
        // The button was pushed, so we should update it
        String answer = "";

        // Randomly select an answer of either Yes, No, or Maybe
        Random randomGenerator = new Random();

        int randomNumber = randomGenerator.nextInt(answers.length);

        answer = answers[randomNumber];
        
        handleNewAnswer();

      }
    });*/
  }
  
  @Override
  public void onResume(){
    super.onResume();
    mSensorManager.registerListener(mShakeDetector, mAccelerometer, 
        SensorManager.SENSOR_DELAY_UI);
  }
  
  @Override
  public void onPause(){
    super.onPause();
    mSensorManager.unregisterListener(mShakeDetector);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  private void animateCrystallBall() {
    // ImageView crystallBallImage = (ImageView)
    // findViewById(R.id.imageView1);

    mCrystalBallImage.setImageResource(R.drawable.ball_animation);
    AnimationDrawable ballAnimation = (AnimationDrawable) mCrystalBallImage
        .getDrawable();

    if (ballAnimation.isRunning()) {
      ballAnimation.stop();
    }
    ballAnimation.start();
  }
  
  private void playSound(){
    MediaPlayer player = MediaPlayer.create(this,R.raw.crystal_ball);
    player.start();
    player.setOnCompletionListener(new OnCompletionListener() {
      
      @Override
      public void onCompletion(MediaPlayer mp) {
        mp.release();
        
      }
    });

  }

  private void animateAnswer() {
    AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1);
    fadeInAnimation.setDuration(1500);
    fadeInAnimation.setFillAfter(true);

    mAnswerLabel.setAnimation(fadeInAnimation);
  }

  private void handleNewAnswer() {
    String answer = mCrystalBall.getAnAnswer();

    animateCrystallBall();
    animateAnswer();
    playSound();

    // Update the label with the answer
    mAnswerLabel.setText(answer);
  }
}




Java Source Code List

com.culebracut.crystalball.CrystalBall.java
com.culebracut.crystalball.MainActivity.java
com.culebracut.crystalball.ShakeDetector.java