<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了android實現簡單拼圖遊戲的具體程式碼,供大家參考,具體內容如下
1.
2.
//使用回撥介面,首先初始化pintuview並繫結,實現回撥介面的方法 mPintuLayout = (PintuLayout) findViewById(R.id.mpintu); mPintuLayout.setOnGamePintuListener(new GamePintuListener() { @Override public void timechanged(int currentTime) { tvTime.setText(currentTime + ""); } @Override public void nextLevel(final int nextLevel) { mtvNextLevel.setVisibility(0); mPintuLayout.pause(); mPintuLayout.nextLevel(); level = nextLevel + ""; } @Override public void gameover() { mtvGameOver.setVisibility(0); } }); } @Override protected void onPause() { super.onPause(); mPintuLayout.pause(); } @Override protected void onResume() { super.onResume(); mPintuLayout.resume(); } // 設定按兩次回退退出程式 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { exit(); Toast.makeText(getApplicationContext(), "再按一次則退出", Toast.LENGTH_SHORT).show(); return false; } return super.onKeyDown(keyCode, event); } private void exit() { if (!isExit) { isExit = true; myHandler.sendEmptyMessageDelayed(0, 2000); } else { finish(); } } Handler myHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); isExit = false; } }; //介面的點選事件 @Override public void onClick(View v) { switch (v.getId()) { case R.id.textView1: mPintuLayout.pause(); Toast.makeText(getApplicationContext(), "Pausing", Toast.LENGTH_SHORT).show(); mPintuLayout.setVisibility(4); miv.setVisibility(0); break; case R.id.textView2: mPintuLayout.resume(); Toast.makeText(getApplicationContext(), "Restarted", Toast.LENGTH_SHORT).show(); mPintuLayout.setVisibility(0); miv.setVisibility(4); break; case R.id.mtvGameOver: mtvGameOver.setVisibility(4); mPintuLayout.restart(); break; case R.id.mtvNextLevel: mtvNextLevel.setVisibility(4); tvLevel.setText("" + level); mPintuLayout.resume(); break; } }
3.每一小塊退片的bean
public class ImagePieces { //小塊圖片的索引值 private int index; //整個一大塊的圖片載體 private 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 ImagePieces(int index, Bitmap bitmap) { super(); this.index = index; this.bitmap = bitmap; } public ImagePieces() { // TODO Auto-generated constructor stub } @Override public String toString() { return "ImagePieces [index=" + index + ", bitmap=" + bitmap + "]"; } }
4.圖片分割類
public class ImageSplitterUtil { //pieces 為切成的塊數,用list儲存 public static List<ImagePieces> splitImage(Bitmap bitmap,int pieces){ List<ImagePieces> imagePieces = new ArrayList<ImagePieces>(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); //去小的一方每一個快的寬度 int pieceWidth = Math.min(width,height)/pieces; for(int i=0;i<pieces;i++){ for(int j=0;j<pieces;j++){ ImagePieces imagepieces = new ImagePieces(); /* * 1+0 2+0 3+0 * 1+1 2+1 3+1 */ imagepieces.setIndex(j+i*pieces); int x = j*pieceWidth; int y = i*pieceWidth; //x,y是每一塊的起點位置,piecewidth是每一塊的長度和高度,這裡弄成正方形 imagepieces.setBitmap(Bitmap.createBitmap(bitmap, x, y, pieceWidth, pieceWidth)); //所有操作setIndex setBitmap 然後把所有碎片按順序加入list集合中 imagePieces.add(imagepieces); } } return imagePieces; } }
5.佈局類
首先
private void init() { // 將margin的值的單位轉為dp mMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics()); mPadding = min(getPaddingLeft(), getPaddingRight(), getPaddingTop(), getPaddingBottom()); }
其次onmeasure
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 獲得寬度和高度的最小值 mWidth = min(getMeasuredHeight(), getMeasuredWidth()); if (!once) { // 進行切圖及排序 initBitmap(); // 設定imageview[item]的寬高等屬性 initItem(); // 標識已畫,防止再畫 once = true; countTimeBaseLevel(); } // 寬度和高度的最小值設為寬高 setMeasuredDimension(mWidth, mWidth); }
其次
// 進行切圖及排序 private void initBitmap() { if (mBitmap == null) { mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.psb); } mItemBitmaps = ImageSplitterUtil.splitImage(mBitmap, mColumn); // 是用sort把小塊的圖片亂序 Collections.sort(mItemBitmaps, new Comparator<ImagePieces>() { @Override public int compare(ImagePieces a, ImagePieces b) { return Math.random() > 0.5 ? 1 : -1; } }); }
private int mTime; // 相對關卡的時間數 private void countTimeBaseLevel() { mTime = (int) Math.pow(2, mLevel) * 30; mHandler.sendEmptyMessage(TIME_CHANGED); }
// 動畫層的佈局 private RelativeLayout mAnimaLayout; private boolean isAniming;
回撥介面
public interface GamePintuListener { void nextLevel(int nextLevel); void timechanged(int currentTime); void gameover(); } public GamePintuListener mListener; // 設定介面回撥 public void setOnGamePintuListener(GamePintuListener mListener) { this.mListener = mListener; }
遊戲的程序控制,這裡利用的是hangler
private static final int TIME_CHANGED = 0x110; public static final int NEXT_LEVEL = 0x111; private Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case TIME_CHANGED: if (isGameSuccess || isGameOver || isPausing) return; if (mListener != null) { mListener.timechanged(mTime); if (mTime == 0) { isGameOver = true; mListener.gameover(); return; } } mTime--; mHandler.sendEmptyMessageDelayed(TIME_CHANGED, 1000); break; case NEXT_LEVEL: mLevel = mLevel + 1; if (mListener != null) { mListener.nextLevel(mLevel); } else { nextLevel(); } break; } } };
重新開始遊戲方法
public void restart() { isGameOver = false; mColumn--; nextLevel(); }
暫停和繼續的方法
private boolean isPausing; public void pause() { isPausing = true; mHandler.removeMessages(TIME_CHANGED); } public void resume() { if (isPausing) { isPausing = false; mHandler.sendEmptyMessage(TIME_CHANGED); } } public void nextLevel() { this.removeAllViews(); mAnimaLayout = null; mColumn++; isGameSuccess = false; countTimeBaseLevel(); initBitmap(); initItem(); };
// 設定imageview[item]的寬高等屬性 private void initItem() { // 把內邊距和外面距剪掉除以列數就是每一塊的寬度 mItemWidth = (mWidth - mPadding * 2 - mMargin * (mColumn - 1)) / mColumn; // item圖片位置的初始化 mPintuItems = new ImageView[mColumn * mColumn]; // item圖片的初始化 for (int i = 0; i < mPintuItems.length; i++) { ImageView item = new ImageView(getContext()); item.setOnClickListener(this); item.setImageBitmap(mItemBitmaps.get(i).getBitmap()); mPintuItems[i] = item; item.setId(i + 1); // 在item裡面存放index,當拼圖成功時候做為判斷一句 item.setTag(i + "_" + mItemBitmaps.get(i).getIndex()); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( mItemWidth, mItemWidth); // 設定間隙 // 如果不是最後一列 if ((i + 1) % mColumn != 0) { lp.rightMargin = mMargin; } // 不是第一列 if (i % mColumn != 0) { lp.addRule(RelativeLayout.RIGHT_OF, mPintuItems[i - 1].getId()); } // 縱向間隙 // 如果不是第一行,設定topMargin和rule if ((i + 1) > mColumn) { lp.topMargin = mMargin; lp.addRule(RelativeLayout.BELOW, mPintuItems[i - mColumn].getId()); } addView(item, lp); } }
交換圖片
private ImageView mFirst; private ImageView mSecond; @Override public void onClick(View v) { if (isAniming) return; if (mFirst == v) { mFirst.setColorFilter(null); mFirst = null; return; } if (mFirst == null) { mFirst = (ImageView) v; mFirst.setColorFilter(Color.parseColor("#45f0f0f0")); } else { mSecond = (ImageView) v; exchangView(); } }
交換圖片的方法exchangeView方法的具體實現
// 交換image private void exchangView() { mFirst.setColorFilter(null); String firstTag = (String) mFirst.getTag(); String secondTag = (String) mSecond.getTag(); String[] firstParams = firstTag.split("_"); String[] secondParams = secondTag.split("_"); final Bitmap firstBitmap = mItemBitmaps.get( Integer.parseInt(firstParams[0])).getBitmap(); final Bitmap secondBitmap = mItemBitmaps.get( Integer.parseInt(secondParams[0])).getBitmap(); //設定動畫層,下面有具體實現 setUpAnimLayout(); ImageView first = new ImageView(getContext()); first.setImageBitmap(firstBitmap); LayoutParams lp = new LayoutParams(mItemWidth, mItemWidth); lp.leftMargin = mFirst.getLeft() - mPadding; lp.topMargin = mFirst.getTop() - mPadding; first.setLayoutParams(lp); mAnimaLayout.addView(first); ImageView second = new ImageView(getContext()); second.setImageBitmap(secondBitmap); LayoutParams lp2 = new LayoutParams(mItemWidth, mItemWidth); lp2.leftMargin = mSecond.getLeft() - mPadding; lp2.topMargin = mSecond.getTop() - mPadding; second.setLayoutParams(lp2); mAnimaLayout.addView(second); // 設定動畫 TranslateAnimation anim = new TranslateAnimation(0, mSecond.getLeft() - mFirst.getLeft(), 0, mSecond.getTop() - mFirst.getTop()); anim.setDuration(300); anim.setFillAfter(true); first.setAnimation(anim); TranslateAnimation anim2 = new TranslateAnimation(0, mFirst.getLeft() - mSecond.getLeft(), 0, mFirst.getTop() - mSecond.getTop()); anim2.setDuration(300); anim2.setFillAfter(true); second.setAnimation(anim2); anim.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation arg0) { mFirst.setVisibility(View.INVISIBLE); mSecond.setVisibility(View.INVISIBLE); isAniming = true; } @Override public void onAnimationRepeat(Animation arg0) { } @Override public void onAnimationEnd(Animation arg0) { mSecond.setImageBitmap(firstBitmap); mFirst.setImageBitmap(secondBitmap); String firstTag = (String) mFirst.getTag(); String secondTag = (String) mSecond.getTag(); mFirst.setTag(secondTag); mSecond.setTag(firstTag); mFirst.setVisibility(View.VISIBLE); mSecond.setVisibility(View.VISIBLE); mFirst = mSecond = null; mAnimaLayout.removeAllViews(); // 每次移動完成判斷是否成功 checkSuccess(); isAniming = false; } private void checkSuccess() { boolean isSuccess = true; for (int i = 0; i < mPintuItems.length; i++) { ImageView imageview = mPintuItems[i]; String[] tag = imageview.getTag().toString().split("_"); if (Integer.parseInt(tag[1]) != i) { isSuccess = false; } } if (isSuccess) { isSuccess = true; mHandler.removeMessages(TIME_CHANGED); Toast.makeText(getContext(), "level up!", Toast.LENGTH_SHORT).show(); mHandler.sendEmptyMessage(NEXT_LEVEL); } } }); }
動畫層的具體實現
private void setUpAnimLayout() { if (mAnimaLayout == null) { mAnimaLayout = new RelativeLayout(getContext()); addView(mAnimaLayout); } }
//獲取多個引數的最少值作為內邊距 private int min(int... params) { int min = params[0]; for (int param : params) { if (param < min) { min = param; } } return min; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45