欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

Android

當(dāng)前位置:主頁 > 軟件編程 > Android >

Android自定義View實(shí)現(xiàn)拼圖小游戲

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:Android|點(diǎn)擊: 次

本文實(shí)例為大家分享了Android拼圖小游戲的具體代碼,供大家參考,具體內(nèi)容如下

1、效果圖:

運(yùn)行時(shí):

結(jié)束時(shí):

2、PuzzleLayoutView:

public class PuzzleLayoutView extends RelativeLayout implements View.OnClickListener {

  //表示將其切成2*2拼圖(默認(rèn)4塊)
  private int mColumn = 2;
  //容器的內(nèi)邊距
  private int mPadding;
  //每個(gè)塊塊的邊距(橫,縱  3:表示間距為3dp)
  private int mMargin = 3;
  //存儲(chǔ)ImageView
  private ImageView[] mGamePintuItems;
  //Item的寬度(一致)
  private int mItemWidth;
  //游戲的圖片
  private Bitmap mBitmap;
  //切圖后的存儲(chǔ)
  private List<ImagePieceBean> mItemBitmaps;
  //操作次數(shù)
  private boolean once;
  //容器寬度(游戲面板 高寬一致)
  private int mWidth;
  //設(shè)置游戲是否成功
  private boolean isGameSuccess;
  //設(shè)置游戲是否失敗
  private boolean isGameOver;

  public GamePintuListner mListner;

  public PuzzleLayoutView(Context context) {
    this(context, null);
  }

