課程
/移動(dòng)開(kāi)發(fā)
/Android
/快速實(shí)現(xiàn)不一樣的移動(dòng)拼圖
daniel661839@163.com ?能發(fā)我一份嗎?
2016-11-07
源自:快速實(shí)現(xiàn)不一樣的移動(dòng)拼圖 3-1
正在回答
抱歉,慕課不許我發(fā)源碼,你可以看看其它同學(xué)做的。
zzlandroid
照著老師的教程敲的一個(gè)樣例:https://github.com/kiritozzl/PuzzleGame
王正一
我實(shí)現(xiàn)的項(xiàng)目源碼:https://github.com/wangzhengyi/HYPinTu
慕粉3288933
我寫(xiě)了代碼,地址在https://github.com/jowang2016/pintumooc 但是判斷程序結(jié)束那里總是沒(méi)有提示
public class MainActivity extends Activity { ? ?private boolean isAnimRun = false; ? ?//判斷游戲是否開(kāi)始 ? ?private boolean isGameStart = false; ? ?private ImageView [] [] iv_game_arr = new ImageView[3][5]; ? ?private GridLayout gl_main_game; ? ?private ImageView iv_null_ImageView; ? ?private GestureDetector mDetector; ? ?@Override ? ?public boolean onTouchEvent(MotionEvent event) { ? ? ? ?return mDetector.onTouchEvent(event); ? ?} ? ?@Override ? ?public boolean dispatchTouchEvent(MotionEvent ev) { ? ? ? ?mDetector.onTouchEvent(ev); ? ? ? ?return super.dispatchTouchEvent(ev); ? ?} ? ?@Override ? ?protected void onCreate(Bundle savedInstanceState) { ? ? ? ?super.onCreate(savedInstanceState); ? ? ? ?setContentView(R.layout.activity_main); ? ? ? ?mDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() { ? ? ? ? ? ?@Override ? ? ? ? ? ?public boolean onDown(MotionEvent motionEvent) { ? ? ? ? ? ? ? ?return false; ? ? ? ? ? ?} ? ? ? ? ? ?@Override ? ? ? ? ? ?public void onShowPress(MotionEvent motionEvent) { ? ? ? ? ? ?} ? ? ? ? ? ?@Override ? ? ? ? ? ?public boolean onSingleTapUp(MotionEvent motionEvent) { ? ? ? ? ? ? ? ?return false; ? ? ? ? ? ?} ? ? ? ? ? ?@Override ? ? ? ? ? ?public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) { ? ? ? ? ? ? ? ?return false; ? ? ? ? ? ?} ? ? ? ? ? ?@Override ? ? ? ? ? ?public void onLongPress(MotionEvent motionEvent) { ? ? ? ? ? ?} ? ? ? ? ? ?@Override ? ? ? ? ? ?public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) { ? ? ? ? ? ? ? ?int type = getDirByGes(motionEvent.getX(),motionEvent.getY(),motionEvent1.getX(),motionEvent1.getY());// ? ? ? ? ? ? ? ?Toast.makeText(MainActivity.this,""+type,Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ?changeByDir(type); ? ? ? ? ? ? ? ?return false; ? ? ? ? ? ?} ? ? ? ?}); ? ? ? ?Bitmap bigBm = ((BitmapDrawable) getResources().getDrawable(R.drawable.ic_game_tu)).getBitmap(); ? ? ? ?int tuWandH = bigBm.getWidth()/5; ? ? ? ?int ivWandH = getWindowManager().getDefaultDisplay().getWidth()/5; ? ? ? ?for (int i=0;i<iv_game_arr.length;i++){ ? ? ? ? ? ?for(int j=0;j<iv_game_arr[0].length;j++){ ? ? ? ? ? ? ? ?Bitmap bm = Bitmap.createBitmap(bigBm,j*tuWandH,i*tuWandH,tuWandH,tuWandH); ? ? ? ? ? ? ? ?iv_game_arr[i][j]=new ImageView(this); ? ? ? ? ? ? ? ?iv_game_arr[i][j].setImageBitmap(bm); ? ? ? ? ? ? ? ?iv_game_arr[i][j].setLayoutParams(new RelativeLayout.LayoutParams(ivWandH,ivWandH)); ? ? ? ? ? ? ? ?iv_game_arr[i][j].setPadding(2,2,2,2); ? ? ? ? ? ? ? ?iv_game_arr[i][j].setTag(new GameData(i,j,bm)); ? ? ? ? ? ? ? ?iv_game_arr[i][j].setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? ? ? ? ?@Override ? ? ? ? ? ? ? ? ? ?public void onClick(View v) { ? ? ? ? ? ? ? ? ? ? ? ?boolean flag = isHasByNullImageView((ImageView)v); ? ? ? ? ? ? ? ? ? ? ? ?//Toast.makeText(MainActivity.this,"位置關(guān)系是否存在:"+flag,Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? ? ? ? ?if(flag){ ? ? ? ? ? ? ? ? ? ? ? ? ? ?changeDataByImageView((ImageView) v); ? ? ? ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ?}); ? ? ? ? ? ?} ? ? ? ?} ? ? ? ?gl_main_game = (GridLayout) findViewById(R.id.gl_main_game); ? ? ? ?for(int i=0;i<iv_game_arr.length;i++){ ? ? ? ? ? ?for(int j=0;j<iv_game_arr[0].length;j++){ ? ? ? ? ? ? ? ?gl_main_game.addView(iv_game_arr[i][j]); ? ? ? ? ? ?} ? ? ? ?} ? ? ? ?//設(shè)置最后一個(gè)方塊為空 ? ? ? ?setNullImageView(iv_game_arr[2][4]); ? ? ? ?//隨機(jī)打亂順序 ? ? ? ?randomMove(); ? ? ? ?isGameStart = true; ? ?} ? ?//隨機(jī)打亂順序 ? ?public void randomMove(){ ? ? ? ?for(int i=0;i<100;i++){ ? ? ? ? ? ?int type = (int) ((Math.random()*4)+1); ? ? ? ? ? ?changeByDir(type,false); ? ? ? ?} ? ?} ? ?public void changeByDir(int type){ ? ? ? ? changeByDir(type,true); ? ?} ? ?public void changeByDir(int type ,boolean isAnim){ ? ? ? ?GameData mNullGameData = (GameData) iv_null_ImageView.getTag(); ? ? ? ?int new_x = mNullGameData.x; ? ? ? ?int new_y = mNullGameData.y; ? ? ? ?if(type == 1){ ? ? ? ? ? ?new_x++; ? ? ? ?}else if(type == 2){ ? ? ? ? ? ?new_x--; ? ? ? ?}else if(type == 3){ ? ? ? ? ? ?new_y++; ? ? ? ?}else if(type == 4){ ? ? ? ? ? ?new_y--; ? ? ? ?} ? ? ? ?if(new_x>=0&&new_x<iv_game_arr.length&&new_y>=0&&new_y<iv_game_arr[0].length){ ? ? ? ? ? ?if(isAnim){ ? ? ? ? ? ? ? ?changeDataByImageView(iv_game_arr[new_x][new_y]); ? ? ? ? ? ?}else { ? ? ? ? ? ? ? ?changeDataByImageView(iv_game_arr[new_x][new_y],isAnim); ? ? ? ? ? ?} ? ? ? ?}else { ? ? ? ?} ? ?} ? ?//判斷結(jié)束的方法 ? ?public void isGameOver(){ ? ? ? ?boolean isGameOver = true; ? ? ? ?for(int i=0;i<iv_game_arr.length;i++){ ? ? ? ? ? ?for(int j=0;j<iv_game_arr[0].length;j++){ ? ? ? ? ? ? ? ?if(iv_game_arr[i][j]==iv_null_ImageView){ ? ? ? ? ? ? ? ? ? ?continue; ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ?GameData mGameData = (GameData) iv_game_arr[i][j].getTag(); ? ? ? ? ? ? ? ?if(!mGameData.isTrue()){ ? ? ? ? ? ? ? ? ? ?isGameOver = false; ? ? ? ? ? ? ? ? ? ?break; ? ? ? ? ? ? ? ?} ? ? ? ? ? ?} ? ? ? ? ? ?} ? ? ? ?if(isGameOver){ ? ? ? ? ? ?Toast.makeText(MainActivity.this,"游戲結(jié)束",Toast.LENGTH_SHORT).show(); ? ? ? ?} ? ?} ? ?public int getDirByGes(float start_x, float start_y, float end_x, float end_y){ ? ? ? ?boolean isLeftOrRight = (Math.abs(start_x-end_x)>Math.abs(start_y-end_y))?true:false; ? ? ? ?if(isLeftOrRight){ ? ? ? ? ? ?boolean isLeft = start_x-end_x>0?true:false; ? ? ? ? ? ?if(isLeft){ ? ? ? ? ? ? ? ?return 3; ? ? ? ? ? ?}else { ? ? ? ? ? ? ? ?return 4; ? ? ? ? ? ?} ? ? ? ?}else { ? ? ? ? ? ?boolean isUp = start_y-end_y>0?true:false; ? ? ? ? ? ?if(isUp){ ? ? ? ? ? ? ? ?return 1; ? ? ? ? ? ?}else { ? ? ? ? ? ? ? ?return 2; ? ? ? ? ? ?} ? ? ? ?} ? ?} ? ?public void setNullImageView(ImageView mImageView){ ? ? ? ?mImageView.setImageBitmap(null); ? ? ? ?iv_null_ImageView = mImageView; ? ?} ? ?public void changeDataByImageView(final ImageView mImageView){ ? ? ? ?changeDataByImageView(mImageView,true); ? ?} ? ?public void changeDataByImageView(final ImageView mImageView, boolean isAnim){ ? ? ? ?if(isAnimRun){ ? ? ? ? ? ?return; ? ? ? ?} ? ? ? ?if(!isAnim){ ? ? ? ? ? ?GameData mGameData = (GameData) mImageView.getTag(); ? ? ? ? ? ?iv_null_ImageView.setImageBitmap(mGameData.bm); ? ? ? ? ? ?GameData mNullGameData = (GameData) iv_null_ImageView.getTag(); ? ? ? ? ? ?mNullGameData.bm = mGameData.bm; ? ? ? ? ? ?mNullGameData.p_x = mGameData.p_x; ? ? ? ? ? ?mNullGameData.p_y = mGameData.p_y; ? ? ? ? ? ?setNullImageView(mImageView);//設(shè)置當(dāng)前點(diǎn)擊的是空方塊 ? ? ? ? ? ?if(isGameStart){ ? ? ? ? ? ? ? ?//成功時(shí),彈出Toast ? ? ? ? ? ? ? ?isGameOver(); ? ? ? ? ? ?} ? ? ? ? ? ?return; ? ? ? ?} ? ? ? ?TranslateAnimation translateAnimation = null; ? ? ? ?if(mImageView.getX()>iv_null_ImageView.getX()){ ? ? ? ? ? ?translateAnimation = new TranslateAnimation(0.1f,-mImageView.getWidth(),0.1f,0.1f); ? ? ? ?}else if(mImageView.getX()<iv_null_ImageView.getX()){ ? ? ? ? ? ?translateAnimation = new TranslateAnimation(0.1f,mImageView.getWidth(),0.1f,0.1f); ? ? ? ?}else if(mImageView.getY()>iv_null_ImageView.getY()){ ? ? ? ? ? ?translateAnimation = new TranslateAnimation(0.1f,0.1f,0.1f,-mImageView.getWidth()); ? ? ? ?}else if(mImageView.getY()<iv_null_ImageView.getY()){ ? ? ? ? ? ?translateAnimation = new TranslateAnimation(0.1f,0.1f,0.1f,mImageView.getWidth()); ? ? ? ?} ? ? ? ?//設(shè)置動(dòng)畫(huà)時(shí)長(zhǎng) ? ? ? ?translateAnimation.setDuration(70); ? ? ? ?//結(jié)束設(shè)置后是否停留 ? ? ? ?translateAnimation.setFillAfter(true); ? ? ? ?translateAnimation.setAnimationListener(new Animation.AnimationListener() { ? ? ? ? ? ?@Override ? ? ? ? ? ?public void onAnimationStart(Animation animation) { ? ? ? ? ? ? ? ?isAnimRun = true; ? ? ? ? ? ?} ? ? ? ? ? ?@Override ? ? ? ? ? ?public void onAnimationEnd(Animation animation) { ? ? ? ? ? ? ? ?isAnimRun = false; ? ? ? ? ? ? ? ?mImageView.clearAnimation(); ? ? ? ? ? ? ? ?GameData mGameData = (GameData) mImageView.getTag(); ? ? ? ? ? ? ? ?iv_null_ImageView.setImageBitmap(mGameData.bm); ? ? ? ? ? ? ? ?GameData mNullGameData = (GameData) iv_null_ImageView.getTag(); ? ? ? ? ? ? ? ?mNullGameData.bm = mGameData.bm; ? ? ? ? ? ? ? ?mNullGameData.p_x = mGameData.p_x; ? ? ? ? ? ? ? ?mNullGameData.p_y = mGameData.p_y; ? ? ? ? ? ? ? ?setNullImageView(mImageView);//設(shè)置當(dāng)前點(diǎn)擊的是空方塊 ? ? ? ? ? ? ? ?if(isGameStart){ ? ? ? ? ? ? ? ? ? ?//成功時(shí),彈出Toast ? ? ? ? ? ? ? ? ? ?isGameOver(); ? ? ? ? ? ? ? ?} ? ? ? ? ? ?} ? ? ? ? ? ?@Override ? ? ? ? ? ?public void onAnimationRepeat(Animation animation) { ? ? ? ? ? ?} ? ? ? ?}); ? ? ? ?//執(zhí)行動(dòng)畫(huà) ? ? ? ?mImageView.startAnimation(translateAnimation); ? ?} ? ?public boolean isHasByNullImageView(ImageView mImageView){ ? ? ? ?GameData mNullGameData = (GameData) iv_null_ImageView.getTag(); ? ? ? ?GameData mGameData = (GameData) mImageView.getTag(); ? ? ? ?if(mNullGameData.y==mGameData.y && mGameData.x+1==mNullGameData.x){ ? ? ? ? ? ?return true; ? ? ? ?}else if(mNullGameData.y==mGameData.y && mGameData.x-1==mNullGameData.x){ ? ? ? ? ? ?return true; ? ? ? ?}else if(mNullGameData.y==mGameData.y+1 && mGameData.x==mNullGameData.x){ ? ? ? ? ? ?return true; ? ? ? ?}else if(mNullGameData.y==mGameData.y-1 && mGameData.x==mNullGameData.x){ ? ? ? ? ? ?return true; ? ? ? ?} ? ? ? ?return false; ? ?} ? ?class GameData{ ? ? ? ?public int x = 0; ? ? ? ?public int y = 0; ? ? ? ?public Bitmap bm; ? ? ? ?public int p_x = 0; ? ? ? ?public int p_y = 0; ? ? ? ?public GameData(int x, int y, Bitmap bm) { ? ? ? ? ? ?super(); ? ? ? ? ? ?this.x = x; ? ? ? ? ? ?this.y = y; ? ? ? ? ? ?this.bm = bm; ? ? ? ? ? ?this.p_x = x; ? ? ? ? ? ?this.p_y = y; ? ? ? ?} ? ? ? ?//判斷游戲是否結(jié)束的方法 ? ? ? ?public boolean isTrue() { ? ? ? ? ? ?if(x==p_x&&y==p_y){ ? ? ? ? ? ? ? ?return true; ? ? ? ? ? ?} ? ? ? ? ? ?return false; ? ? ? ?} ? ?}}
舉報(bào)
實(shí)現(xiàn)一個(gè)支持手勢(shì)的移動(dòng)拼圖小游戲,手把手帶你開(kāi)發(fā)小游戲
1 回答手勢(shì)設(shè)置的那一張為什么只能在空白處滑動(dòng)才可以?
2 回答圖片不能換行
1 回答為什么我做出來(lái)的東西,左右點(diǎn)擊實(shí)現(xiàn)不了呢? 它智能在對(duì)角線移動(dòng)
2 回答不能生成手勢(shì)點(diǎn)擊代碼
2 回答資源在哪?有源碼嗎?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2016-11-08
抱歉,慕課不許我發(fā)源碼,你可以看看其它同學(xué)做的。
zzlandroid
照著老師的教程敲的一個(gè)樣例:https://github.com/kiritozzl/PuzzleGame
王正一
我實(shí)現(xiàn)的項(xiàng)目源碼:https://github.com/wangzhengyi/HYPinTu
慕粉3288933
我寫(xiě)了代碼,地址在https://github.com/jowang2016/pintumooc 但是判斷程序結(jié)束那里總是沒(méi)有提示
2017-09-06
public class MainActivity extends Activity {
? ?private boolean isAnimRun = false;
? ?//判斷游戲是否開(kāi)始
? ?private boolean isGameStart = false;
? ?private ImageView [] [] iv_game_arr = new ImageView[3][5];
? ?private GridLayout gl_main_game;
? ?private ImageView iv_null_ImageView;
? ?private GestureDetector mDetector;
? ?@Override
? ?public boolean onTouchEvent(MotionEvent event) {
? ? ? ?return mDetector.onTouchEvent(event);
? ?}
? ?@Override
? ?public boolean dispatchTouchEvent(MotionEvent ev) {
? ? ? ?mDetector.onTouchEvent(ev);
? ? ? ?return super.dispatchTouchEvent(ev);
? ?}
? ?@Override
? ?protected void onCreate(Bundle savedInstanceState) {
? ? ? ?super.onCreate(savedInstanceState);
? ? ? ?setContentView(R.layout.activity_main);
? ? ? ?mDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public boolean onDown(MotionEvent motionEvent) {
? ? ? ? ? ? ? ?return false;
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onShowPress(MotionEvent motionEvent) {
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?public boolean onSingleTapUp(MotionEvent motionEvent) {
? ? ? ? ? ? ? ?return false;
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
? ? ? ? ? ? ? ?return false;
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onLongPress(MotionEvent motionEvent) {
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
? ? ? ? ? ? ? ?int type = getDirByGes(motionEvent.getX(),motionEvent.getY(),motionEvent1.getX(),motionEvent1.getY());
// ? ? ? ? ? ? ? ?Toast.makeText(MainActivity.this,""+type,Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ?changeByDir(type);
? ? ? ? ? ? ? ?return false;
? ? ? ? ? ?}
? ? ? ?});
? ? ? ?Bitmap bigBm = ((BitmapDrawable) getResources().getDrawable(R.drawable.ic_game_tu)).getBitmap();
? ? ? ?int tuWandH = bigBm.getWidth()/5;
? ? ? ?int ivWandH = getWindowManager().getDefaultDisplay().getWidth()/5;
? ? ? ?for (int i=0;i<iv_game_arr.length;i++){
? ? ? ? ? ?for(int j=0;j<iv_game_arr[0].length;j++){
? ? ? ? ? ? ? ?Bitmap bm = Bitmap.createBitmap(bigBm,j*tuWandH,i*tuWandH,tuWandH,tuWandH);
? ? ? ? ? ? ? ?iv_game_arr[i][j]=new ImageView(this);
? ? ? ? ? ? ? ?iv_game_arr[i][j].setImageBitmap(bm);
? ? ? ? ? ? ? ?iv_game_arr[i][j].setLayoutParams(new RelativeLayout.LayoutParams(ivWandH,ivWandH));
? ? ? ? ? ? ? ?iv_game_arr[i][j].setPadding(2,2,2,2);
? ? ? ? ? ? ? ?iv_game_arr[i][j].setTag(new GameData(i,j,bm));
? ? ? ? ? ? ? ?iv_game_arr[i][j].setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? ? ? ? ?@Override
? ? ? ? ? ? ? ? ? ?public void onClick(View v) {
? ? ? ? ? ? ? ? ? ? ? ?boolean flag = isHasByNullImageView((ImageView)v);
? ? ? ? ? ? ? ? ? ? ? ?//Toast.makeText(MainActivity.this,"位置關(guān)系是否存在:"+flag,Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? ? ?if(flag){
? ? ? ? ? ? ? ? ? ? ? ? ? ?changeDataByImageView((ImageView) v);
? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?});
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?gl_main_game = (GridLayout) findViewById(R.id.gl_main_game);
? ? ? ?for(int i=0;i<iv_game_arr.length;i++){
? ? ? ? ? ?for(int j=0;j<iv_game_arr[0].length;j++){
? ? ? ? ? ? ? ?gl_main_game.addView(iv_game_arr[i][j]);
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?//設(shè)置最后一個(gè)方塊為空
? ? ? ?setNullImageView(iv_game_arr[2][4]);
? ? ? ?//隨機(jī)打亂順序
? ? ? ?randomMove();
? ? ? ?isGameStart = true;
? ?}
? ?//隨機(jī)打亂順序
? ?public void randomMove(){
? ? ? ?for(int i=0;i<100;i++){
? ? ? ? ? ?int type = (int) ((Math.random()*4)+1);
? ? ? ? ? ?changeByDir(type,false);
? ? ? ?}
? ?}
? ?public void changeByDir(int type){
? ? ? ? changeByDir(type,true);
? ?}
? ?public void changeByDir(int type ,boolean isAnim){
? ? ? ?GameData mNullGameData = (GameData) iv_null_ImageView.getTag();
? ? ? ?int new_x = mNullGameData.x;
? ? ? ?int new_y = mNullGameData.y;
? ? ? ?if(type == 1){
? ? ? ? ? ?new_x++;
? ? ? ?}else if(type == 2){
? ? ? ? ? ?new_x--;
? ? ? ?}else if(type == 3){
? ? ? ? ? ?new_y++;
? ? ? ?}else if(type == 4){
? ? ? ? ? ?new_y--;
? ? ? ?}
? ? ? ?if(new_x>=0&&new_x<iv_game_arr.length&&new_y>=0&&new_y<iv_game_arr[0].length){
? ? ? ? ? ?if(isAnim){
? ? ? ? ? ? ? ?changeDataByImageView(iv_game_arr[new_x][new_y]);
? ? ? ? ? ?}else {
? ? ? ? ? ? ? ?changeDataByImageView(iv_game_arr[new_x][new_y],isAnim);
? ? ? ? ? ?}
? ? ? ?}else {
? ? ? ?}
? ?}
? ?//判斷結(jié)束的方法
? ?public void isGameOver(){
? ? ? ?boolean isGameOver = true;
? ? ? ?for(int i=0;i<iv_game_arr.length;i++){
? ? ? ? ? ?for(int j=0;j<iv_game_arr[0].length;j++){
? ? ? ? ? ? ? ?if(iv_game_arr[i][j]==iv_null_ImageView){
? ? ? ? ? ? ? ? ? ?continue;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?GameData mGameData = (GameData) iv_game_arr[i][j].getTag();
? ? ? ? ? ? ? ?if(!mGameData.isTrue()){
? ? ? ? ? ? ? ? ? ?isGameOver = false;
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ?if(isGameOver){
? ? ? ? ? ?Toast.makeText(MainActivity.this,"游戲結(jié)束",Toast.LENGTH_SHORT).show();
? ? ? ?}
? ?}
? ?public int getDirByGes(float start_x, float start_y, float end_x, float end_y){
? ? ? ?boolean isLeftOrRight = (Math.abs(start_x-end_x)>Math.abs(start_y-end_y))?true:false;
? ? ? ?if(isLeftOrRight){
? ? ? ? ? ?boolean isLeft = start_x-end_x>0?true:false;
? ? ? ? ? ?if(isLeft){
? ? ? ? ? ? ? ?return 3;
? ? ? ? ? ?}else {
? ? ? ? ? ? ? ?return 4;
? ? ? ? ? ?}
? ? ? ?}else {
? ? ? ? ? ?boolean isUp = start_y-end_y>0?true:false;
? ? ? ? ? ?if(isUp){
? ? ? ? ? ? ? ?return 1;
? ? ? ? ? ?}else {
? ? ? ? ? ? ? ?return 2;
? ? ? ? ? ?}
? ? ? ?}
? ?}
? ?public void setNullImageView(ImageView mImageView){
? ? ? ?mImageView.setImageBitmap(null);
? ? ? ?iv_null_ImageView = mImageView;
? ?}
? ?public void changeDataByImageView(final ImageView mImageView){
? ? ? ?changeDataByImageView(mImageView,true);
? ?}
? ?public void changeDataByImageView(final ImageView mImageView, boolean isAnim){
? ? ? ?if(isAnimRun){
? ? ? ? ? ?return;
? ? ? ?}
? ? ? ?if(!isAnim){
? ? ? ? ? ?GameData mGameData = (GameData) mImageView.getTag();
? ? ? ? ? ?iv_null_ImageView.setImageBitmap(mGameData.bm);
? ? ? ? ? ?GameData mNullGameData = (GameData) iv_null_ImageView.getTag();
? ? ? ? ? ?mNullGameData.bm = mGameData.bm;
? ? ? ? ? ?mNullGameData.p_x = mGameData.p_x;
? ? ? ? ? ?mNullGameData.p_y = mGameData.p_y;
? ? ? ? ? ?setNullImageView(mImageView);//設(shè)置當(dāng)前點(diǎn)擊的是空方塊
? ? ? ? ? ?if(isGameStart){
? ? ? ? ? ? ? ?//成功時(shí),彈出Toast
? ? ? ? ? ? ? ?isGameOver();
? ? ? ? ? ?}
? ? ? ? ? ?return;
? ? ? ?}
? ? ? ?TranslateAnimation translateAnimation = null;
? ? ? ?if(mImageView.getX()>iv_null_ImageView.getX()){
? ? ? ? ? ?translateAnimation = new TranslateAnimation(0.1f,-mImageView.getWidth(),0.1f,0.1f);
? ? ? ?}else if(mImageView.getX()<iv_null_ImageView.getX()){
? ? ? ? ? ?translateAnimation = new TranslateAnimation(0.1f,mImageView.getWidth(),0.1f,0.1f);
? ? ? ?}else if(mImageView.getY()>iv_null_ImageView.getY()){
? ? ? ? ? ?translateAnimation = new TranslateAnimation(0.1f,0.1f,0.1f,-mImageView.getWidth());
? ? ? ?}else if(mImageView.getY()<iv_null_ImageView.getY()){
? ? ? ? ? ?translateAnimation = new TranslateAnimation(0.1f,0.1f,0.1f,mImageView.getWidth());
? ? ? ?}
? ? ? ?//設(shè)置動(dòng)畫(huà)時(shí)長(zhǎng)
? ? ? ?translateAnimation.setDuration(70);
? ? ? ?//結(jié)束設(shè)置后是否停留
? ? ? ?translateAnimation.setFillAfter(true);
? ? ? ?translateAnimation.setAnimationListener(new Animation.AnimationListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onAnimationStart(Animation animation) {
? ? ? ? ? ? ? ?isAnimRun = true;
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onAnimationEnd(Animation animation) {
? ? ? ? ? ? ? ?isAnimRun = false;
? ? ? ? ? ? ? ?mImageView.clearAnimation();
? ? ? ? ? ? ? ?GameData mGameData = (GameData) mImageView.getTag();
? ? ? ? ? ? ? ?iv_null_ImageView.setImageBitmap(mGameData.bm);
? ? ? ? ? ? ? ?GameData mNullGameData = (GameData) iv_null_ImageView.getTag();
? ? ? ? ? ? ? ?mNullGameData.bm = mGameData.bm;
? ? ? ? ? ? ? ?mNullGameData.p_x = mGameData.p_x;
? ? ? ? ? ? ? ?mNullGameData.p_y = mGameData.p_y;
? ? ? ? ? ? ? ?setNullImageView(mImageView);//設(shè)置當(dāng)前點(diǎn)擊的是空方塊
? ? ? ? ? ? ? ?if(isGameStart){
? ? ? ? ? ? ? ? ? ?//成功時(shí),彈出Toast
? ? ? ? ? ? ? ? ? ?isGameOver();
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onAnimationRepeat(Animation animation) {
? ? ? ? ? ?}
? ? ? ?});
? ? ? ?//執(zhí)行動(dòng)畫(huà)
? ? ? ?mImageView.startAnimation(translateAnimation);
? ?}
? ?public boolean isHasByNullImageView(ImageView mImageView){
? ? ? ?GameData mNullGameData = (GameData) iv_null_ImageView.getTag();
? ? ? ?GameData mGameData = (GameData) mImageView.getTag();
? ? ? ?if(mNullGameData.y==mGameData.y && mGameData.x+1==mNullGameData.x){
? ? ? ? ? ?return true;
? ? ? ?}else if(mNullGameData.y==mGameData.y && mGameData.x-1==mNullGameData.x){
? ? ? ? ? ?return true;
? ? ? ?}else if(mNullGameData.y==mGameData.y+1 && mGameData.x==mNullGameData.x){
? ? ? ? ? ?return true;
? ? ? ?}else if(mNullGameData.y==mGameData.y-1 && mGameData.x==mNullGameData.x){
? ? ? ? ? ?return true;
? ? ? ?}
? ? ? ?return false;
? ?}
? ?class GameData{
? ? ? ?public int x = 0;
? ? ? ?public int y = 0;
? ? ? ?public Bitmap bm;
? ? ? ?public int p_x = 0;
? ? ? ?public int p_y = 0;
? ? ? ?public GameData(int x, int y, Bitmap bm) {
? ? ? ? ? ?super();
? ? ? ? ? ?this.x = x;
? ? ? ? ? ?this.y = y;
? ? ? ? ? ?this.bm = bm;
? ? ? ? ? ?this.p_x = x;
? ? ? ? ? ?this.p_y = y;
? ? ? ?}
? ? ? ?//判斷游戲是否結(jié)束的方法
? ? ? ?public boolean isTrue() {
? ? ? ? ? ?if(x==p_x&&y==p_y){
? ? ? ? ? ? ? ?return true;
? ? ? ? ? ?}
? ? ? ? ? ?return false;
? ? ? ?}
? ?}
}