Pit filling records for displaying pictures and playing videos in Android viewpager

Viewpager introduction @ h_ 301_ 5@

The function of viewpager is to slide the view, just as lanucher slides left and right@ H_ 301_ 5@

Viewpager is used to realize the switching effect of multiple pages. This class exists in Google's compatibility package android-support-v4.jar. @ h_ 301_ 5@

ViewPager:@H_ 301_ 5@

1) The viewpager class directly inherits the ViewGroup class, which is a container class in which other view classes can be added@ H_ 301_ 5@

2) The viewpager class needs a pageradapter adapter class to provide it with data@ H_ 301_ 5@

3) Viewpager is often used with fragment, and special fragmentpageradapter and fragmentstatepageradapter classes are provided for viewpager in fragment@ H_ 301_ 5@

4) When writing the application of viewpager, you also need to use two component classes: pagertitlestrip class and pagertabstrip class. Pagertitlestrip class directly inherits from ViewGroup class, and pagertabstrip class inherits pagertitlestrip class, so these two classes are also container classes. However, it should be noted that when defining the layout of XML, these two classes must be child tags of the viewpager tag, otherwise an error will occur.

This article will introduce in detail the contents related to the pit filling by displaying pictures and playing videos in Android viewpager, and share them for your reference and learning. I won't say much below. Let's take a look at the detailed introduction together.

1、 Demand source and realization idea

1. In recent project requirements, the function of playing video and displaying pictures in viewpager is useful. The video is a local video. The initial implementation idea is to initialize photoview and surfaceview according to the picture or video corresponding to the current item location in viewpager. At the same time, determine to remove photoview and surfaceview according to the item location when it is destroyed.

2. The above method can be implemented, but there are two problems. First, the life cycle of mediaplayer is not easy to control and there is a memory leak problem. Second, when three consecutive items are videos, the bug of the last frame of the previous video will appear during the sliding back and forth.

3. The user experience is not improved. Before the initialization of the video player is completed, the first frame of the video will be covered on it. However, it is found that the first frame of the video is inconsistent with the information of the first frame of the video, and a solution will be given through the code later.

4. How to fit the picture and video size to ensure no deformation.

2、 Pit to be filled

1. The essential reason why the life cycle of mediaplayer is not easy to control is that there is only one player in this implementation idea. Frequent initialization and destruction cause problems, so I changed the implementation method later. One item video corresponds to one player.

2. For the bug that the last frame of the previous video will appear during the sliding process, it is found that it is caused by the control surfaceview. Later, the problem is perfectly solved by replacing the playing carrier with TextureView.

3. Essential similarities and differences between surfaceview and TextureView

First: both can draw and render in independent threads, and greatly improve the rendering performance in dedicated GPU threads.

Second: surfaceview specifically provides a drawing interface embedded in the view level. Developers can control the form of the interface, such as size, to ensure the correct position of the interface on the screen. But there are limitations: @ h_ 301_ 5 @ 1. Because it is an independent layer of view, it is more like an independent window, and animation, translation and zoom cannot be added@ H_ 301_ 5 @ 2. Two surfaceviews cannot overlap each other.

Third: texture is more like a general view. Like textview, it can be zoomed, translated and animated. TextureView can only be used in windows with hardware acceleration turned on, and consumes more memory than surfaceview, with a delay of 1-3 frames.

Fourth: when the screen is locked, surfaceview will be destroyed and rebuilt, but TextureView will not!

3、 Specific implementation core code

1. Initialization of viewpager

2. Handler processing messages

3. Processing logic for deleting video or picture

4. Adapter corresponding to viewpager

5 display the fragment corresponding to the picture

6. The picture adapts to the custom view of the height according to the width

7. Play the fragment corresponding to the video

4. Conclusion

The author has realized the project requirements in this way, but because I have less access to audio and video related content, I am moving forward in continuous exploration and learning. If there are any deficiencies, please comment and correct them. Thank you. Let's learn and make progress together.

Well, the above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>