第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

滑動不進(jìn)行數(shù)據(jù)交互

我最后demo的時(shí)候,點(diǎn)擊空方塊旁邊有圖案的方塊時(shí),有圖案的方塊進(jìn)行了滑動的動畫,但是卻沒有和空方塊進(jìn)行交換,而是直接消失了,這就造成了我點(diǎn)擊的有圖的方塊也變成空的,而之前空方塊依舊是空方塊。。是真的消失了,如果我把一列按順序點(diǎn)下來,這一列就都消失了,后一列就自動補(bǔ)充到這一列來,請問這是什么原因造成的?我的代碼和老師寫的應(yīng)該是一樣的,檢查了好幾遍,都沒有發(fā)現(xiàn)哪里錯(cuò)誤。ps.我用的是AndroidStudio,不知道和這有沒有關(guān)系。

正在回答

5 回答

找到原因了,少了一句話。所以動畫結(jié)束后要交換圖片的時(shí)候,打印發(fā)現(xiàn)綁定的bm都是空的因此設(shè)置顯示的圖片也都是空白的。

this.bm?=?bm;

http://img1.sycdn.imooc.com//580eb06a0001779c06770531.jpg

1 回復(fù) 有任何疑惑可以回復(fù)我~
#1

慕粉4140763 提問者

謝謝老師
2016-10-25 回復(fù) 有任何疑惑可以回復(fù)我~
#2

慕粉4140763 提問者

老師,我修改過后發(fā)現(xiàn)游戲結(jié)束時(shí),我的Toast也沒有彈出來,麻煩您再幫我看一下是哪里的問題,謝謝。
2016-10-25 回復(fù) 有任何疑惑可以回復(fù)我~

是判斷結(jié)束的方法,默認(rèn)應(yīng)該是true,邏輯是默認(rèn)認(rèn)為就是游戲結(jié)束了,當(dāng)遍歷所有方塊發(fā)現(xiàn)有一個(gè)位置不對的時(shí)候都再改為false(這種方法叫開關(guān)變量方法,比如求一個(gè)數(shù)是不是素?cái)?shù)就用了這種方法)

boolean?isGameOver?=?true;

http://img1.sycdn.imooc.com//580ec14200014b7406250350.jpg

1 回復(fù) 有任何疑惑可以回復(fù)我~
#1

慕粉4140763 提問者

噢,明白啦,謝謝老師!
2016-10-25 回復(fù) 有任何疑惑可以回復(fù)我~

和AndroidStudio沒關(guān)系,把整個(gè)Activity代碼貼上來,我?guī)湍阏以?/p>

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

慕粉4140763 提問者

老師,我貼好了,麻煩您看一下
2016-10-24 回復(fù) 有任何疑惑可以回復(fù)我~


? ? ? ?/**利用動畫結(jié)束之后,交換兩個(gè)方塊的數(shù)據(jù)
? ? ? ? * @param mImageView
? ? ? ? * ? ? ? ? ? ? ? ? ? ?點(diǎn)擊的方塊
? ? ? ? * @param isAnim
? ? ? ? * ? ? ? ? ? ? ? ?true:有動畫,false:無動畫
? ? ? ? */
? ?public void changeDataByImageView(final ImageView mImageView,boolean isAnim){
? ? ? ?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)擊的方塊變?yōu)榭辗綁K
? ? ? ? ? ?if (isGameStart){
? ? ? ? ? ? ? ?isGameOver();//成功時(shí),會彈出一個(gè)Toast
? ? ? ? ? ?}
? ? ? ? ? ?return;
? ? ? ?}
? ? ? ?//創(chuàng)建一個(gè)動畫,設(shè)置的方向,以及移動的距離
? ? ? ?TranslateAnimation translateAnimation = null;
? ? ? ?if(mImageView.getX() > iv_null_ImageView.getX()){//當(dāng)前點(diǎn)擊的方塊在空方塊的下邊
? ? ? ? ? ?//往上移動
? ? ? ? ? ?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è)置動畫的時(shí)長
? ? ? ?translateAnimation.setDuration(70);
? ? ? ?//設(shè)置動畫結(jié)束之后是否停留
? ? ? ?translateAnimation.setFillAfter(true);
? ? ? ?//設(shè)置動畫結(jié)束之后,要真正的把數(shù)據(jù)交換了
? ? ? ?translateAnimation.setAnimationListener(new Animation.AnimationListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onAnimationStart(Animation animation) {
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onAnimationRepeat(Animation animation) {
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?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)擊的方塊變?yōu)榭辗綁K
? ? ? ? ? ? ? ?if (isGameStart){
? ? ? ? ? ? ? ? ? ?isGameOver();//成功時(shí),會彈出一個(gè)Toast
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ?});
? ? ? ?//執(zhí)行動畫
? ? ? ?mImageView.startAnimation(translateAnimation);
? ?}

