Back to project page 2048GameAndroid.
The source code is released under:
Apache License
If you think the Android project 2048GameAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package zhengxiao.myapplication; //from w w w . j a va2s .c o m import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.view.GestureDetectorCompat; import android.support.v7.app.ActionBarActivity; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.GridLayout; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; import butterknife.ButterKnife; import butterknife.InjectView; import zhengxiao.myapplication.game.Board; import zhengxiao.myapplication.game.Direction; import zhengxiao.myapplication.game.Game; import zhengxiao.myapplication.utils.OnGestureListenerAdapter; public class GameActivity extends ActionBarActivity implements Game.Callback { private Game game; private Toast toast; public static void actionShow(Activity activity) { activity.startActivity(new Intent(activity, GameActivity.class)); } @InjectView(R.id.board) GridLayout board; List<TextView> blocks = new ArrayList<>(); GestureDetectorCompat detector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game); ButterKnife.inject(this); detector = new GestureDetectorCompat(this, new OnGestureListenerAdapter() { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { return onFlingDirection(velocityX, velocityY); } }); board.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return detector.onTouchEvent(event); } }); // ???? LayoutInflater inflater = LayoutInflater.from(this); for (int i = 0; i < Game.ROW_COUNT * Game.ROW_COUNT; i++) { TextView block = (TextView) inflater.inflate(R.layout.block, board, false); block.setText(""); board.addView(block); blocks.add(block); } startGame(); } /** * ???????? * * @param velocityX * @param velocityY * @return */ private boolean onFlingDirection(float velocityX, float velocityY) { Direction direction; if (velocityX > velocityY) { if (velocityX > 0) { direction = Direction.RIGHT; } else { direction = Direction.UP; } } else { if (velocityY > 0) { direction = Direction.DOWN; } else { direction = Direction.LEFT; } } toast("Move in "+ direction.name()); game.moveInDirection(direction); return true; } private void toast(String message) { if(toast != null){ toast.cancel(); } toast = Toast.makeText(this, message, Toast.LENGTH_SHORT); toast.show(); } @Override public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) { switch (event.getKeyCode()) { case KeyEvent.KEYCODE_DPAD_LEFT: game.moveInDirection(Direction.LEFT); break; case KeyEvent.KEYCODE_DPAD_RIGHT: game.moveInDirection(Direction.LEFT); break; case KeyEvent.KEYCODE_DPAD_UP: game.moveInDirection(Direction.LEFT); break; case KeyEvent.KEYCODE_DPAD_DOWN: game.moveInDirection(Direction.LEFT); break; } return super.onKeyUp(keyCode, event); } /** * ??????? */ private void startGame() { game = new Game(this); } // region Menu @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_game, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } // endregion Board lastBoard; @Override public void render(@NonNull Board board) { // TODO compare with lastBoard and animate List<Integer> values = board.getValues(); for (int i = 0; i < values.size(); i++) { blocks.get(i).setText("" + values.get(i)); } lastBoard = board; } @Override public void gameOver(int finalScore) { finish(); } }