Android Open Source - 101AndroidApps Status Fragment






From Project

Back to project page 101AndroidApps.

License

The source code is released under:

Licenced under the Creative Commons Attribution 4.0 licence. For full text see http://creativecommons.org/licenses/by/4.0/

If you think the Android project 101AndroidApps 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.hulzenga.ioi.android.app_007;
//from   w  ww  . ja  v a  2 s . c  om
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hulzenga.ioi.android.R;
import com.hulzenga.ioi.android.util.Constrain;
import com.hulzenga.ioi.android.util.ScreenMetrics;

public class StatusFragment extends Fragment {

  private static final String TAG = "STATUS FRAGMENT";

  private TextView mTimerText;
  private TextView mDifficultyText;
  private TextView mScoreDeltaText;
  private TextView mScoreText;

  private int mScore = 0;
  private int mTime  = 60;

  private Thread mTimerThread;
  private volatile boolean mPaused = false;
  private volatile boolean mFinish = false;

  private TimeOutListener mTimeOutListener;

  private int   mAnimationLengthMedium;
  private float mAnimationDistance;

  public interface TimeOutListener {
    public void onTimeOut(int score);
  }

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);

    mAnimationLengthMedium = activity.getResources().getInteger(R.integer.animation_medium);
    mAnimationDistance = ScreenMetrics.dpToPix(60.0f, activity);

    try {
      mTimeOutListener = (TimeOutListener) activity;
    } catch (ClassCastException e) {
      Log.e(TAG, activity.toString() + " must implement StatusFragment.TimeOutListener");
    }
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    return inflater.inflate(R.layout.app_007_fragment_status, container, false);
  }

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


    mTimerText = (TextView) getView().findViewById(R.id.app_007_timerTextView);
    mDifficultyText = (TextView) getView().findViewById(R.id.app_007_difficultyTextView);
    mScoreText = (TextView) getView().findViewById(R.id.app_007_scoreTextView);
    mScoreDeltaText = (TextView) getView().findViewById(R.id.app_007_scoreDeltaView);
    showScore();

  }

  public void resetScore() {
    mScore = 0;
  }

  public void addPoint() {
    mScore++;

    mScoreDeltaText.setText("+1");
    mScoreDeltaText.setTextColor(Color.GREEN);
    AnimatorSet set = new AnimatorSet();
    Animator scoreDelta = ObjectAnimator.ofFloat(mScoreDeltaText, View.TRANSLATION_Y, -mAnimationDistance);
    set.play(scoreDelta);
    set.setDuration(mAnimationLengthMedium);
    set.addListener(new AnimatorListener() {

      @Override
      public void onAnimationStart(Animator animation) {

      }

      @Override
      public void onAnimationRepeat(Animator animation) {
      }

      @Override
      public void onAnimationEnd(Animator animation) {
        mScoreDeltaText.setText("");
        mScoreDeltaText.setTranslationY(0.0f);
        showScore();
      }

      @Override
      public void onAnimationCancel(Animator animation) {
      }
    });
    set.start();
  }

  public void penaltyPoints(int points) {
    // only do the animation and update if there are actual pennalty points
    if (points > 0) {
      mScore = Constrain.lowerBound(0, mScore - points);
      mScoreDeltaText.setText("-" + points);
      mScoreDeltaText.setTextColor(Color.RED);
      AnimatorSet set = new AnimatorSet();
      Animator scoreDelta = ObjectAnimator.ofFloat(mScoreDeltaText, View.TRANSLATION_Y, +mAnimationDistance);
      set.play(scoreDelta);
      set.setDuration(mAnimationLengthMedium);
      set.addListener(new AnimatorListener() {

        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {
          mScoreDeltaText.setText("");
          mScoreDeltaText.setTranslationY(0.0f);
          showScore();
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }
      });
      set.start();
    }

  }

  private void showScore() {
    mScoreText.setText(String.valueOf(mScore));
  }

  public void setTimer(int seconds) {

  }

  private boolean mRunning = false;

  public boolean isRunning() {
    return mRunning;
  }

  public void startTimer() {

    final Activity activity = getActivity();
    mRunning = true;

    mTimerThread = new Thread(new Runnable() {

      @Override
      public void run() {
        int i = 0;

        while (true) {
          try {
            Thread.sleep(100);
          } catch (InterruptedException e) {
            Log.d(TAG, "timer interrupted");
            break;
          }
          if (mFinish) {
            break;
          }
          if (!mPaused) {
            if (i++ % 10 == 0) {
              mTime--;
              activity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                  mTimerText.setText(String.valueOf(mTime));
                }
              });

              if (mTime <= 0) {
                activity.runOnUiThread(new Runnable() {

                  @Override
                  public void run() {
                    mTimeOutListener.onTimeOut(mScore);
                  }
                });

                mFinish = true;
              }
            }
          }
        }
      }
    });
    mTimerThread.start();
  }

  public void pauseTimer() {
    mPaused = true;
  }

  public void resumeTimer() {
    mPaused = false;
  }

  public void stopTimer() {
    mFinish = true;
    mPaused = false;
  }
}




Java Source Code List

