How to use Android to realize jigsaw puzzle
Editor to share with you how to use Android to achieve jigsaw puzzles, I hope you will gain something after reading this article, let's discuss it together!
The details are as follows
Source code
Package packageName;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.Toast;import MyImg;public class MainActivity extends AppCompatActivity {/ / display the width of the picture public static final int W = 250; / / the top left margin public static final int MARGIN = 200 / / the index of the empty picture public static final int NULLINDEX = 0; private MyImg [] imgs = new MyImg [9]; / / the map that stores the location of the picture private int [] map = new int [9]; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); / / nothing setContentView (R.layout.activity_main) is set in the main layout. / / set the width and height of the generated view object ViewGroup.LayoutParams params = new LinearLayout.LayoutParams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); initImg (params); randomMap (); addImg (params); / / start a new game Button newBtn = new Button (this); newBtn.setText ("New Game"); newBtn.setTextSize (16) NewBtn.setX (40); / / ViewGroup.LayoutParams p1 = new LinearLayout.LayoutParams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); addContentView (newBtn, p1); newBtn.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {newGame ();}}) } / / add a picture to the layout and set the click event private void addImg (ViewGroup.LayoutParams params) {for (int I = 0; I < 3; iTunes +) {for (int j = 0; j < 3; Jake +) {int index = I * 3 + j; / calculate x, y coordinates int x = j * W + MARGIN Int y = I * W + MARGIN; ImageView imgView = IMGs [map] .getImg (); imgView.setX (x); imgView.setY (y); addContentView (imgView, params) ImgView.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {ImageView tempImg = IMGS [NULLINDEX] .getImg (); int x = (int) v.getX (); int y = (int) v.getY () / / goal image int x1 = (int) tempImg.getX (); int y1 = (int) tempImg.getY (); / / move top if (y-y1 = = W & & x = = x1) {top ((ImageView) v) } else if (y-y1 = =-W & & x = = x1) {down ((ImageView) v);} else if (x-x1 = = W & & y = = y1) {left ((ImageView) v) } else if (x-x1 =-W & & y = = y1) {right ((ImageView) v);} if (isWin ()) {Toast.makeText (MainActivity.this, "You Win!", Toast.LENGTH_SHORT). Show () });} private void newGame () {randomMap (); / / set the x, y coordinates of the picture for (int I = 0; I < 3; iTunes +) {for (int j = 0; j < 3) MARGIN; int y +) {int index = I * 3 + j; int x = j * W + MARGIN; int y = I * W + MARGIN; ImageView imgView = IMGs [index] .getImg (); imgView.setX (x); imgView.setY (y) } private void left (ImageView img) {img.setX (img.getX ()-W); IMGS [NULLINDEX] .getImg (). SetX (img.getX () + W);} private void right (ImageView img) {img.setX (img.getX () + W); IMGS [NULLINDEX] .getImg (). SetX (img.getX ()-W) } private void top (ImageView img) {img.setY (img.getY ()-W); IMGS [NULLINDEX] .getImg (). SetY (img.getY () + W);} private void down (ImageView img) {img.setY (img.getY () + W); IMGS [NULLINDEX] .getImg (). SetY (img.getY ()-W) } private boolean isWin () {/ / calculates the position of the picture according to the coordinates of x, y. If there is an one-to-one correspondence, it takes a long time to win for (int I = 0; I < 9; iTunes +) {ImageView img = IMGS [I] .getImg (); int x = (int) img.getX (); int y = (int) img.getY () Int index = (y-MARGIN) / W * 3 + (x-MARGIN) / W; / / if there is a mismatch, it does not win if (index! = imgs.getType ()) {return false;}} return true } private void randomMap () {/ / disturbs the location of the map int a, b; for (int I = 0; I < 50; iSum +) {a = (int) (Math.random () * 9); b = (int) (Math.random () * 9); int t = map [a]; map [a] = map [b] Map [b] = t;}} / / arrange an array of pictures private void initImg (ViewGroup.LayoutParams params) {int [] imgId = {R.drawable.img10, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9}; for (int I = 0 I < 3; iTunes +) {for (int j = 0; j < 3; jacks +) {int index = I * 3 + j; imgs [index] = new MyImg (index); MyImg img = imgs [index]; ImageView image = new ImageView (this); params.width = W; params.height = W Image.setLayoutParams (params); image.setImageResource (imgID); img.setImg (image); / / Let the map initialize map [index] = index;}
MyImg class
Package packageName;import android.widget.ImageView;public class MyImg {/ / Index private int type; private ImageView img; public MyImg (int type) {this.type = type;} public void setImg (ImageView img) {this.img = img;} public ImageView getImg () {return img } / / get picture index public int getType () {return type;}} after reading this article, I believe you have a certain understanding of "how to use Android to achieve jigsaw puzzles". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!