Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Android jigsaw puzzle by Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "Java how to achieve Android jigsaw puzzle". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to achieve Android jigsaw puzzle in Java" can help you solve the problem.

1. Project introduction

This is a mobile Android Mini Game jigsaw puzzle based on Java.

two。 Project principle

Divide a selected picture into many parts, first in 33 format, and click on the grid exchange within a certain period of time to make the figure into a complete picture, even if the pass is successful, so that the level is also easy to design.

3. Project design

We need a container to hold the blocks of these pictures. For convenience, we are going to use RelativeLayout with addRule to implement

For each picture block, we are going to use ImageView

Click swap, and we are going to use traditional TranslationAnimation to implement it.

4. Project realization

Code implementation:

First of all, we are going to be able to cut a picture into nasty pieces and put it in a specified location.

We just need to set the number n, and then divide it by n according to the width or height of the layout, and subtract some margins to get the width and height of our ImageView.

5. Get layout

In the construction method, we get to convert the set margin value to dp; to get the padding value of the layout; the whole is a square, so we take the lowest of the four directions of padding

The main thing in onMeasure is to get the width of the layout, then prepare the picture, initialize our Item, and set the width and height for Item.

6. Prepare the picture

Here, if mBitmap is not set, prepare a spare picture, and then call ImageSplitter.split to cut the picture into n * n and return a List. This requires a new image cutting class ImageSplitterUtil.

After cutting, we need to scramble the order, so we call the sort method. As for the comparator, we use random to randomly compare the size, so we have finished our out-of-order operation, and here we need to create a new ImagePiece class to hold the picture and index.

7. Initialize item

Calculation of Item width: childWidth = (mWidth-mPadding * 2-mMargin * (mColumn-1)) / mColumn

The width of the container, remove its own inner margin, remove the spacing between Item, and then divide by the number of Item lines to get the width of Item.

The next step is to traverse the generated Item and set the Rule according to their location

8. The switch of game pictures

To click on two Item, their pictures can be exchanged, the hidden copy of B moves to the position of B, and the copy of B moves to the position of A.

Set the picture to B, remove the copy of B, and display it, so that it fits perfectly. The user feels that B moved it.

B ditto

9. The judgment of victory in the game

Go through all the Item, according to the Tag to get the real index and of course order comparison, exactly the same victory ~ ~ after the victory to the next level

10. Game cover

Add a background picture, write the name of the game (let's do the jigsaw puzzle), set up a few buttons, and then click start to jump to the game content interface.

Project effect display:

This is the end of the introduction to "how Java implements the Android jigsaw puzzle". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report