Android Fifth Party

Today is full of dry goods~ I found that if I just follow the teacher to do the program, I can easily look confused

 

image

Therefore, we still need a reserve of basic knowledge. Today, we will learn the use of custom controls by explaining the jigsaw puzzle game. We also talked about a little bit before. Those who have learned should be reviewed, and those who have not learned should be previews.

Come~

   When we learn to make small games, we should first have a systematic plan and know how to do it. Like when we want to make a jigsaw puzzle game, the first step should be to import the picture, and then cut the picture. The small pieces of the jigsaw puzzle are not cut by us before, but are realized through code. Then proceed to the next series of work.

1. Remember how we import a picture?

image

 

image

These are these two sentences.

    Of course, if what we want to import is a background image, there is no need to perform any additional processing on the image, just add the background image directly in the RelativeLayout, and how to add it has also been mentioned before.

2. We cut the picture

    Then we need to write a cutting method, we single this method out and put it in a class. Then if it is singled out, our method should be a class method . I still remember that when we talked about java before, we talked about the role of the static modifier many times . If I remember correctly, the method decorated with static is a class method, which is used by the class. It can be called directly through the class or through an instance:

image

This is how our method header is written. The meaning of the parameters in parentheses is: the complete large image bitmap passed in and the number of blocks to be cut. Our jigsaw puzzle is a square, so pieces means that each row is cut into several (each column is cut into several)

Everyone should have discovered that there is a List<ImagePiece> in it. What is this?

Last time we mentioned that List is a collection. I was a little confused when I was studying. What is the difference between it and an array? Sometimes it feels the same. I believe everyone also wants to know the difference:

 

image

·In fact, I have another question here. What type is in the angle brackets after List? Or can it be of any type? It should be like this when understood in terms of sets. Because a set is a synthesis of a class of elements with the same properties. (Who knows, you can tell me, thank you).

 

After the List here is ImagePiece, it refers to the relevant attributes (index value and image) of each small picture after the picture is divided.

We define a class to put this thing specifically:

image

In the picture, I have written some notes for everyone to help understand.

Here we are involved in the setter and getter methods, which I have circled them. The setter and getter methods are a kind of encapsulation, which is of great help to the robustness of the program. What's the point?

 

image

So we understand why these two methods are used. As for the writing method, we need to be familiar with and learn to use it.

    Let's think about it. If we want to cut the picture, how should we cut it? Where are the cut pictures?

    Okay, first answer the first question:

    We must first determine how many times the graph is cut, the usual one is 3*3. Take this as an example, that is, we need to obtain 9 image fragments in sequence. 3 rows and 3 columns. Cutting the picture actually obtains the x and y coordinates of each fragment (take the upper left corner of the picture as the criterion), and then sets the picture information of the fragment, including index and image. We realize it through a for loop, a double loop.

image

Our picture index method is sauce purple:

 

image

These numbers are the index. So the above indexing algorithm will know.

As for the first red frame, it refers to our newly cut picture. With the x and y values ​​in the upper left corner, as well as the width of the fragment, a picture can be obtained.

At this point, our picture cutting function is complete.

        In fact, today I also completed the step of disordering the fragments, but the time limit cannot be written too much. I feel that the focus is on the latter step. . .

 

image

 

    END


Originality is not easy, please support and pay attention~

Guess you like

Origin blog.csdn.net/allein_STR/article/details/113988179