├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── cn
│ │ └── kevin
│ │ └── puzzle
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── cn
│ │ │ └── kevin
│ │ │ └── puzzle
│ │ │ ├── BitmapUtil.java
│ │ │ ├── DataHelper.java
│ │ │ ├── PuzzleActivity.java
│ │ │ ├── PuzzleLayout.java
│ │ │ └── model
│ │ │ └── Block.java
│ └── res
│ │ ├── layout
│ │ └── activity_puzzle.xml
│ │ ├── mipmap-hdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ ├── pic_02.jpg
│ │ ├── pic_03.jpg
│ │ ├── pic_04.jpg
│ │ ├── pic_05.jpg
│ │ ├── pic_06.jpg
│ │ ├── pic_07.jpg
│ │ ├── pic_08.jpg
│ │ ├── pic_09.jpg
│ │ └── pic_10.jpg
│ │ ├── mipmap-xxxhdpi
│ │ └── ic_launcher.png
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── cn
│ └── kevin
│ └── puzzle
│ └── ExampleUnitTest.java
├── build.gradle
├── demo.gif
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/README.md:
--------------------------------------------------------------------------------
1 | # Puzzle game
2 | Android puzzle game.
3 |
4 | [](https://www.apache.org/licenses/LICENSE-2.0)
5 |
6 | 简单的安卓拼图游戏,内部滑动通过ViewDragHelper实现。
7 |
8 | Simple Android puzzle game, internal sliding is ViewDragHelper implementation.
9 |
10 | ## 预览 Preview
11 | 
12 |
13 |
14 | 附上实现步骤 [http://www.jianshu.com/p/8613c748aaaf](http://www.jianshu.com/p/8613c748aaaf)
15 |
16 | License
17 | --------
18 |
19 | Copyright 2017 kevin.
20 |
21 | Licensed under the Apache License, Version 2.0 (the "License");
22 | you may not use this file except in compliance with the License.
23 | You may obtain a copy of the License at
24 |
25 | http://www.apache.org/licenses/LICENSE-2.0
26 |
27 | Unless required by applicable law or agreed to in writing, software
28 | distributed under the License is distributed on an "AS IS" BASIS,
29 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 | See the License for the specific language governing permissions and
31 | limitations under the License.
32 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "26.0.1"
6 | defaultConfig {
7 | applicationId "cn.kevin.puzzle"
8 | minSdkVersion 14
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | })
27 | compile 'com.android.support:appcompat-v7:25.3.1'
28 | testCompile 'junit:junit:4.12'
29 | }
30 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\Users\Administrator\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/cn/kevin/puzzle/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package cn.kevin.puzzle;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("cn.kevin.puzzle", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/kevin/puzzle/BitmapUtil.java:
--------------------------------------------------------------------------------
1 | package cn.kevin.puzzle;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.Matrix;
5 |
6 | /**
7 | * 创建日期:2017/10/23.
8 | *
9 | * @author kevin
10 | */
11 |
12 | public class BitmapUtil {
13 | public static Bitmap zoomImg(Bitmap bm, int newWidth ,int newHeight){
14 | int width = bm.getWidth();
15 | int height = bm.getHeight();
16 | float scaleWidth = ((float) newWidth) / width;
17 | float scaleHeight = ((float) newHeight) / height;
18 | Matrix matrix = new Matrix();
19 | matrix.postScale(scaleWidth, scaleHeight);
20 | return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/kevin/puzzle/DataHelper.java:
--------------------------------------------------------------------------------
1 | package cn.kevin.puzzle;
2 |
3 | import android.util.Log;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 | import java.util.Random;
8 |
9 | import cn.kevin.puzzle.model.Block;
10 |
11 | /**
12 | * 创建日期:2017/10/23.
13 | *
14 | * @author kevin
15 | */
16 | class DataHelper {
17 | static final int N = -1;
18 | static final int L = 0;
19 | static final int T = 1;
20 | static final int R = 2;
21 | static final int B = 3;
22 | private static final String TAG = DataHelper.class.getSimpleName();
23 |
24 | private int squareRootNum;
25 | private List models;
26 |
27 | DataHelper(){
28 | models = new ArrayList<>();
29 | }
30 |
31 | private void reset() {
32 | models.clear();
33 | int position = 0;
34 | for (int i = 0; i< squareRootNum; i++){
35 | for (int j = 0; j < squareRootNum; j++){
36 | models.add(new Block(position, i, j));
37 | position ++;
38 | }
39 | }
40 | }
41 |
42 | void setSquareRootNum(int squareRootNum){
43 | this.squareRootNum = squareRootNum;
44 | reset();
45 | }
46 |
47 | /**
48 | * 将索引出的model的值与空白model的值互换。
49 | */
50 | boolean swapValueWithInvisibleModel(int index){
51 | Block formModel = models.get(index);
52 | Block invisibleModel = models.get(0);
53 | swapValue(formModel, invisibleModel);
54 | return isCompleted();
55 | }
56 |
57 | /**
58 | * 交换两个model的值
59 | */
60 | private void swapValue(Block model, Block invisibleModel) {
61 |
62 | int position = model.position;
63 | int hPosition = model.hPosition;
64 | int vPosition = model.vPosition;
65 |
66 | model.position = invisibleModel.position;
67 | model.hPosition = invisibleModel.hPosition;
68 | model.vPosition = invisibleModel.vPosition;
69 |
70 | invisibleModel.position = position;
71 | invisibleModel.hPosition = hPosition;
72 | invisibleModel.vPosition = vPosition;
73 | }
74 |
75 | /**
76 | * 判断是否拼图完成。
77 | */
78 | private boolean isCompleted(){
79 | int num = squareRootNum * squareRootNum;
80 | for (int i = 0; i < num; i++){
81 | Block model = models.get(i);
82 | if(model.position != i){
83 | return false;
84 | }
85 | }
86 | return true;
87 | }
88 |
89 | public Block getModel(int index){
90 | return models.get(index);
91 | }
92 |
93 | /**
94 | * 通过给定的位置获取model的索引
95 | */
96 | private int getIndexByCurrentPosition(int currentPosition){
97 | int num = squareRootNum * squareRootNum;
98 | for (int i = 0; i < num; i++) {
99 | if(models.get(i).position == currentPosition)
100 | return i;
101 | }
102 | return -1;
103 | }
104 |
105 | /**
106 | * 随机查询出空白位置周围的一个model的索引。
107 | */
108 | public int findNeighborIndexOfInvisibleModel() {
109 | Block invisibleModel = models.get(0);
110 | int position = invisibleModel.position;
111 | int x = position % squareRootNum;
112 | int y = position / squareRootNum;
113 | int direction = new Random(System.nanoTime()).nextInt(4);
114 | Log.d(TAG, "direction " + direction);
115 | switch (direction){
116 | case L:
117 | if(x != 0)
118 | return getIndexByCurrentPosition(position - 1);
119 | case T:
120 | if(y != 0)
121 | return getIndexByCurrentPosition(position - squareRootNum);
122 | case R:
123 | if(x != squareRootNum - 1)
124 | return getIndexByCurrentPosition(position + 1);
125 | case B:
126 | if(y != squareRootNum - 1)
127 | return getIndexByCurrentPosition(position + squareRootNum);
128 | }
129 | return findNeighborIndexOfInvisibleModel();
130 | }
131 |
132 | /**
133 | * 获取索引处model的可移动方向,不能移动返回 -1。
134 | */
135 | int getScrollDirection(int index){
136 |
137 | Block model = models.get(index);
138 | int position = model.position;
139 |
140 | //获取当前view所在位置的坐标 x y
141 | /*
142 | * * * * *
143 | * * o * *
144 | * * * * *
145 | * * * * *
146 | */
147 | int x = position % squareRootNum;
148 | int y = position / squareRootNum;
149 | int invisibleModelPosition = models.get(0).position;
150 |
151 | /*
152 | * 判断当前位置是否可以移动,如果可以移动就return可移动的方向。
153 | */
154 |
155 | if(x != 0 && invisibleModelPosition == position - 1)
156 | return L;
157 |
158 | if(x != squareRootNum - 1 && invisibleModelPosition == position + 1)
159 | return R;
160 |
161 | if(y != 0 && invisibleModelPosition == position - squareRootNum)
162 | return T;
163 |
164 | if(y != squareRootNum - 1 && invisibleModelPosition == position + squareRootNum)
165 | return B;
166 |
167 | return N;
168 | }
169 | }
170 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/kevin/puzzle/PuzzleActivity.java:
--------------------------------------------------------------------------------
1 | package cn.kevin.puzzle;
2 |
3 | import android.content.DialogInterface;
4 | import android.support.v7.app.AlertDialog;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.os.Bundle;
7 | import android.view.MotionEvent;
8 | import android.view.View;
9 | import android.widget.ImageView;
10 | import android.widget.TextView;
11 | import android.widget.Toast;
12 |
13 | public class PuzzleActivity extends AppCompatActivity implements Runnable, View.OnTouchListener {
14 | PuzzleLayout puzzleLayout;
15 | TextView tvTips;
16 | ImageView ivTips;
17 | int squareRootNum = 2;
18 | int drawableId = R.mipmap.pic_02;
19 | @Override
20 | protected void onCreate(Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 | setContentView(R.layout.activity_puzzle);
23 | ivTips = (ImageView) findViewById(R.id.iv_tips);
24 | ivTips.setImageResource(drawableId);
25 | tvTips = (TextView) findViewById(R.id.tv_tips);
26 | tvTips.setOnTouchListener(this);
27 | puzzleLayout = (PuzzleLayout) findViewById(R.id.activity_swipe_card);
28 | puzzleLayout.setImage(drawableId, squareRootNum);
29 | puzzleLayout.setOnCompleteCallback(new PuzzleLayout.OnCompleteCallback() {
30 | @Override
31 | public void onComplete() {
32 | Toast.makeText(PuzzleActivity.this, R.string.next, Toast.LENGTH_LONG).show();
33 | puzzleLayout.postDelayed(PuzzleActivity.this, 800);
34 | }
35 | });
36 | }
37 |
38 | @Override
39 | public void run() {
40 | squareRootNum++;
41 | drawableId++;
42 | if(squareRootNum > 10){
43 | Toast.makeText(PuzzleActivity.this, R.string.complete, Toast.LENGTH_SHORT).show();
44 | showDialog();
45 | }else {
46 | ivTips.setImageResource(drawableId);
47 | puzzleLayout.setImage(drawableId, squareRootNum);
48 | }
49 | }
50 |
51 | private void showDialog() {
52 | new AlertDialog.Builder(PuzzleActivity.this)
53 | .setTitle(R.string.success)
54 | .setMessage(R.string.restart)
55 | .setPositiveButton(R.string.ok,
56 | new DialogInterface.OnClickListener() {
57 | @Override
58 | public void onClick(DialogInterface dialog, int which) {
59 | squareRootNum = 2;
60 | drawableId = R.mipmap.pic_02;
61 | ivTips.setImageResource(drawableId);
62 | puzzleLayout.setImage(drawableId, squareRootNum);
63 | }
64 | }).setNegativeButton(R.string.exit,
65 | new DialogInterface.OnClickListener() {
66 | @Override
67 | public void onClick(DialogInterface dialog, int which) {
68 | finish();
69 | }
70 | }).show();
71 | }
72 |
73 | @Override
74 | public boolean onTouch(View v, MotionEvent event) {
75 | switch (event.getAction()){
76 | case MotionEvent.ACTION_DOWN:
77 | ivTips.setVisibility(View.VISIBLE);
78 | break;
79 | default:
80 | ivTips.setVisibility(View.GONE);
81 | }
82 | return true;
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/kevin/puzzle/PuzzleLayout.java:
--------------------------------------------------------------------------------
1 | package cn.kevin.puzzle;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.graphics.BitmapFactory;
6 | import android.support.v4.widget.ViewDragHelper;
7 | import android.util.AttributeSet;
8 | import android.util.DisplayMetrics;
9 | import android.util.Log;
10 | import android.view.MotionEvent;
11 | import android.view.View;
12 | import android.view.ViewGroup;
13 | import android.view.ViewTreeObserver;
14 | import android.widget.ImageView;
15 | import android.widget.RelativeLayout;
16 |
17 | import cn.kevin.puzzle.model.Block;
18 |
19 | /**
20 | * 创建日期:2017/10/23.
21 | *
22 | * @author kevin
23 | */
24 |
25 | public class PuzzleLayout extends RelativeLayout {
26 | private ViewDragHelper viewDragHelper;
27 | private static final String TAG = PuzzleLayout.class.getSimpleName();
28 | private DataHelper mHelper;
29 | private int mDrawableId;
30 | private int mSquareRootNum;
31 | private int mHeight;
32 | private int mWidth;
33 | private int mItemWidth;
34 | private int mItemHeight;
35 | private OnCompleteCallback mOnCompleteCallback;
36 |
37 | public PuzzleLayout(Context context) {
38 | super(context);
39 | init();
40 | }
41 |
42 |
43 | public PuzzleLayout(Context context, AttributeSet attrs) {
44 | super(context, attrs);
45 | init();
46 | }
47 |
48 | public PuzzleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
49 | super(context, attrs, defStyleAttr);
50 | init();
51 | }
52 |
53 | private void init() {
54 |
55 | getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
56 | @Override
57 | public boolean onPreDraw() {
58 | mHeight = getHeight();
59 | mWidth = getWidth();
60 | getViewTreeObserver().removeOnPreDrawListener(this);
61 | if(mDrawableId != 0 && mSquareRootNum != 0){
62 | createChildren();
63 | }
64 | return false;
65 | }
66 | });
67 | mHelper = new DataHelper();
68 |
69 | viewDragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {
70 | @Override
71 | public boolean tryCaptureView(View child, int pointerId) {
72 | int index = indexOfChild(child);
73 | return mHelper.getScrollDirection(index) != DataHelper.N;
74 | }
75 |
76 | @Override
77 | public int clampViewPositionHorizontal(View child, int left, int dx) {
78 |
79 | int index = indexOfChild(child);
80 | int position = mHelper.getModel(index).position;
81 | int selfLeft = (position % mSquareRootNum) * mItemWidth;
82 | int leftEdge = selfLeft - mItemWidth;
83 | int rightEdge = selfLeft + mItemWidth;
84 | int direction = mHelper.getScrollDirection(index);
85 | //Log.d(TAG, "left " + left + " index" + index + " dx " + dx + " direction " + direction);
86 | switch (direction){
87 | case DataHelper.L:
88 | if(left <= leftEdge)
89 | return leftEdge;
90 | else if(left >= selfLeft)
91 | return selfLeft;
92 | else
93 | return left;
94 |
95 | case DataHelper.R:
96 | if(left >= rightEdge)
97 | return rightEdge;
98 | else if (left <= selfLeft)
99 | return selfLeft;
100 | else
101 | return left;
102 | default:
103 | return selfLeft;
104 | }
105 | }
106 |
107 | @Override
108 | public int clampViewPositionVertical(View child, int top, int dy) {
109 | int index = indexOfChild(child);
110 | Block model = mHelper.getModel(index);
111 | int position = model.position;
112 |
113 | int selfTop = (position / mSquareRootNum) * mItemHeight;
114 | int topEdge = selfTop - mItemHeight;
115 | int bottomEdge = selfTop + mItemHeight;
116 | int direction = mHelper.getScrollDirection(index);
117 | //Log.d(TAG, "top " + top + " index " + index + " direction " + direction);
118 | switch (direction){
119 | case DataHelper.T:
120 | if(top <= topEdge)
121 | return topEdge;
122 | else if (top >= selfTop)
123 | return selfTop;
124 | else
125 | return top;
126 | case DataHelper.B:
127 | if(top >= bottomEdge)
128 | return bottomEdge;
129 | else if (top <= selfTop)
130 | return selfTop;
131 | else
132 | return top;
133 | default:
134 | return selfTop;
135 | }
136 | }
137 |
138 | @Override
139 | public void onViewReleased(View releasedChild, float xvel, float yvel) {
140 | Log.d(TAG, "xvel " + xvel + " yvel " + yvel);
141 | int index = indexOfChild(releasedChild);
142 | boolean isCompleted = mHelper.swapValueWithInvisibleModel(index);
143 | Block item = mHelper.getModel(index);
144 | viewDragHelper.settleCapturedViewAt(item.hPosition * mItemWidth, item.vPosition * mItemHeight);
145 | View invisibleView = getChildAt(0);
146 | ViewGroup.LayoutParams layoutParams = invisibleView.getLayoutParams();
147 | invisibleView.setLayoutParams(releasedChild.getLayoutParams());
148 | releasedChild.setLayoutParams(layoutParams);
149 | invalidate();
150 | if(isCompleted){
151 | invisibleView.setVisibility(VISIBLE);
152 | mOnCompleteCallback.onComplete();
153 | }
154 | }
155 | });
156 | }
157 |
158 | @Override
159 | public boolean onInterceptTouchEvent(MotionEvent event){
160 | return viewDragHelper.shouldInterceptTouchEvent(event);
161 | }
162 |
163 | @Override
164 | public boolean onTouchEvent(MotionEvent event) {
165 | viewDragHelper.processTouchEvent(event);
166 | return true;
167 | }
168 |
169 | @Override
170 | public void computeScroll() {
171 | if(viewDragHelper.continueSettling(true)) {
172 | invalidate();
173 | }
174 | }
175 |
176 | public void setImage(int drawableId, int squareRootNum){
177 | this.mSquareRootNum = squareRootNum;
178 | this.mDrawableId = drawableId;
179 | if(mWidth != 0 && mHeight != 0){
180 | createChildren();
181 | }
182 | }
183 |
184 | /**
185 | * 将子View index与mHelper中models的index一一对应,
186 | * 每次在交换子View位置的时候model同步更新currentPosition。
187 | */
188 | private void createChildren(){
189 | removeAllViews();
190 | mHelper.setSquareRootNum(mSquareRootNum);
191 |
192 | DisplayMetrics dm = getResources().getDisplayMetrics();
193 | BitmapFactory.Options options = new BitmapFactory.Options();
194 | options.inDensity = dm.densityDpi;
195 |
196 | Bitmap resource = BitmapFactory.decodeResource(getResources(), mDrawableId, options);
197 | Bitmap bitmap = BitmapUtil.zoomImg(resource, mWidth, mHeight);
198 | resource.recycle();
199 |
200 | mItemWidth = mWidth / mSquareRootNum;
201 |
202 | mItemHeight = mHeight / mSquareRootNum;
203 |
204 |
205 | for (int i = 0; i < mSquareRootNum; i++){
206 | for (int j = 0; j < mSquareRootNum; j++){
207 | Log.d(TAG, "mItemWidth * x " + (mItemWidth * i));
208 | Log.d(TAG, "mItemWidth * y " + (mItemWidth * j));
209 | ImageView iv = new ImageView(getContext());
210 | iv.setScaleType(ImageView.ScaleType.FIT_XY);
211 | LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
212 | lp.leftMargin = j * mItemWidth;
213 | lp.topMargin = i * mItemHeight;
214 | iv.setLayoutParams(lp);
215 | Bitmap b = Bitmap.createBitmap(bitmap, lp.leftMargin, lp.topMargin, mItemWidth, mItemHeight);
216 | iv.setImageBitmap(b);
217 | addView(iv);
218 | }
219 | }
220 | randomOrder();
221 | }
222 |
223 | public void randomOrder(){
224 | int num = mSquareRootNum * mSquareRootNum * 8;
225 | View invisibleView = getChildAt(0);
226 | View neighbor;
227 | for (int i = 0; i < num; i ++){
228 | int neighborPosition = mHelper.findNeighborIndexOfInvisibleModel();
229 | ViewGroup.LayoutParams invisibleLp = invisibleView.getLayoutParams();
230 | neighbor = getChildAt(neighborPosition);
231 | invisibleView.setLayoutParams(neighbor.getLayoutParams());
232 | neighbor.setLayoutParams(invisibleLp);
233 | mHelper.swapValueWithInvisibleModel(neighborPosition);
234 | }
235 | invisibleView.setVisibility(INVISIBLE);
236 | }
237 |
238 | public void setOnCompleteCallback(OnCompleteCallback onCompleteCallback){
239 | mOnCompleteCallback = onCompleteCallback;
240 | }
241 |
242 | public interface OnCompleteCallback{
243 | void onComplete();
244 | }
245 | }
246 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/kevin/puzzle/model/Block.java:
--------------------------------------------------------------------------------
1 | package cn.kevin.puzzle.model;
2 |
3 | /**
4 | * 创建日期:2017/10/23.
5 | *
6 | * @author kevin
7 | */
8 |
9 | public class Block {
10 | public Block(int position, int vPosition, int hPosition){
11 | this.position = position;
12 | this.vPosition = vPosition;
13 | this.hPosition = hPosition;
14 | }
15 | public int position;
16 | public int vPosition;
17 | public int hPosition;
18 | }
19 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_puzzle.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
13 |
14 |
15 |
21 |
22 |
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kevin-mob/Puzzle/7f688a21af7c9681d96b47efc2a84aa0c4a961f9/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kevin-mob/Puzzle/7f688a21af7c9681d96b47efc2a84aa0c4a961f9/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kevin-mob/Puzzle/7f688a21af7c9681d96b47efc2a84aa0c4a961f9/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kevin-mob/Puzzle/7f688a21af7c9681d96b47efc2a84aa0c4a961f9/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/pic_02.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kevin-mob/Puzzle/7f688a21af7c9681d96b47efc2a84aa0c4a961f9/app/src/main/res/mipmap-xxhdpi/pic_02.jpg
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/pic_03.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kevin-mob/Puzzle/7f688a21af7c9681d96b47efc2a84aa0c4a961f9/app/src/main/res/mipmap-xxhdpi/pic_03.jpg
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/pic_04.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kevin-mob/Puzzle/7f688a21af7c9681d96b47efc2a84aa0c4a961f9/app/src/main/res/mipmap-xxhdpi/pic_04.jpg
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/pic_05.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kevin-mob/Puzzle/7f688a21af7c9681d96b47efc2a84aa0c4a961f9/app/src/main/res/mipmap-xxhdpi/pic_05.jpg
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/pic_06.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kevin-mob/Puzzle/7f688a21af7c9681d96b47efc2a84aa0c4a961f9/app/src/main/res/mipmap-xxhdpi/pic_06.jpg
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/pic_07.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kevin-mob/Puzzle/7f688a21af7c9681d96b47efc2a84aa0c4a961f9/app/src/main/res/mipmap-xxhdpi/pic_07.jpg
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/pic_08.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kevin-mob/Puzzle/7f688a21af7c9681d96b47efc2a84aa0c4a961f9/app/src/main/res/mipmap-xxhdpi/pic_08.jpg
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/pic_09.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kevin-mob/Puzzle/7f688a21af7c9681d96b47efc2a84aa0c4a961f9/app/src/main/res/mipmap-xxhdpi/pic_09.jpg
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/pic_10.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kevin-mob/Puzzle/7f688a21af7c9681d96b47efc2a84aa0c4a961f9/app/src/main/res/mipmap-xxhdpi/pic_10.jpg
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kevin-mob/Puzzle/7f688a21af7c9681d96b47efc2a84aa0c4a961f9/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Puzzle
3 | 恭喜通关!!
4 | 解锁新关卡成功!!
5 | 重新开始?
6 | 确定
7 | 退出
8 | 你是天才,恭喜通关!!
9 | Tips
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/cn/kevin/puzzle/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package cn.kevin.puzzle;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.2.1'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kevin-mob/Puzzle/7f688a21af7c9681d96b47efc2a84aa0c4a961f9/demo.gif
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kevin-mob/Puzzle/7f688a21af7c9681d96b47efc2a84aa0c4a961f9/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------