  public PuzzleLayoutView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public PuzzleLayoutView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }

  private void init() {
    mMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3,
        getResources().getDisplayMetrics());//將dp轉(zhuǎn)化為px,或xp轉(zhuǎn)化為px
    mPadding = min(getPaddingLeft(), getPaddingRight(), getPaddingTop(), getPaddingBottom());
  }

  //接口方法
  public interface GamePintuListner {

    void nextLevel(int nextLevel);//下一關(guān)

    void timechanged(int currentTime);//關(guān)卡時(shí)間

    void gameover();//游戲結(jié)束
  }

  public void setOnGamePintuListner(GamePintuListner mListner) {
    this.mListner = mListner;
  }

  private int level = 1;
  private static final int TIME_CHANGED = 0X123;
  private static final int NEXT_LEVEL = 0X124;


  private Handler handler = new Handler() {
    public void handleMessage(android.os.Message msg) {
      switch (msg.what) {
        case TIME_CHANGED:
          if (isGameSuccess || isGameOver)
            return;
          if (mListner != null) {
            mListner.timechanged(mTime);
            //時(shí)間結(jié)束后,游戲結(jié)束
            if (mTime == 0) {
              isGameOver = true;
              mListner.gameover();
            }
          }
          mTime--;
          //延遲1秒發(fā)送
          handler.sendEmptyMessageDelayed(TIME_CHANGED, 1000);
          break;
        case NEXT_LEVEL:
          level = level + 1;//切換到下一關(guān)
          if (mListner != null) {
            mListner.nextLevel(level);
          } else {
            nextLevel();
          }
        default:
          break;
      }
    }
  };


  private boolean isTimeEnabled = false;
  private int mTime;

  /**
   * 設(shè)置是否啟動(dòng)時(shí)間  (默認(rèn)不啟動(dòng))
   *
   * @param isTimeEnabled
   */
  public void setTimeEnabled(boolean isTimeEnabled) {
    this.isTimeEnabled = isTimeEnabled;
  }

  /**
   * 獲取當(dāng)前布局的大小(正方形)
   */
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    //取寬和高中的最小值
    mWidth = Math.min(getMeasuredHeight(), getMeasuredWidth());
    if (!once) {
      //調(diào)用進(jìn)行切圖,以及排序(方法)
      initBitmap();
      //調(diào)用設(shè)置ImageView(Item)的寬高等屬性(方法)
      initItem();
      //判斷是否開啟時(shí)間(方法調(diào)用)
      checkTimeEnable();
      once = true;
    }
    setMeasuredDimension(mWidth, mWidth);//強(qiáng)制調(diào)用使面板為正方形
  }

  /**
   * 判斷是否開啟時(shí)間
   */
  private void checkTimeEnable() {
    if (isTimeEnabled) {
      //根據(jù)當(dāng)前等級(jí)設(shè)置時(shí)間
      countTimeBaseLevel();
      //通知線程更新關(guān)卡時(shí)間
      handler.sendEmptyMessage(TIME_CHANGED);
    }

  }

  private void countTimeBaseLevel() {
    mTime = (int) Math.pow(2, level) * 60;//第一關(guān)120秒 第二關(guān):240 第三關(guān):480
  }

  /**
   * 進(jìn)行切圖,以及排序方法
   */
  private void initBitmap() {
    //將圖片引入
    if (mBitmap == null) {
      mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic_view);//注意此處的導(dǎo)包
    }
    mItemBitmaps = ImageSplitterUtil.sqlitImage(mBitmap, mColumn);//返回長(zhǎng)度為4 (2*2)
    //使用sort進(jìn)行亂排序
    Collections.sort(mItemBitmaps, new Comparator<ImagePieceBean>() {
      public int compare(ImagePieceBean a, ImagePieceBean b) {//注意此處的a,b
        //是否大于0.5具有不確定性
        return Math.random() > 0.5 ? 1 : -1;
      }
    });
  }

  /**
   * 設(shè)置ImageView(Item)的寬高等屬性方法
   */
  private void initItem() {
    //容器的寬度-Item內(nèi)邊距 =所有小塊塊加起來的/Item個(gè)數(shù)(寬度)  2:左邊和右邊邊距
    mItemWidth = (mWidth - mPadding * 2 - mMargin * (mColumn - 1)) / mColumn;
    mGamePintuItems = new ImageView[mColumn * mColumn];//界面塊塊個(gè)數(shù)相*
    //生成我們的Item,設(shè)置Rule(Item間的關(guān)系,高矮等)
    for (int i = 0; i < mGamePintuItems.length; i++) {
      ImageView item = new ImageView(getContext());
      /**
       * item點(diǎn)擊事件
       */
      item.setOnClickListener(this);
      item.setImageBitmap(mItemBitmaps.get(i).getBitmap());//此前以進(jìn)行過亂排序
      mGamePintuItems[i] = item;//保存Item
      item.setId(i + 1);
      //在Item的tag中存儲(chǔ)了index
      item.setTag(i + "_" + mItemBitmaps.get(i).getIndex());
      RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(mItemWidth, mItemWidth);
      //[設(shè)置游戲規(guī)則]
      //設(shè)置Item間橫向間隙,通過rightMargin
      //不是最后一列
      if ((i + 1) % mColumn != 0) {
        lp.rightMargin = mMargin;
      }
      //不是第一列
      if (i % mColumn != 0) {
        lp.addRule(RelativeLayout.RIGHT_OF, mGamePintuItems[i - 1].getId());
      }
      //如果不是第一行,設(shè)置topMargin和rule
      if (i + 1 > mColumn) {
        lp.topMargin = mMargin;
        lp.addRule(RelativeLayout.BELOW, mGamePintuItems[i - mColumn].getId());
      }
      addView(item, lp);//添加到RelativeLayout中
    }
  }

  /**
   * 當(dāng)過關(guān)失敗,時(shí)間停止時(shí)調(diào)用此方法(重新開始此關(guān)卡)
   */
  public void restart() {
    isGameOver = false;//重置當(dāng)前關(guān)卡
    mColumn--;
    nextLevel();
  }

  public void nextLevel() {
    this.removeAllViews();//移除當(dāng)前所有View
    mAnimLayout = null;
    mColumn++;//由2*2 變?yōu)?*3游戲面版
    isGameSuccess = false;//游戲未成功(新的開始)
    checkTimeEnable();//下一關(guān)時(shí)間重新計(jì)算
    initBitmap();
    initItem();
  }

  /**
   * 獲取多個(gè)參數(shù)的最小值
   */
  private int min(int... params) {//...傳多個(gè)參數(shù)
    int min = params[0];//獲取最小的
    for (int param : params) {//發(fā)現(xiàn)最小的則賦值
      if (param < min) {
        min = param;
      }
    }
    return min;
  }

  /**
   * 點(diǎn)擊事件
   */
  private ImageView mFirst;//點(diǎn)擊的IItem
  private ImageView mSecond;

  public void onClick(View v) {
    if (isAniming)
      return;
    //兩次點(diǎn)擊同一個(gè)Item
    if (mFirst == v) {
      mFirst.setColorFilter(null);
      mFirst = null;
      return;
    }
    if (mFirst == null) {
      mFirst = (ImageView) v;
      mFirst.setColorFilter(Color.parseColor("#5551c4d4"));//設(shè)置選中Item時(shí)的顏色(55為半透明)
    } else {
      mSecond = (ImageView) v;
      //交換我們的Item
      exchangeView();
    }
  }

  /**
   * 動(dòng)畫層
   */
  private RelativeLayout mAnimLayout;
  //設(shè)置圖片進(jìn)行切換時(shí)用戶瘋狂點(diǎn)擊
  private boolean isAniming;

  /**
   * 交換我們的Item
   */
  private void exchangeView() {
    mFirst.setColorFilter(null);//去除顏色狀態(tài)(高亮)
    //調(diào)用構(gòu)造我們的動(dòng)畫層方法
    setUpAnimLayout();
    //進(jìn)行圖片的交換
    ImageView first = new ImageView(getContext());
    final Bitmap firstBitmap = mItemBitmaps.get(getImageIdByTag((String) mFirst.getTag())).getBitmap();
    first.setImageBitmap(firstBitmap);
    LayoutParams lp = new LayoutParams(mItemWidth, mItemWidth);
    lp.leftMargin = mFirst.getLeft() - mPadding;
    lp.topMargin = mFirst.getTop() - mPadding;
    first.setLayoutParams(lp);
    mAnimLayout.addView(first);//添加至動(dòng)畫層
    ImageView second = new ImageView(getContext());
    final Bitmap secondBitmap = mItemBitmaps.get(getImageIdByTag((String) mSecond.getTag())).getBitmap();
    second.setImageBitmap(secondBitmap);
    LayoutParams lp2 = new LayoutParams(mItemWidth, mItemWidth);
    lp2.leftMargin = mSecond.getLeft() - mPadding;
    lp2.topMargin = mSecond.getTop() - mPadding;
    second.setLayoutParams(lp2);
    mAnimLayout.addView(second);//添加至動(dòng)畫層

    //設(shè)置動(dòng)畫
    TranslateAnimation animFirst = new TranslateAnimation(0, mSecond.getLeft() - mFirst.getLeft(),
        0, mSecond.getTop() - mFirst.getTop());
    animFirst.setDuration(500);//設(shè)置動(dòng)畫時(shí)間
    animFirst.setFillAfter(true);//設(shè)置動(dòng)畫結(jié)束的位置
    first.startAnimation(animFirst);//啟動(dòng)動(dòng)畫

    TranslateAnimation animSecond = new TranslateAnimation(0, -mSecond.getLeft() + mFirst.getLeft(),
        0, -mSecond.getTop() + mFirst.getTop());
    animSecond.setDuration(500);//設(shè)置動(dòng)畫時(shí)間
    animSecond.setFillAfter(true);//設(shè)置動(dòng)畫結(jié)束的位置
    second.startAnimation(animSecond);//啟動(dòng)動(dòng)畫

    /**
     * 監(jiān)聽動(dòng)畫事件
     */
    animFirst.setAnimationListener(new Animation.AnimationListener() {
      public void onAnimationStart(Animation animation) {
        mFirst.setVisibility(View.INVISIBLE);//隱藏動(dòng)畫
        mSecond.setVisibility(View.INVISIBLE);
        isAniming = true;
      }

      public void onAnimationRepeat(Animation animation) {
      }

      public void onAnimationEnd(Animation animation) {

        String firstTag = (String) mFirst.getTag();
        String secondTag = (String) mSecond.getTag();
        mFirst.setImageBitmap(secondBitmap);
        mSecond.setImageBitmap(firstBitmap);
        mFirst.setTag(secondTag);
        mSecond.setTag(firstTag);
        mFirst.setVisibility(View.VISIBLE);//顯示隱藏的圖片
        mSecond.setVisibility(View.VISIBLE);
        //此處為空,并不是將對(duì)象設(shè)置為null 而是將mFirst與Bitmap對(duì)象鏈接的線斷開
        mFirst = mSecond = null;//回到初始狀態(tài)
        mAnimLayout.removeAllViews();//移除動(dòng)畫層的兩個(gè)View
        //調(diào)用判斷游戲成功時(shí)的方法
        checkSuccess();
        isAniming = false;
      }
    });
  }

  /**
   * 判斷游戲是否成功
   */
  private void checkSuccess() {

    boolean isSuccess = true;
    for (int i = 0; i < mGamePintuItems.length; i++) {
      ImageView imageView = mGamePintuItems[i];
      //getImageIndex:上面的方法名(注意此處方法名)
      if (getImageIndex((String) imageView.getTag()) != i) {
        isSuccess = false;
      }
    }
    if (isSuccess) {
      isGameSuccess = true;
      handler.removeMessages(TIME_CHANGED);//進(jìn)入下一關(guān)時(shí)的時(shí)間
      Log.e("TAG", "SUCCESS");
      Toast.makeText(getContext(), "Success,level up 游戲升級(jí)!!!", Toast.LENGTH_LONG).show();
      handler.sendEmptyMessage(NEXT_LEVEL);
    }
  }

  /**
   * 根據(jù)tag獲取Id
   */
  public int getImageIdByTag(String tag) {
    String[] split = tag.split("_");
    return Integer.parseInt(split[0]);//拿ID
  }

  public int getImageIndex(String tag) {
    String[] split = tag.split("_");
    return Integer.parseInt(split[1]);//拿ID
  }

  /**
   * 構(gòu)造我們的動(dòng)畫層
   */
  private void setUpAnimLayout() {
    if (mAnimLayout == null) {
      mAnimLayout = new RelativeLayout(getContext());
      addView(mAnimLayout);//添加到游戲面板中
    }
  }
}

