Skip to content

Commit cea16f6

Browse files
committed
Drag the Image Pieces Around
1 parent 59e4425 commit cea16f6

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

app/src/main/java/dragosholban/com/androidpuzzlegame/MainActivity.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.support.v7.app.AppCompatActivity;
99
import android.os.Bundle;
1010
import android.widget.ImageView;
11+
import android.widget.RelativeLayout;
1112

1213
import java.util.ArrayList;
1314

@@ -21,7 +22,7 @@ protected void onCreate(Bundle savedInstanceState) {
2122
super.onCreate(savedInstanceState);
2223
setContentView(R.layout.activity_main);
2324

24-
final ConstraintLayout layout = findViewById(R.id.layout);
25+
final RelativeLayout layout = findViewById(R.id.layout);
2526
ImageView imageView = findViewById(R.id.imageView);
2627

2728
// run image related code after the view was laid out
@@ -30,9 +31,11 @@ protected void onCreate(Bundle savedInstanceState) {
3031
@Override
3132
public void run() {
3233
pieces = splitImage();
34+
TouchListener touchListener = new TouchListener();
3335
for(Bitmap piece : pieces) {
3436
ImageView iv = new ImageView(getApplicationContext());
3537
iv.setImageBitmap(piece);
38+
iv.setOnTouchListener(touchListener);
3639
layout.addView(iv);
3740
}
3841
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package dragosholban.com.androidpuzzlegame;
2+
3+
import android.view.MotionEvent;
4+
import android.view.View;
5+
import android.widget.RelativeLayout;
6+
7+
public class TouchListener implements View.OnTouchListener {
8+
private float xDelta;
9+
private float yDelta;
10+
11+
@Override
12+
public boolean onTouch(View view, MotionEvent motionEvent) {
13+
float x = motionEvent.getRawX();
14+
float y = motionEvent.getRawY();
15+
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
16+
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
17+
case MotionEvent.ACTION_DOWN:
18+
xDelta = x - lParams.leftMargin;
19+
yDelta = y - lParams.topMargin;
20+
break;
21+
case MotionEvent.ACTION_MOVE:
22+
lParams.leftMargin = (int) (x - xDelta);
23+
lParams.topMargin = (int) (y - yDelta);
24+
view.setLayoutParams(lParams);
25+
break;
26+
}
27+
28+
return true;
29+
}
30+
}

app/src/main/res/layout/activity_main.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
7-
tools:context="dragosholban.com.androidpuzzlegame.MainActivity"
8-
android:id="@+id/layout">
7+
tools:context="dragosholban.com.androidpuzzlegame.MainActivity">
98

109
<ImageView
1110
android:id="@+id/imageView"
@@ -23,4 +22,14 @@
2322
app:layout_constraintStart_toStartOf="parent"
2423
app:layout_constraintTop_toTopOf="parent"
2524
app:srcCompat="@drawable/photo" />
25+
26+
<RelativeLayout
27+
android:id="@+id/layout"
28+
android:layout_width="match_parent"
29+
android:layout_height="match_parent"
30+
app:layout_constraintBottom_toBottomOf="parent"
31+
app:layout_constraintLeft_toLeftOf="parent"
32+
app:layout_constraintRight_toRightOf="parent"
33+
app:layout_constraintTop_toTopOf="parent">
34+
</RelativeLayout>
2635
</android.support.constraint.ConstraintLayout>

0 commit comments

Comments
 (0)