課程
/移動(dòng)開(kāi)發(fā)
/Android
/快速實(shí)現(xiàn)不一樣的移動(dòng)拼圖
為啥我點(diǎn)哪個(gè)方塊返回的都是false?代碼比著老師的打的。
2016-08-04
源自:快速實(shí)現(xiàn)不一樣的移動(dòng)拼圖 2-2
正在回答
GameData?mNullGameData?=?(GameData)?iv_null_ImageView.getTag();//?空的
qq_Dazzlingly_0 提問(wèn)者
代碼貼出來(lái)我看看。
package com.example.yidongpintu;
import com.example.yidongpintu.R;
import java.security.PublicKey;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity {
//利用二維數(shù)組創(chuàng)建若干個(gè)小方塊
private ImageView[][] iv_game_arr= new ImageView[3][5];
private GridLayout gl_main_game;//游戲主界面
private ImageView iv_null_ImageView;//當(dāng)前空方塊實(shí)例的保存
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? Bitmap bigBm=((BitmapDrawable)getResources().getDrawable(R.drawable.ic_game_tu)).getBitmap();
? ? ? ? int tuWandH=bigBm.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);//設(shè)置每一個(gè)游戲小方塊的圖案
? ? ?iv_game_arr[i][j].setPadding(2, 2, 2, 2);//設(shè)置方塊之間的間隙
? ? ?iv_game_arr[i][j].setTag(new GameData(i, j, bm));//保定自定義的數(shù)據(jù)
? ? ?iv_game_arr[i][j].setOnClickListener(new OnClickListener() {
? ? ?//設(shè)置監(jiān)聽(tīng)
@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);
? ? ?gl_main_game.addView(iv_game_arr[i][j]);
? ? }
? ? setNullImageView(iv_game_arr[2][4]);
? ??
? ? //利用動(dòng)畫結(jié)束之后,交換兩個(gè)方塊的數(shù)據(jù)
? ? //mImageView點(diǎn)擊的方塊
? ? public void changeDataByImageView(final ImageView mImageView){
//創(chuàng)建一個(gè)動(dòng)畫,設(shè)置好方向,移動(dòng)的距離
? TranslateAnimation translateAnimation=null;
? if(mImageView.getX()>iv_null_ImageView.getX()){//當(dāng)前電機(jī)的方塊在空方塊的下面
?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)畫的時(shí)長(zhǎng)
translateAnimation.setDuration(70);
?//設(shè)置動(dòng)畫之后是否停留
? ? translateAnimation.setFillAfter(true);
?//設(shè)置 動(dòng)畫結(jié)束之后要真正的把數(shù)據(jù)交換了
translateAnimation.setAnimationListener(new AnimationListener(){
?public void onAnimationStart(Animation animation) {
?
?}
public void onAnimationRepeat(Animation animation) {
public void onAnimationEnd(Animation animation) {
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)擊的為空方塊
//執(zhí)行動(dòng)畫
mImageView.startAnimation(translateAnimation);
? ? // ?設(shè)置最后一個(gè)方塊為空
private void setNullImageView(ImageView mImageView) {
mImageView.setImageBitmap(null);
iv_null_ImageView=mImageView;
/*
* 判斷當(dāng)前點(diǎn)擊的方塊,是否與空方塊的位置關(guān)系是相鄰關(guān)系
* true是相鄰,false是不相鄰
* mImageView為所點(diǎn)擊的方塊*/
public boolean isHasByNullImageView(ImageView mImageView){
//分別獲取當(dāng)前空方塊的位置與點(diǎn)擊方塊的位置,通過(guò)x,y兩邊都差1的方式判斷
GameData mNullGameData=(GameData)mImageView.getTag();//空的
GameData mGameData=(GameData)mImageView.getTag();//當(dāng)前點(diǎn)擊的
if(mNullGameData.y==mGameData.y&&mNullGameData.x==mGameData.x+1)
{/*當(dāng)時(shí)點(diǎn)擊的方塊在空方塊的上邊*/
return true;}
else if (mNullGameData.y==mGameData.y&&mNullGameData.x==mGameData.x-1)
{/*下邊*/
else if (mNullGameData.y==mGameData.y+1&&mNullGameData.x==mGameData.x)
{/* 左邊*/
else if (mNullGameData.y==mGameData.y-1&&mNullGameData.x==mGameData.x)
{/* 右邊*/
return false;
}/*每個(gè)游戲上要綁定的數(shù)據(jù)*/
class GameData{
/*每個(gè)小方塊的實(shí)際位置x,y
* 每個(gè)小方塊的圖片位置p_x,p_y*/
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;
舉報(bào)
實(shí)現(xiàn)一個(gè)支持手勢(shì)的移動(dòng)拼圖小游戲,手把手帶你開(kāi)發(fā)小游戲
2 回答運(yùn)行時(shí)閃退,高度出了問(wèn)題
4 回答代碼編寫的第一次運(yùn)行
3 回答出現(xiàn)項(xiàng)目停止運(yùn)行了
1 回答為什么運(yùn)行時(shí)布局中只顯示3行4列
1 回答運(yùn)行之后手機(jī)桌面沒(méi)有l(wèi)ogo
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-08-04
2016-08-04
代碼貼出來(lái)我看看。
2016-08-04
package com.example.yidongpintu;
import com.example.yidongpintu.R;
import java.security.PublicKey;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity {
//利用二維數(shù)組創(chuàng)建若干個(gè)小方塊
private ImageView[][] iv_game_arr= new ImageView[3][5];
private GridLayout gl_main_game;//游戲主界面
private ImageView iv_null_ImageView;//當(dāng)前空方塊實(shí)例的保存
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? Bitmap bigBm=((BitmapDrawable)getResources().getDrawable(R.drawable.ic_game_tu)).getBitmap();
? ? ? ? int tuWandH=bigBm.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);//設(shè)置每一個(gè)游戲小方塊的圖案
? ? ?iv_game_arr[i][j].setPadding(2, 2, 2, 2);//設(shè)置方塊之間的間隙
? ? ?iv_game_arr[i][j].setTag(new GameData(i, j, bm));//保定自定義的數(shù)據(jù)
? ? ?iv_game_arr[i][j].setOnClickListener(new OnClickListener() {
? ? ?//設(shè)置監(jiān)聽(tīng)
@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]);
? ? ?}
? ? }
? ? setNullImageView(iv_game_arr[2][4]);
? ??
}
? ? //利用動(dòng)畫結(jié)束之后,交換兩個(gè)方塊的數(shù)據(jù)
? ? //mImageView點(diǎn)擊的方塊
? ? public void changeDataByImageView(final ImageView mImageView){
//創(chuàng)建一個(gè)動(dòng)畫,設(shè)置好方向,移動(dòng)的距離
? TranslateAnimation translateAnimation=null;
? if(mImageView.getX()>iv_null_ImageView.getX()){//當(dāng)前電機(jī)的方塊在空方塊的下面
?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)畫的時(shí)長(zhǎng)
translateAnimation.setDuration(70);
?//設(shè)置動(dòng)畫之后是否停留
? ? translateAnimation.setFillAfter(true);
?//設(shè)置 動(dòng)畫結(jié)束之后要真正的把數(shù)據(jù)交換了
translateAnimation.setAnimationListener(new AnimationListener(){
?public void onAnimationStart(Animation animation) {
?
?}
public void onAnimationRepeat(Animation animation) {
?
?}
public void onAnimationEnd(Animation animation) {
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)擊的為空方塊
}
});
//執(zhí)行動(dòng)畫
mImageView.startAnimation(translateAnimation);
}
? ? // ?設(shè)置最后一個(gè)方塊為空
private void setNullImageView(ImageView mImageView) {
mImageView.setImageBitmap(null);
iv_null_ImageView=mImageView;
}
/*
* 判斷當(dāng)前點(diǎn)擊的方塊,是否與空方塊的位置關(guān)系是相鄰關(guān)系
* true是相鄰,false是不相鄰
* mImageView為所點(diǎn)擊的方塊*/
public boolean isHasByNullImageView(ImageView mImageView){
//分別獲取當(dāng)前空方塊的位置與點(diǎn)擊方塊的位置,通過(guò)x,y兩邊都差1的方式判斷
GameData mNullGameData=(GameData)mImageView.getTag();//空的
GameData mGameData=(GameData)mImageView.getTag();//當(dāng)前點(diǎn)擊的
if(mNullGameData.y==mGameData.y&&mNullGameData.x==mGameData.x+1)
{/*當(dāng)時(shí)點(diǎn)擊的方塊在空方塊的上邊*/
return true;}
else if (mNullGameData.y==mGameData.y&&mNullGameData.x==mGameData.x-1)
{/*下邊*/
return true;}
else if (mNullGameData.y==mGameData.y+1&&mNullGameData.x==mGameData.x)
{/* 左邊*/
return true;}
else if (mNullGameData.y==mGameData.y-1&&mNullGameData.x==mGameData.x)
{/* 右邊*/
return true;}
return false;
}/*每個(gè)游戲上要綁定的數(shù)據(jù)*/
class GameData{
/*每個(gè)小方塊的實(shí)際位置x,y
* 每個(gè)小方塊的圖片位置p_x,p_y*/
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;
}
?
}
}