File tree Expand file tree Collapse file tree 3 files changed +45
-3
lines changed
java/dragosholban/com/androidpuzzlegame Expand file tree Collapse file tree 3 files changed +45
-3
lines changed Original file line number Diff line number Diff line change 8
8
import android .support .v7 .app .AppCompatActivity ;
9
9
import android .os .Bundle ;
10
10
import android .widget .ImageView ;
11
+ import android .widget .RelativeLayout ;
11
12
12
13
import java .util .ArrayList ;
13
14
@@ -21,7 +22,7 @@ protected void onCreate(Bundle savedInstanceState) {
21
22
super .onCreate (savedInstanceState );
22
23
setContentView (R .layout .activity_main );
23
24
24
- final ConstraintLayout layout = findViewById (R .id .layout );
25
+ final RelativeLayout layout = findViewById (R .id .layout );
25
26
ImageView imageView = findViewById (R .id .imageView );
26
27
27
28
// run image related code after the view was laid out
@@ -30,9 +31,11 @@ protected void onCreate(Bundle savedInstanceState) {
30
31
@ Override
31
32
public void run () {
32
33
pieces = splitImage ();
34
+ TouchListener touchListener = new TouchListener ();
33
35
for (Bitmap piece : pieces ) {
34
36
ImageView iv = new ImageView (getApplicationContext ());
35
37
iv .setImageBitmap (piece );
38
+ iv .setOnTouchListener (touchListener );
36
39
layout .addView (iv );
37
40
}
38
41
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 4
4
xmlns : tools =" http://schemas.android.com/tools"
5
5
android : layout_width =" match_parent"
6
6
android : layout_height =" match_parent"
7
- tools : context =" dragosholban.com.androidpuzzlegame.MainActivity"
8
- android : id =" @+id/layout" >
7
+ tools : context =" dragosholban.com.androidpuzzlegame.MainActivity" >
9
8
10
9
<ImageView
11
10
android : id =" @+id/imageView"
23
22
app : layout_constraintStart_toStartOf =" parent"
24
23
app : layout_constraintTop_toTopOf =" parent"
25
24
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 >
26
35
</android .support.constraint.ConstraintLayout>
You can’t perform that action at this time.
0 commit comments