Android tile game wireless Split Version
1.
2. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;"> // Use the callback interface. initialize and bind the pintuview to implement the callback interface method 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 () ;}// set to roll back and exit the program twice @ Override public boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK) {exit (); Toast. makeText (getApplicationContext (), "Click again to exit", 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 ;}}; // Click Event @ 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. Each small piece of backslice bean
Public class ImagePieces {// the index value of a small image block is private int index; // The entire large image block is 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;} // constructor 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. Image Segmentation
Public class ImageSplitterUtil {// pieces indicates the number of cut parts. Use list to save the public static List.
SplitImage (Bitmap bitmap, int pieces) {List
ImagePieces = new ArrayList
(); Int width = bitmap. getWidth (); int height = bitmap. getHeight (); // remove the smallest side of each fast width int pieceWidth = Math. min (width, height)/pieces; for (int I = 0; I
5. Layout
First
Private void init () {// convert the unit of the margin value to dp mMargin = (int) TypedValue. applyDimension (TypedValue. COMPLEX_UNIT_DIP, 3, getResources (). getDisplayMetrics (); mPadding = min (getPaddingLeft (), getPaddingRight (), getPaddingTop (), getPaddingBottom ());}
Second, onmeasure
@ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec); // obtain the minimum mWidth = min (getMeasuredHeight (), getMeasuredWidth () for width and height; if (! Once) {// cut the image and sort the initBitmap (); // set the width and height attribute initItem () of imageview [item); // identify the image that has been painted to prevent the image from being painted once = true; countTimeBaseLevel ();} // the minimum value of the width and height is set to the width and height setMeasuredDimension (mWidth, mWidth );}
Second
// Cut and sort private void initBitmap () {if (mBitmap = null) {mBitmap = BitmapFactory. decodeResource (getResources (), R. drawable. psb);} mItemBitmaps = ImageSplitterUtil. splitImage (mBitmap, mColumn); // use sort to sort small pieces of images in disorder. sort (mItemBitmaps, new Comparator () {@ Override public int compare (ImagePieces a, ImagePieces B) {return Math. random ()> 0.5? 1:-1 ;}});}
Private int mTime; // The number of times relative to the level. private void countTimeBaseLevel () {mTime = (int) Math. pow (2, mLevel) * 30; mHandler. sendEmptyMessage (TIME_CHANGED );}
// Animation layer layout private RelativeLayout mAnimaLayout; private boolean isAniming;
Callback Interface
Public interface listener {void nextLevel (int nextLevel); void timechanged (int currentTime); void gameover ();} public GamePintuListener mListener; // set the interface callback public void listener (GamePintuListener mListener) {this. mListener = mListener ;}
The process control of the game. Here we use 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; } } };
How to restart the game
public void restart() { isGameOver = false; mColumn--; nextLevel(); }
Pause and continue Methods
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(); };
// Set the private void initItem () attribute of imageview [item () {// cut the padding and outer margin by the number of columns, that is, the width of each piece, mItemWidth = (mWidth-mPadding * 2-mMargin * (mColumn-1)/mColumn; // initialize the position of the item image mPintuItems = new ImageView [mColumn * mColumn]; // initialize the item image for (int I = 0; I <mPintuItems. length; I ++) {ImageView item = new ImageView (getContext (); item. setOnClickListener (this); item. setImageBitmap (mItemBitmaps. get (I ). getBit Map (); mPintuItems [I] = item; item. setId (I + 1); // store the index in the item. When the puzzle is successful, it is used as an item. setTag (I + "_" + mItemBitmaps. get (I ). getIndex (); RelativeLayout. layoutParams lp = new RelativeLayout. layoutParams (mItemWidth, mItemWidth); // set the gap // if it is not the last column if (I + 1) % mColumn! = 0) {lp. rightMargin = mMargin;} // if (I % mColumn! = 0) {lp. addRule (RelativeLayout. RIGHT_OF, mPintuItems [I-1]. getId ();} // longitudinal gap // if it is not the first line, set topMargin and rule if (I + 1)> mColumn) {lp. topMargin = mMargin; lp. addRule (RelativeLayout. BELOW, mPintuItems [I-mColumn]. getId ();} addView (item, lp );}}
Exchange Images
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
// Exchange 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. parseI Nt (secondParams [0]). getBitmap (); // set the animation layer. The following describes the specific implementation of 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); // set the animated TranslateAnimation anim = new TranslateAnimation (0, mSecond. getLeft ()-mFirst. getLeft (), 0, mSecond. getTop ()-mFirst. getTop (); anim. setDu Ration (1, 300); anim. setFillAfter (true); first. setAnimation (anim); TranslateAnimation anim2 = new TranslateAnimation (0, mFirst. getLeft ()-mSecond. getLeft (), 0, mFirst. getTop ()-mSecond. get top (); anim2.setDuration (300); anim2.setFillAfter (true); second. setAnimation (anim2); anim. setAnimationListener (new AnimationListener () {@ Override public void onAnimationStart (Animation arg0) {mFirst. setVisibili Ty (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 (f IrstTag); mFirst. setVisibility (View. VISIBLE); mSecond. setVisibility (View. VISIBLE); mFirst = mSecond = null; mAnimaLayout. removeAllViews (); // check whether 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 (Intege R. 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 );}}});}
Implementation of the animation Layer
private void setUpAnimLayout() { if (mAnimaLayout == null) { mAnimaLayout = new RelativeLayout(getContext()); addView(mAnimaLayout); } }
// Obtain the minimum value of multiple parameters as the padding private int min (int... params) {int min = params [0]; for (int param: params) {if (param <min) {min = param ;}} return min ;}