Android Open Source - rock-paper-scissors Result Fragment






From Project

Back to project page rock-paper-scissors.

License

The source code is released under:

MIT License

If you think the Android project rock-paper-scissors 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 au.com.codeka.rps;
// w  ww.  j a  v a  2 s . c  o  m
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.support.wearable.view.GridPagerAdapter;
import android.support.wearable.view.GridViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import au.com.codeka.rps.game.StateManager;

/**
 * This fragment shows the result of the game.
 */
public class ResultFragment extends Fragment {
    private ImageView otherChoice;
    private ProgressBar otherLoading;
    private TextView resultText;
    private Handler handler;
    private TextView numYouWinsText;
    private TextView numThemWinsText;

    public enum Result {
        Win,
        Loss,
        Draw
    }

    public static ResultFragment newInstance(String playerChoice) {
        Bundle args = new Bundle();
        args.putString("PlayerChoice", playerChoice);
        ResultFragment fragment = new ResultFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        handler = new Handler();

        View view = inflater.inflate(R.layout.fragment_result, container, false);
        setImage(((ImageView) view.findViewById(R.id.player_choice)), getArguments().getString("PlayerChoice"));
        otherChoice = (ImageView) view.findViewById(R.id.other_choice);
        otherLoading = (ProgressBar) view.findViewById(R.id.other_loading);
        resultText = (TextView) view.findViewById(R.id.result);
        numYouWinsText = (TextView) view.findViewById(R.id.you_wins);
        numThemWinsText = (TextView) view.findViewById(R.id.them_wins);
        updateWinCounts();
        return view;
    }

    public void setOtherChoice(final String choice, final Result result) {
        handler.post(new Runnable() {
            @Override
            public void run() {
            setImage(otherChoice, choice);
            otherLoading.setVisibility(View.GONE);
            otherChoice.setVisibility(View.VISIBLE);

            resultText.startAnimation(AnimationUtils.loadAnimation(
                    getActivity(), R.anim.result_text));
            switch(result) {
                case Win:
                    resultText.setText("YOU WIN!");
                    break;
                case Loss:
                    resultText.setText("YOU LOSE!");
                    break;
                case Draw:
                    resultText.setText("DRAW!");
                    break;
            }

            updateWinCounts();
            }
        });
    }

    private void updateWinCounts() {
        numYouWinsText.setText(Integer.toString(StateManager.i.getNumYouWins()));
        numThemWinsText.setText(Integer.toString(StateManager.i.getNumThemWins()));
    }

    private void setImage(ImageView iv, String choice) {
        if (choice.toLowerCase().equals("rock")) {
            iv.setImageResource(R.drawable.rock);
        } else if (choice.toLowerCase().equals("paper")) {
            iv.setImageResource(R.drawable.paper);
        } else if (choice.toLowerCase().equals("scissors")) {
            iv.setImageResource(R.drawable.scissors);
        } else {
            iv.setImageBitmap(null);
        }
    }
}




Java Source Code List

au.com.codeka.rps.ApplicationTest.java
au.com.codeka.rps.DebugLog.java
au.com.codeka.rps.FindingOpponentFragment.java
au.com.codeka.rps.GameActivity.java
au.com.codeka.rps.GameFragment.java
au.com.codeka.rps.MessageListenerService.java
au.com.codeka.rps.NotificationUpdateService.java
au.com.codeka.rps.PhoneConnection.java
au.com.codeka.rps.ResultFragment.java
au.com.codeka.rps.SplashActivity.java
au.com.codeka.rps.WatchConnection.java
au.com.codeka.rps.game.AwaitingPlayerChoiceState.java
au.com.codeka.rps.game.AwaitingPlayerChoiceState.java
au.com.codeka.rps.game.AwaitingResultState.java
au.com.codeka.rps.game.AwaitingResultState.java
au.com.codeka.rps.game.DisplayingResultState.java
au.com.codeka.rps.game.DisplayingResultState.java
au.com.codeka.rps.game.FindingOpponentState.java
au.com.codeka.rps.game.FindingOpponentState.java
au.com.codeka.rps.game.GameRunningState.java
au.com.codeka.rps.game.GameRunningState.java
au.com.codeka.rps.game.MatchInfo.java
au.com.codeka.rps.game.ResultInfo.java
au.com.codeka.rps.game.StateManager.java
au.com.codeka.rps.game.StateManager.java
au.com.codeka.rps.game.State.java
au.com.codeka.rps.game.State.java