? ?/** 設(shè)置某個(gè)方塊為空方塊
? ? *
? ? * @param mImageView ?當(dāng)前要設(shè)置為空的方塊的實(shí)例
? ? */
? ?public void setNullImageView(ImageView mImageView){
? ? ? ?mImageView.setImageBitmap(null);
? ? ? ?iv_null_ImageView = mImageView;
? ?}
? ?/* ?判斷當(dāng)前點(diǎn)擊的方塊,是否與空方塊的位置關(guān)系是相鄰關(guān)系
? ? * ?@param ?mImageView 所點(diǎn)擊的方塊
? ? * ?@return ?true:相鄰,false:不相鄰
? ? */
? ?public boolean isHasByNullImageView(ImageView mImageView){
? ? ? //分別獲取當(dāng)前空方塊的位置與點(diǎn)擊空方塊的位置,通過x y兩邊都差1的
? ? ? ?GameData mNullGameData = (GameData) iv_null_ImageView.getTag();
? ? ? ?GameData mGameData = (GameData) mImageView.getTag();
? ? ? ?if(mNullGameData.y==mGameData.y&&mGameData.x+1==mNullGameData.x){//當(dāng)前點(diǎn)擊的方塊在空方塊的上邊
? ? ? ? ? ?return true;
? ? ? ?}else if (mNullGameData.y==mGameData.y&&mGameData.x-1==mNullGameData.x){//當(dāng)前點(diǎn)擊的方塊在空方塊的下邊
? ? ? ? ? ?return true;
? ? ? ?}else if (mNullGameData.y==mGameData.y+1&&mGameData.x==mNullGameData.x){//當(dāng)前點(diǎn)擊的方塊在空方塊的左邊
? ? ? ? ? ?return true;
? ? ? ?}else if (mNullGameData.y==mGameData.y-1&&mGameData.x==mNullGameData.x){//當(dāng)前點(diǎn)擊的方塊在空方塊的右邊
? ? ? ? ? ?return true;
? ? ? ?}
? ? ? ?return false;
? ?}
? ?/*每個(gè)游戲小方塊上要綁定的數(shù)據(jù)*/
? ?class GameData{
? ? ? ?/*每個(gè)游戲小方塊的實(shí)際位置x*/
? ? ? ?public int x = 0;
? ? ? ?/*每個(gè)游戲小方塊的實(shí)際位置y*/
? ? ? ?public int y =0;
? ? ? ?/*每個(gè)游戲小方塊的圖片*/
? ? ? ?public Bitmap bm;
? ? ? ?/*每個(gè)游戲小方塊的圖片的位置x*/
? ? ? ?public int p_x = 0;
? ? ? ?/*每個(gè)游戲小方塊的圖片的位置i*/
? ? ? ?public int p_y = 0;
? ? ? ?public GameData(int x,int y,Bitmap bm) {
? ? ? ? ? ?super();
? ? ? ? ? ?this.x = x;
? ? ? ? ? ?this.y = y;
? ? ? ? ? ?this.p_x = x;
? ? ? ? ? ?this.p_y = y;
? ? ? ?}
? ? ? ?/*每個(gè)小方塊的位置,是否是正確的*/
? ? ? ?public boolean isTrue() {
? ? ? ? ? ?if (x == p_x && y == p_y){
? ? ? ? ? ? ? ?return true;
? ? ? ? ? ?}
? ? ? ? ? ?return false;
? ? ? ?}
? ?}
}

0 回復(fù) 有任何疑惑可以回復(fù)我~

package imooc.a01_pintu;

public class MainActivity extends AppCompatActivity {
// ? ?判斷游戲是否開始
? ?private boolean isGameStart = false;

// ? ?利用二維數(shù)組創(chuàng)建若干個(gè)游戲小方塊
? ?private ImageView[][] iv_game_arr = new ImageView[3][5];
// ? ?游戲主界面
? ?private GridLayout gl_main_game;
// ? ?當(dāng)前空方塊的實(shí)例的保存
? ?private ImageView iv_null_ImageView;
? ?/*當(dāng)前手勢*/
? ?private GestureDetector mDetector;

? ?@Override
? ?public boolean onTouchEvent(MotionEvent event) {
? ? ? ?mDetector.onTouchEvent(event);
? ? ? ?return mDetector.onTouchEvent(event);
? ?}


? ?@Override
? ?protected void onCreate(Bundle savedInstanceState) {
? ? ? ?super.onCreate(savedInstanceState);
? ? ? ?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;
? ? ? ? ? ?}
? ? ? ?});
? ? ? ?setContentView(R.layout.activity_main);
// ? ? ? ?初始化游戲的若干個(gè)小方塊
? ? ? ? Bitmap bigBm = ((BitmapDrawable)getResources().getDrawable(R.drawable.kb)).getBitmap();//獲取一張大圖
? ? ? ?int tuWandH = bigBm.getWidth() / 5;//每個(gè)游戲小方塊的寬和高
? ? ? ?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); ?//根據(jù)行和列來切成若干個(gè)游戲小圖片