工具類:ImageSplitterUtil

public class ImageSplitterUtil {

  /**
   * 傳入bitmap,切成piece*piece塊
   */
  public static List<ImagePieceBean> sqlitImage(Bitmap bitmap, int piece) {
    List<ImagePieceBean> ImagePieceBeans = new ArrayList<>();

    int width = bitmap.getWidth();//拿到圖片寬高
    int height = bitmap.getHeight();
    int pieceWidth = Math.min(width, height) / piece;//得到每一塊的寬度

    for (int i = 0; i < piece; i++) {//切第一行
      for (int j = 0; j < piece; j++) {//循環(huán)切第二,三行
        ImagePieceBean ImagePieceBean = new ImagePieceBean();
        ImagePieceBean.setIndex(j + i * piece);//第一次i為0,第0行 j++遞增 0-6
        int x = j * pieceWidth;//第一次循環(huán)X,Y為0
        int y = i * pieceWidth;
        ImagePieceBean.setBitmap(Bitmap.createBitmap(bitmap, x, y, pieceWidth, pieceWidth));
        ImagePieceBeans.add(ImagePieceBean);
      }
    }
    return ImagePieceBeans;
  }
}

實(shí)體類:ImagePieceBean

public class ImagePieceBean {

  private int index;  //表示當(dāng)前第幾塊
  private Bitmap bitmap;  //當(dāng)前圖片

