运行返回值的问题
为啥我点哪个方块返回的都是false?代码比着老师的打的。
为啥我点哪个方块返回的都是false?代码比着老师的打的。
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 {
//利用二维数组创建若干个小方块
private ImageView[][] iv_game_arr= new ImageView[3][5];
private GridLayout gl_main_game;//游戏主界面
private ImageView iv_null_ImageView;//当前空方块实例的保存
@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);//设置每一个游戏小方块的图案
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 OnClickListener() {
//设置监听
@Override
public void onClick(View v) {
boolean flag = isHasByNullImageView((ImageView) v);
Toast.makeText(MainActivity.this, "位置关系是否存在:"+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]);
}
//利用动画结束之后,交换两个方块的数据
//mImageView点击的方块
public void changeDataByImageView(final ImageView mImageView){
//创建一个动画,设置好方向,移动的距离
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());}
//设置动画的时长
translateAnimation.setDuration(70);
//设置动画之后是否停留
translateAnimation.setFillAfter(true);
//设置 动画结束之后要真正的把数据交换了
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);//设置当前点击的为空方块
}
});
//执行动画
mImageView.startAnimation(translateAnimation);
}
// 设置最后一个方块为空
private void setNullImageView(ImageView mImageView) {
mImageView.setImageBitmap(null);
iv_null_ImageView=mImageView;
}
/*
* 判断当前点击的方块,是否与空方块的位置关系是相邻关系
* true是相邻,false是不相邻
* mImageView为所点击的方块*/
public boolean isHasByNullImageView(ImageView mImageView){
//分别获取当前空方块的位置与点击方块的位置,通过x,y两边都差1的方式判断
GameData mNullGameData=(GameData)mImageView.getTag();//空的
GameData mGameData=(GameData)mImageView.getTag();//当前点击的
if(mNullGameData.y==mGameData.y&&mNullGameData.x==mGameData.x+1)
{/*当时点击的方块在空方块的上边*/
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;
}/*每个游戏上要绑定的数据*/
class GameData{
/*每个小方块的实际位置x,y
* 每个小方块的图片位置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;
}
}
}
举报