那是没有设置好吧
这个难道要用set()吗?
我和你一样,你怎么解决了?在做实训,求教教我
把图片放小一点啊,真是的
换一种新的图片加载框架,是不是图片大小过大
数组变成5*5的数组不就可以了吗
就是图片过大 换成了 555*333就好了
在构造函数是没有对bm初始化
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.baiyun.pitu.MainActivity" android:background="#f79b11"> <GridLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/gl_main_game" android:columnCount="5" android:rowCount="3"></GridLayout> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/ic_game_tu" android:layout_marginRight="10dp" android:layout_marginLeft="10dp" android:layout_marginTop="200dp"/> </RelativeLayout>
没有。
int displayWidthPixels = getResources().getDisplayMetrics().widthPixels; int displayHeightPixels = getResources().getDisplayMetrics().heightPixels; float density = getResources().getDisplayMetrics().density;
1、获取到屏幕像素宽度和高度;
2、获取到图片dpi宽高;
3、搜图片转换方法,将图片缩放到和屏幕大小一致。
好像也没报错,就是移动不了
调试时已修改过来
public class MainActivity extends Activity {
private boolean isAnimRun = false;
//判断游戏是否开始
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,"位置关系是否存在:"+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]);
//随机打乱顺序
randomMove();
isGameStart = true;
}
//随机打乱顺序
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 {
}
}
//判断结束的方法
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,"游戏结束",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);//设置当前点击的是空方块
if(isGameStart){
//成功时,弹出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());
}
//设置动画时长
translateAnimation.setDuration(70);
//结束设置后是否停留
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);//设置当前点击的是空方块
if(isGameStart){
//成功时,弹出Toast
isGameOver();
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
//执行动画
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;
}
//判断游戏是否结束的方法
public boolean isTrue() {
if(x==p_x&&y==p_y){
return true;
}
return false;
}
}
}
兄弟 你看清楚啊
在AndroidManifest.xml的启动activity标签内增加
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
if (isAnim) {
changeDataByImageView(iv_game_arr[new_x][new_y]);
} else {
changeDataByImageView(iv_game_arr[new_x][new_y]);
}
changeDataByImageView(iv_game_arr[new_x][new_y],isAnim);
} else {
// 什么也不做
}
你这里多了个else
应该是:
if (isAnim) {
changeDataByImageView(iv_game_arr[new_x][new_y]);
} else {
changeDataByImageView(iv_game_arr[new_x][new_y],isAnim);
}
} else {
//什么也不做
}
看看崩溃日志会有提示,猜测是图片太大了,可以利用http://www.webps.cn/
剪裁一张图片大小333*555像素的应该就可以了。
我自己用你的代码+下面的图片测试了一下,是可以的。
好吧,解决了,AndroidStudio中默认的包是android.icu.text.SimpleDateFormat;
而我把包替换成了java.text.SimpleDateFormat; 就解决了。
x 和 y 用来判断最终游戏结束时,与对应的二维数组图片的下标是否一致来决定游戏是否成功。
p_x 和 p_y 用来表示图片所在的位置,当点击某个图片的时候我想知道当前图片与空方块的位置(是左边还是右边等……)
解决的方法不唯一,只要你理解了一个View可以通过这种方式绑定多个数据(通常一个tag只能绑定一个值,但如果是这种class的GameData类型就可以绑定多个值)这种方式,自己设计一下也许能设计的比我设计的更好哈。
抱歉,慕课不许我发源码,你可以看看其它同学做的。
zzlandroid
照着老师的教程敲的一个样例:https://github.com/kiritozzl/PuzzleGame
王正一
我实现的项目源码:https://github.com/wangzhengyi/HYPinTu
慕粉3288933
我写了代码,地址在https://github.com/jowang2016/pintumooc 但是判断程序结束那里总是没有提示
AndroidManifest.xml里设置的 android:minSdkVersion的最小值要大于等于14才行。
别的布局不行,因为我这个代码是针对GridLayout做的。