Android Open Source - rock-paper-scissors Game 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;
/*from  ww  w.  j a v  a 2  s  .  c om*/
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
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.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * This Fragment displays the actual game where you choose your rock, paper or scissors.
 *
 */
public class GameFragment extends Fragment {
    private GridViewPager gridViewPager;
    private TextView timerText;
    private Handler handler;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_game, container, false);
        gridViewPager = (GridViewPager) view.findViewById(R.id.pager);
        gridViewPager.setAdapter(new MyGridViewPagerAdapter());

        handler = new Handler();

        timerText = (TextView) view.findViewById(R.id.timer);
        setTimer(3);

        return view;
    }

    private void setTimer(final int value) {
        Activity activity = getActivity();
        if (activity == null) {
            return;
        }

        Animation timerAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.timer_text);
        timerAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }
            @Override
            public void onAnimationEnd(Animation animation) {
                timerText.setVisibility(View.GONE);
            }
            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });

        timerText.setVisibility(View.VISIBLE);
        timerText.setText(Integer.toString(value));
        timerText.setAnimation(timerAnimation);

        if (value > 1) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    setTimer(value - 1);
                }
            }, 2000);
        }
    }

    public String getCurrentChoice() {
        String[] choices = {"Rock", "Paper", "Scissors"};
        return choices[gridViewPager.getCurrentItem().y % 3];
    }

    private class MyGridViewPagerAdapter extends GridPagerAdapter {
        @Override
        public int getColumnCount(int arg0) {
            return 1;
        }

        @Override
        public int getRowCount() {
            return 100; // TODO: infinite?
        }

        @Override
        protected Object instantiateItem(ViewGroup container, int row, int col) {
            ImageView iv = new ImageView(getActivity(), null);
            if (row % 3 == 0) {
                iv.setImageResource(R.drawable.rock);
            } else if (row % 3 == 1) {
                iv.setImageResource(R.drawable.paper);
            } else {
                iv.setImageResource(R.drawable.scissors);
            }
            container.addView(iv);
            return iv;
        }

        @Override
        protected void destroyItem(ViewGroup container, int row, int col, Object view) {
            container.removeView((View) view);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
}




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