qq_一岁一枯荣丶_0
2018-09-06 01:17
package com.example.lee.pintu; import android.content.pm.ActivityInfo; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.os.TransactionTooLargeException; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.GridLayout; import android.widget.ImageView; import android.widget.Toast; import java.security.PublicKey; import android.view.GestureDetector; public class MainActivity extends AppCompatActivity { private ImageView[][] iv_game_arr = new ImageView[3][5]; private GridLayout gl_main_game; private ImageView iv_null_Imageview; //private boolean isAnim = false; /* * 手势 * */ private GestureDetector mDetector; @Override /* * 设置手势监听*/ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// 设置打开为 横竖屏 setContentView(R.layout.activity_main); gl_main_game = findViewById(R.id.gl_main_game); Bitmap bigBm = ((BitmapDrawable) getResources().getDrawable(R.mipmap.pintu)).getBitmap(); int tuwandH = bigBm.getWidth() / 5; mDetector=new GestureDetector(this, new GestureDetector.OnGestureListener() { @Override public boolean onDown(MotionEvent e) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { int type=getDirByGes(e1.getX(),e1.getY(),e2.getX(),e2.getY()); // Toast.makeText(MainActivity.this,""+type,Toast.LENGTH_SHORT).show(); changeByDir(type); return false; } }); /* 初始化方块*/ 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 GameDate(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){ changeDateByImageview((ImageView) v); } } }); } } /*添加至游戏界面*/ 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(); } } @Override public boolean onTouchEvent(MotionEvent event) { return mDetector.onTouchEvent(event); } /** * 1 上 2 下 3 左 4右 * */ //重载 /* public void changeByDir(int type){ changeByDir(type,true); }*/ public void changeByDir(int type/*,boolean isAnim*/){ GameDate mNullGameDate= (GameDate) iv_null_Imageview.getTag(); int new_x=mNullGameDate.x; int new_y=mNullGameDate.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) {*/ changeDateByImageview(iv_game_arr[new_x][new_y]);/* }else{ changeDateByImageview(iv_game_arr[new_x][new_y],isAnim); }*/ }else{ // 不操作 } } public int getDirByGes (float start_x,float start_y,float end_x,float end_y){ boolean isLRorUD=(Math.abs(start_x-end_x)>Math.abs(start_y-end_y))?true:false; if (isLRorUD){ boolean isL=start_x-end_x >0?true:false; if (isL){ return 3; } else { return 4; } } else { boolean isU=start_y-end_y >0?true:false; if (isU){ return 1; } else { return 2; } } } //打乱顺序 /*public void randomMove(){ for (int i=1;i<10;i++){ int type =(int ) (Math.random()*4)+1; changeByDir(type,false); } } */ //方法的重载 /*public void changeDateByImageview(final ImageView imageView){ changeDateByImageview(imageView,true); }*/ public void changeDateByImageview(final ImageView imageView/*,boolean isAnim*/) { /*if (!isAnim){ GameDate mGameDate = (GameDate) imageView.getTag(); iv_null_Imageview.setImageBitmap(mGameDate.bm); GameDate mNullGameDate = (GameDate) iv_null_Imageview.getTag(); mNullGameDate.bm = mGameDate.bm; mNullGameDate.p_X = mGameDate.p_X; mNullGameDate.p_Y = mGameDate.p_Y; setNullImageView(imageView); return; }*/ TranslateAnimation translateAnimation = null; if (imageView.getX() > iv_null_Imageview.getX()) { translateAnimation = new TranslateAnimation(0.1f, -imageView.getWidth(), 0.1f, 0.1f); } else if (imageView.getX() < iv_null_Imageview.getX()) { translateAnimation = new TranslateAnimation(0.1f, +imageView.getWidth(), 0.1f, 0.1f); } else if (imageView.getY() > iv_null_Imageview.getY()) { translateAnimation = new TranslateAnimation(0.1f, 0.1f, 0.1f, -imageView.getWidth()); } else if (imageView.getY() < iv_null_Imageview.getY()) { translateAnimation = new TranslateAnimation(0.1f, 0.1f, 0.1f, +imageView.getWidth()); } translateAnimation.setDuration(70); translateAnimation.setFillAfter(true); // 结束后是否停留 translateAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { imageView.clearAnimation(); GameDate mGameDate = (GameDate) imageView.getTag(); iv_null_Imageview.setImageBitmap(mGameDate.bm); GameDate mNullGameDate = (GameDate) iv_null_Imageview.getTag(); mNullGameDate.bm = mGameDate.bm; mNullGameDate.p_X = mGameDate.p_X; mNullGameDate.p_Y = mGameDate.p_Y; setNullImageView(imageView); } @Override public void onAnimationRepeat(Animation animation) { } }); imageView.startAnimation(translateAnimation); } public void setNullImageView(ImageView mImageView) { mImageView.setImageBitmap(null); iv_null_Imageview=mImageView; } public boolean ishasByNullImageview(ImageView imageView){ GameDate mNullGameDate= (GameDate) iv_null_Imageview.getTag(); GameDate mGameDate= (GameDate) imageView.getTag(); if (mNullGameDate.y==mGameDate.y&&mNullGameDate.x==mGameDate.x+1) { return true; } else if (mNullGameDate.y==mGameDate.y&&mNullGameDate.x==mGameDate.x-1){ return true; } else if (mNullGameDate.y==mGameDate.y+1&&mNullGameDate.x==mGameDate.x){ return true; } else if (mNullGameDate.y==mGameDate.y-1&&mNullGameDate.x==mGameDate.x){ return true; } return false; } class GameDate{ public int x=0; // 方块位置 public int y=0; public Bitmap bm; public int p_X=0; // 图片位置 public int p_Y=0; public GameDate(int x,int y,Bitmap bm){ super(); this.x=x; this.y=y; this.bm=bm; this.p_X=p_X; this.p_Y=p_Y; } } }
我和你一样,你怎么解决了?在做实训,求教教我
GameData的构造方法是不是有问题。
快速实现不一样的移动拼图
15576 学习 · 67 问题
相似问题