com.hulzenga.ioi.android.AppActivity.java
com.hulzenga.ioi.android.AppDetailsDialog.java
com.hulzenga.ioi.android.App.java
com.hulzenga.ioi.android.TableOfContentsActivity.java
com.hulzenga.ioi.android.app_001.HelloWorldActivity.java
com.hulzenga.ioi.android.app_002.Ball.java
com.hulzenga.ioi.android.app_002.BouncyBallsActivity.java
com.hulzenga.ioi.android.app_002.BouncyBallsView.java
com.hulzenga.ioi.android.app_003.MonsterDatabaseActivity.java
com.hulzenga.ioi.android.app_003.MonsterEditDialog.java
com.hulzenga.ioi.android.app_003.MonsterGenerator.java
com.hulzenga.ioi.android.app_003.MonsterListAdapter.java
com.hulzenga.ioi.android.app_003.database.MonsterContract.java
com.hulzenga.ioi.android.app_003.database.MonsterDatabaseHelper.java
com.hulzenga.ioi.android.app_003.database.MonsterProvider.java
com.hulzenga.ioi.android.app_004.BouncyBall3dActivity.java
com.hulzenga.ioi.android.app_004.BouncyBall3dGLSurfaceView.java
com.hulzenga.ioi.android.app_004.BouncyBall3dRenderer.java
com.hulzenga.ioi.android.app_004.BouncyBallGame.java
com.hulzenga.ioi.android.app_005.ElementActivity.java
com.hulzenga.ioi.android.app_005.ElementAdapter.java
com.hulzenga.ioi.android.app_005.ElementAnimator.java
com.hulzenga.ioi.android.app_005.ElementSnakeView.java
com.hulzenga.ioi.android.app_005.ElementView.java
com.hulzenga.ioi.android.app_005.Element.java
com.hulzenga.ioi.android.app_006.EmptyFragment.java
com.hulzenga.ioi.android.app_006.ExposureFragment.java
com.hulzenga.ioi.android.app_006.SettingChangeListener.java
com.hulzenga.ioi.android.app_006.SettingIconFragment.java
com.hulzenga.ioi.android.app_006.SettingMenuFragment.java
com.hulzenga.ioi.android.app_006.SettingMenuLvl2Fragment.java
com.hulzenga.ioi.android.app_006.Setting.java
com.hulzenga.ioi.android.app_006.SimpleCameraActivity.java
com.hulzenga.ioi.android.app_006.SimpleCameraPreview.java
com.hulzenga.ioi.android.app_007.ButtonsFragment.java
com.hulzenga.ioi.android.app_007.HighScores.java
com.hulzenga.ioi.android.app_007.LinksFragment.java
com.hulzenga.ioi.android.app_007.Menu.java
com.hulzenga.ioi.android.app_007.Review.java
com.hulzenga.ioi.android.app_007.Score.java
com.hulzenga.ioi.android.app_007.StatusFragment.java
com.hulzenga.ioi.android.app_007.WikiGameActivity.java
com.hulzenga.ioi.android.app_007.Wiki.java
com.hulzenga.ioi.android.app_008.OverheidActivity.java
com.hulzenga.ioi.android.app_008.OverheidFeedAdapter.java
com.hulzenga.ioi.android.app_008.OverheidFeedFragment.java
com.hulzenga.ioi.android.app_008.OverheidFeedItem.java
com.hulzenga.ioi.android.app_008.OverheidFeed.java
com.hulzenga.ioi.android.app_008.OverheidPagerAdapter.java
com.hulzenga.ioi.android.app_009.ThumbsUpActivity.java
com.hulzenga.ioi.android.app_009.ThumbsUpServer.java
com.hulzenga.ioi.android.app_009.ThumbsUpService.java
com.hulzenga.ioi.android.app_010.CompassActivity.java
com.hulzenga.ioi.android.app_010.CompassDetailsView.java
com.hulzenga.ioi.android.app_010.CompassView.java
com.hulzenga.ioi.android.util.Constrain.java
com.hulzenga.ioi.android.util.DeveloperTools.java
com.hulzenga.ioi.android.util.FileManager.java
com.hulzenga.ioi.android.util.ScreenMetrics.java
com.hulzenga.ioi.android.util.open_gl.ColorFunctionFactory.java
com.hulzenga.ioi.android.util.open_gl.ColorFunction.java
com.hulzenga.ioi.android.util.open_gl.ShaderTools.java
com.hulzenga.ioi.android.util.open_gl.engine.NodeController.java
com.hulzenga.ioi.android.util.open_gl.engine.SceneGraph.java
com.hulzenga.ioi.android.util.open_gl.engine.SceneNode.java
com.hulzenga.ioi.android.util.open_gl.geometry.Box.java
com.hulzenga.ioi.android.util.open_gl.geometry.Cylinder.java
com.hulzenga.ioi.android.util.open_gl.geometry.Geometry.java
com.hulzenga.ioi.android.util.open_gl.geometry.Grid.java
com.hulzenga.ioi.android.util.open_gl.geometry.Sphere.java
com.hulzenga.ioi.android.util.open_gl.geometry.Transform.java
com.hulzenga.ioi.android.util.open_gl.vector.Vec3.java
com.hulzenga.ioi.android.util.open_gl.vector.Vec4.java
fi.iki.elonen.NanoHTTPD.java