Android component banner realizes left and right sliding screen effect

What is a banner component? In many Android applications, such as iqiyi client, Baidu meipai, APP treasure, etc., there is a small advertising bar that can be manually slid. This is banner. In practical applications, Banner's information (pictures and click behavior) is configurable in the background and needs to be pulled from the background through the network. There are many examples of manual sliding screen on the Internet, but it is generally only a demo and cannot be used in practice, because it generally does not consider the following problems: image cache, oom, whether it can be flexibly configured, whether to reserve external interfaces and whether it is well encapsulated. Without good packaging, manual sliding screen is added to the code, which will make the code very bad and fragile.

1. The principle is shown in the figure below. The whole component is a FrameLayout, in which there are two views. The first is linear layout, which carries four (or more) views that can slide, as shown in the green background in the figure; The second is a relativelayout. A LinearLayout is placed at the bottom, and several small dots are placed inside the LinearLayout to indicate the index of the current screen. Gesturedetector is used for gesture detection, and onggesturelistener interface is implemented. In order to control the sliding speed, the scroller elastic sliding object is used. The reason why the component inherits FrameLayout is that the dots used for indication appear on the view, and one view overlaps another. This is the characteristic of FrameLayout

2. The function and effect banner properties can be dynamically set. The default number is 4. The default number can be adjusted. Banner information can be obtained from the background. The number of banner is the number of screens. It can slide automatically or manually. Pictures can be downloaded as multiple threads. Common three-level cache strategies (memory, file and network) are adopted to save traffic, The oom exception is handled, and the internal click event is handled. At the same time, the interface function banner is reserved and encapsulated into a ViewGroup class. It is simple to use and requires at least two lines of code

3. The code comments are written in detail and should be well understood. It is divided into two files, one is the banner class, and the other is the interface declaration.

ScrollBanner. java

ComponentCallBack. java

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support 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
分享
二维码
< <上一篇
下一篇>>