  public ImagePieceBean() {
  }

  //快捷鍵構(gòu)造方法 Source 倒3
  public ImagePieceBean(int index, Bitmap bitmap) {
    this.index = index;
    this.bitmap = bitmap;
  }

  public int getIndex() {
    return index;
  }

  public void setIndex(int index) {
    this.index = index;
  }

  public Bitmap getBitmap() {
    return bitmap;
  }

  public void setBitmap(Bitmap bitmap) {
    this.bitmap = bitmap;
  }

  public String toString() {
    return "ImagePiece [index=" + index + ", bitmap=" + bitmap + "]";
  }
}

3、使用方法:GameActivity

/**
 * 總結(jié):
 * 1.自定義控件選擇,九宮格,RelativeLayout, id+Rule
 * 2.切圖
 * 3.動(dòng)畫圖層
 * 4.pause resume restart
 * 5.游戲時(shí)間 Handler sendMessageDelayed() 延遲一秒發(fā)送線程
 */
public class GameActivity extends AppCompatActivity {

  private PuzzleLayoutView puzzleLayoutView;
  private TextView mLevel, mTime;

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_game);

    mLevel = this.findViewById(R.id.id_level);
    mTime = this.findViewById(R.id.id_time);
    puzzleLayoutView = this.findViewById(R.id.puzzle_layout_view);
    puzzleLayoutView.setTimeEnabled(true);
    //監(jiān)聽事件
    puzzleLayoutView.setOnGamePintuListner(new PuzzleLayoutView.GamePintuListner() {
      public void timechanged(int currentTime) {
        //此處為int 注意加""
        mTime.setText(currentTime + "秒");
      }

      public void nextLevel(final int nextLevel) {
        //彈出提示框
        new AlertDialog.Builder(GameActivity.this).setTitle("游戲信息")
            .setMessage("游戲升級(jí)").setPositiveButton("進(jìn)入下一關(guān)",
            new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                //游戲結(jié)束后,調(diào)用下一關(guān)
                puzzleLayoutView.nextLevel();
                mLevel.setText("第" + +nextLevel + "關(guān)");
              }
            }).show();
      }

      public void gameover() {
        //彈出提示框
        new AlertDialog.Builder(GameActivity.this).setTitle("游戲信息")
            .setMessage("游戲結(jié)束!").setPositiveButton("是否繼續(xù)該關(guān)卡?",
            new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                puzzleLayoutView.restart();//重新啟動(dòng)
              }
            }).setNegativeButton("是否放棄該游戲!", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
            finish();
          }
        }).show();
      }
    });
  }
}

對(duì)應(yīng)布局:activity_game

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_margin="10dp"
  android:gravity="center_horizontal"
  android:orientation="vertical">

  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
      android:id="@+id/id_level"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:text="1"
      android:textSize="25sp"
      android:textStyle="bold" />

    <TextView
      android:id="@+id/id_time"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:gravity="center"
      android:text="120"
      android:textColor="#ea7821"
      android:textSize="25sp"
      android:textStyle="bold" />
  </RelativeLayout>

  <ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:src="@drawable/pic_view" />

  <com.helloworld.game.utils.PuzzleLayoutView
    android:id="@+id/puzzle_layout_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="3dp" />

</LinearLayout>

注意:pic_view圖片資源自行更換

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:解決android設(shè)備斷電重啟后WIFI不能自動(dòng)重連的BUG(收藏)

欄    目:Android

下一篇:Android仿微信錄音功能

本文標(biāo)題:Android自定義View實(shí)現(xiàn)拼圖小游戲

本文地址:http://mengdiqiu.com.cn/a1/Android/9094.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有