? ? ? ? ? ? ? ?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 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 );
? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?});
? ? ? ? ? ?}
? ? ? ?}

// ? ? ? ?初始化游戲主界面,并添加若干個(gè)小方塊
? ? ? ?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ī)打亂順序方塊

? ? ? ?randowMove();
? ? ? ?isGameStart = true;
? ?}

? ?/**根據(jù)手勢的方向,獲取空方塊響應(yīng)的相鄰位置如果存在方塊,那么進(jìn)行數(shù)據(jù)交換
? ? * type ?1:上 ? ?2:下 ? ?3:左 ? 4:右
? ? */
? ?public void changeByDir(int type){
? ? ? ?changeByDir(type,true);
? ?}


? ?/**根據(jù)手勢的方向,獲取空方塊響應(yīng)的相鄰位置如果存在方塊,那么進(jìn)行數(shù)據(jù)交換
? ? * @param ?type ?1:上 ? ?2:下 ? ?3:左 ? 4:右
? ? * @param ?isAnim ?true:有動畫,false:沒有動畫
? ? */
? ?public void changeByDir(int type,boolean isAnim){
? ? ? ?//獲取當(dāng)前空方塊的位置
? ? ? ?GameData mNullGameData = (GameData) iv_null_ImageView.getTag();
? ? ? ?//根據(jù)方向,設(shè)置響應(yīng)的相鄰的位置的坐標(biāo)
? ? ? ?int new_x = mNullGameData.x;
? ? ? ?int new_y = mNullGameData.y;
? ? ? ?if (type == 1){//要移動的方塊在當(dāng)前空方塊的下邊
? ? ? ? ? ?new_x++;
? ? ? ?}else if (type == 2){//要移動的方塊在當(dāng)前空方塊的上邊
? ? ? ? ? ?new_x--;
? ? ? ?}else if (type == 3){//要移動的方塊在當(dāng)前空方塊的左邊
? ? ? ? ? ?new_y++;
? ? ? ?}else if (type == 4){//要移動的方塊在當(dāng)前空方塊的右邊
? ? ? ? ? ?new_y--;
? ? ? ?}
? ? ? ?//判斷這個(gè)新坐標(biāo),是否存在
? ? ? ?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 = false;
? ? ? ?//要遍歷每個(gè)游戲小方塊
? ? ? ?for (int i = 0; i < iv_game_arr.length; i++) {
? ? ? ? ? ?for (int j = 0; j < iv_game_arr[0].length; j ++){
? ? ? ? ? ? ? ?//為空的方塊數(shù)據(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;
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}

? ? ? ?}

? ? ? ?//根據(jù)一個(gè)開關(guān)變量決定游戲是否結(jié)束,結(jié)束時(shí)給提示
? ? ? ?if (isGameOver){
? ? ? ? ? ?Toast.makeText(this,"游戲結(jié)束",Toast.LENGTH_SHORT).show();
? ? ? ?}
? ?}

? ?/**手勢判斷,是向左滑,還是向右滑
? ? * @param start_x ?手勢的起始點(diǎn)x
? ? * @param start_y ?手勢的起始點(diǎn)y
? ? * @param end_x ? ?手勢的中止點(diǎn)x
? ? * @param end_y ? ?手勢的中止點(diǎn)y
? ? * @return ? 1:上 ? ?2:下 ? ?3:左 ? 4:右
? ? */
? ?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;
? ? ? ? ? ?}
? ? ? ?}
? ?}
? ?//隨機(jī)打亂順序
? ?public void randowMove(){
? ? ? ?//打亂的次數(shù)
? ? ? ?for (int i = 0; i < 10 ; i++) {
? ? ? ? ? ?//根據(jù)手勢開始交換,無動畫
? ? ? ? ? ?int type = (int) (Math.random() * 4)+1;
? ? ? ? ? ?changeByDir(type,false);
? ? ? ?}
? ?}

? ?/**利用動畫結(jié)束之后,交換兩個(gè)方塊的數(shù)據(jù)
? ? * @param mImageView ?點(diǎn)擊的方塊
? ? */
? ?public void changeDataByImageView(final ImageView mImageView) {
? ? ? ?changeDataByImageView(mImageView,true);
? ?}

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

滑動不進(jìn)行數(shù)據(jù)交互

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號