Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Make the Pieces Snap into Place
- Loading branch information
|
@@ -15,7 +15,7 @@ |
|
|
import static java.lang.Math.abs; |
|
|
|
|
|
public class MainActivity extends AppCompatActivity { |
|
|
ArrayList<Bitmap> pieces; |
|
|
ArrayList<PuzzlePiece> pieces; |
|
|
|
|
|
@Override |
|
|
protected void onCreate(Bundle savedInstanceState) { |
|
@@ -32,23 +32,21 @@ protected void onCreate(Bundle savedInstanceState) { |
|
|
public void run() { |
|
|
pieces = splitImage(); |
|
|
TouchListener touchListener = new TouchListener(); |
|
|
for(Bitmap piece : pieces) { |
|
|
ImageView iv = new ImageView(getApplicationContext()); |
|
|
iv.setImageBitmap(piece); |
|
|
iv.setOnTouchListener(touchListener); |
|
|
layout.addView(iv); |
|
|
for(PuzzlePiece piece : pieces) { |
|
|
piece.setOnTouchListener(touchListener); |
|
|
layout.addView(piece); |
|
|
} |
|
|
} |
|
|
}); |
|
|
} |
|
|
|
|
|
private ArrayList<Bitmap> splitImage() { |
|
|
private ArrayList<PuzzlePiece> splitImage() { |
|
|
int piecesNumber = 12; |
|
|
int rows = 4; |
|
|
int cols = 3; |
|
|
|
|
|
ImageView imageView = findViewById(R.id.imageView); |
|
|
ArrayList<Bitmap> pieces = new ArrayList<>(piecesNumber); |
|
|
ArrayList<PuzzlePiece> pieces = new ArrayList<>(piecesNumber); |
|
|
|
|
|
// Get the scaled bitmap of the source image |
|
|
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); |
|
@@ -75,7 +73,14 @@ public void run() { |
|
|
for (int row = 0; row < rows; row++) { |
|
|
int xCoord = 0; |
|
|
for (int col = 0; col < cols; col++) { |
|
|
pieces.add(Bitmap.createBitmap(croppedBitmap, xCoord, yCoord, pieceWidth, pieceHeight)); |
|
|
Bitmap pieceBitmap = Bitmap.createBitmap(croppedBitmap, xCoord, yCoord, pieceWidth, pieceHeight); |
|
|
PuzzlePiece piece = new PuzzlePiece(getApplicationContext()); |
|
|
piece.setImageBitmap(pieceBitmap); |
|
|
piece.xCoord = xCoord + imageView.getLeft(); |
|
|
piece.yCoord = yCoord + imageView.getTop(); |
|
|
piece.pieceWidth = pieceWidth; |
|
|
piece.pieceHeight = pieceHeight; |
|
|
pieces.add(piece); |
|
|
xCoord += pieceWidth; |
|
|
} |
|
|
yCoord += pieceHeight; |
|
|
|
|
@@ -0,0 +1,15 @@ |
|
|
package dragosholban.com.androidpuzzlegame; |
|
|
|
|
|
import android.content.Context; |
|
|
|
|
|
public class PuzzlePiece extends android.support.v7.widget.AppCompatImageView { |
|
|
public int xCoord; |
|
|
public int yCoord; |
|
|
public int pieceWidth; |
|
|
public int pieceHeight; |
|
|
public boolean canMove = true; |
|
|
|
|
|
public PuzzlePiece(Context context) { |
|
|
super(context); |
|
|
} |
|
|
}
|
|
@@ -2,8 +2,13 @@ |
|
|
|
|
|
import android.view.MotionEvent; |
|
|
import android.view.View; |
|
|
import android.view.ViewGroup; |
|
|
import android.widget.RelativeLayout; |
|
|
|
|
|
import static java.lang.Math.pow; |
|
|
import static java.lang.Math.sqrt; |
|
|
import static java.lang.StrictMath.abs; |
|
|
|
|
|
public class TouchListener implements View.OnTouchListener { |
|
|
private float xDelta; |
|
|
private float yDelta; |
|
@@ -12,19 +17,46 @@ |
|
|
public boolean onTouch(View view, MotionEvent motionEvent) { |
|
|
float x = motionEvent.getRawX(); |
|
|
float y = motionEvent.getRawY(); |
|
|
final double tolerance = sqrt(pow(view.getWidth(), 2) + pow(view.getHeight(), 2)) / 10; |
|
|
|
|
|
PuzzlePiece piece = (PuzzlePiece) view; |
|
|
if (!piece.canMove) { |
|
|
return true; |
|
|
} |
|
|
|
|
|
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams(); |
|
|
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { |
|
|
case MotionEvent.ACTION_DOWN: |
|
|
xDelta = x - lParams.leftMargin; |
|
|
yDelta = y - lParams.topMargin; |
|
|
piece.bringToFront(); |
|
|
break; |
|
|
case MotionEvent.ACTION_MOVE: |
|
|
lParams.leftMargin = (int) (x - xDelta); |
|
|
lParams.topMargin = (int) (y - yDelta); |
|
|
view.setLayoutParams(lParams); |
|
|
break; |
|
|
case MotionEvent.ACTION_UP: |
|
|
int xDiff = abs(piece.xCoord - lParams.leftMargin); |
|
|
int yDiff = abs(piece.yCoord - lParams.topMargin); |
|
|
if (xDiff <= tolerance && yDiff <= tolerance) { |
|
|
lParams.leftMargin = piece.xCoord; |
|
|
lParams.topMargin = piece.yCoord; |
|
|
piece.setLayoutParams(lParams); |
|
|
piece.canMove = false; |
|
|
sendViewToBack(piece); |
|
|
} |
|
|
break; |
|
|
} |
|
|
|
|
|
return true; |
|
|
} |
|
|
|
|
|
public void sendViewToBack(final View child) { |
|
|
final ViewGroup parent = (ViewGroup)child.getParent(); |
|
|
if (null != parent) { |
|
|
parent.removeView(child); |
|
|
parent.addView(child, 0); |
|
|
} |
|
|
} |
|
|
}
|
|
@@ -21,7 +21,8 @@ |
|
|
app:layout_constraintRight_toRightOf="parent" |
|
|
app:layout_constraintStart_toStartOf="parent" |
|
|
app:layout_constraintTop_toTopOf="parent" |
|
|
app:srcCompat="@drawable/photo" /> |
|
|
app:srcCompat="@drawable/photo" |
|
|
android:alpha="0.5" /> |
|
|
|
|
|
<RelativeLayout |
|
|
android:id="@+id/layout" |
|
|