The principle and practice of realizing banner by using the animation of Android property

Source: Internet
Author: User

In fact , when Android just launched the property animation, I want to use it to design a Banner Control, there is no time to try. At that time to see our application in the Banner, using a timer to control the AutoPlay, set a large number, using the remainder principle to achieve the Viewpager cycle, in the memory analysis, Seemingly insignificant Banner is a large consumption of memory, I believe many of the App's Banner are timer plus Viewpager The way it is implemented.

The recent time is quite rich, can do some research work, therefore intends to attempt to use the attribute animation realization Banner. At present, just try to do it, not yet make it a control, the final example code is the same, just a demonstration of the implementation of the principle, to be followed by the performance of the test, if you can then make a custom control. Make some statements first:

1. Here is just an example of how this Banner is implemented and is not encapsulated as a control

2. the gesture swipe in the example only simulates with a button click

3. because only the principle is demonstrated, the Indicator linkage effect is not added in the example, and there is no Banner Click Response

4.Banner movement speed by animation time control, can be set freely, the example set in a short time, so move faster

5.Banner size temporarily dead, only suitable for NEXUS4 mobile phone, other mobile phone test please get Banner width yourself

6. Please test auto play and left slide, right slide separately .

7. the above issues can be resolved when encapsulated as a formal control, as well as picture caching

8. Additionally Banner playback moves are controlled by the property animation, so 3.0 The following additional animation libraries are required nineoldandroids

Let's take a look (the animation is very poor, on the two static diagram OK, probably can see the Banner of the movement process ):


Let's talk about the implementation principle, currently only test auto-play ads and left and right sliding ads, temporarily did not deal with the auto-play process of the left and wrong sliding behavior. There is no art skills, the papyrus has probably drawn a:


Here is a specific explanation:

1. First of all, the support of any advertisement, the personal test took 4 , the following directly according to 4 explanation principle

2. Look firstBannerlayout. The outermost layer puts aFramelayoutas a container, the reason for usingFramelayout, because subsequent needs to be covered on the bannerIndicator, which is the small dot indicator (not currently in the example). Framelayoutput a level ofLinearLayout, theLinearLayoutin sequence, put4aImageView, each representing an advertisement. It is important to note that initially, the screen width of the phone only shows the first ad, the other3aImageViewcan be understood as invisible on the right side of the phone screen.

3. How to realize the movement of ad bit. As you know, the biggest difference between property animations and previous animations is that property animations actually change their properties, such as color, location, and so on. Yes, it is the use of this, the ImageView to do animation, adapt its horizontal position attributes, to achieve the purpose of mobile advertising bit. For specific animation implementations, refer to the code below.

4. Next see how to achieveBannerAuto Play. At the beginning, at the same time4aImageViewanimate the properties and move them all to the left with the width of one ad bit. The first time the animation is complete, their position is this:1can be understood as moving to the left of the phone screen, the phone screen is the first2an advertisement, section3One, section4ads are still not visible on the right side of the phone screen. And then again on them4To do the above moving animation, so, finish the animation once again, if there is no processing, ranking4Zhang Ye moved to the left side of the phone screen, and then did not see any animation, because they are far away from the phone screen. Therefore, a special treatment is needed to4aImageVieweach time the animation is completed, check your location, is already on the left side of the phone screen, if so, then reset your location, this location is not casually set, you need to set it to the right end, immediately following the previous ad. The effect of this is that wheneverBanneronce moved, the banner that moves to the left will automatically be re-placed to the right end without affecting the visual effect, so you can achieve the effect of loop playback. Personally feel that the advantage of doing so is always only4aImageViewobjects, eliminating the way to take the rest of the cycle, the efficiency should be improved.

5. Finally, the principle of manual left slide right slide is realized. Because it's just a demonstration, this example doesn't really handle gesture swiping, just a button click to simulate. If you always swipe to the left or to the right to handle, but gestures do not like auto-carousel, gesture swipe can be left, right, any number of slides, so also to do special treatment. If you swipe left, the principle is the same as AutoPlay, using the animation to move 4 ImageView to the left one ad bit, then move to the left of the re-append to the right of the last side, any time the left slip is the same processing, So once the left slide is finished, an ad is currently displayed and the remaining 3 can be considered on the right side of the phone screen. For the right slide, which is different from the left slide, it needs to be processed before the right slide, and we need to provide a sliding ad bit before we slide right, so we need to get the rightmost ad bit to the left of the phone screen before performing a right-slip process. In fact, it can be thought that each time the right slide before the phone screen to the left is no ad bit, you need to advance from the far right to get 1 ads to the left of the phone screen.

The following is a sample layout code with some related code:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ".        Mainactivity "> <framelayout android:id=" @+id/banner_container "android:layout_width=" Match_parent "            android:layout_height= "Wrap_content" > <linearlayout android:layout_width= "match_parent"                android:layout_height= "wrap_content" android:orientation= "Horizontal" > <imageview Android:id= "@+id/banner_1" android:layout_width= "704px" android:layout_height= "WR Ap_content "android:src=" @drawable/baNner1 "/> <imageview android:id=" @+id/banner_2 "android:layout_width=" 704px "Android:layout_height=" wrap_content "android:src=" @drawable/banner2 "/> < ImageView android:id= "@+id/banner_3" android:layout_width= "704px" Android:lay out_height= "Wrap_content" android:src= "@drawable/banner3"/> <imageview and                Roid:id= "@+id/banner_4" android:layout_width= "704px" android:layout_height= "Wrap_content"        android:src= "@drawable/banner4"/> </LinearLayout> </FrameLayout> <button        Android:id= "@+id/btn_start" android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:layout_below= "@+id/banner_container" android:text= "Automatic Carousel"/> <button android:id= "@+id/btn _stop "Android:laYout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_below= "@+id/btn_start" Android:enabled= "false" android:text= "auto play and swipe" test "/> <linearlayout android:layout_width=" separately Match_pa            Rent "android:layout_height=" wrap_content "android:layout_below=" @+id/btn_stop "> <button            Android:id= "@+id/btn_left" android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "Analog left slide"/> <button android:id= "@+id/btn_ri Ght "android:layout_width=" 0DP "android:layout_height=" Wrap_content "Android:layout_weigh t= "1" android:text= "Analog right slide"/> </LinearLayout></RelativeLayout>

Processing code for AutoPlay:

/** * Auto Play ad bar, playback speed can be controlled by animation time */public void Doautoplay () {for (int i = 0; i < COUNT;        i++) {doautoanimation (imgarray[i]); }}/** * only applies to autoplay * * @param v */public void doautoanimation (final View v) {v.animate () . x (V.getx ()-v.getwidth ()). Setduration (). Setinterpolator (New Linearinterpolator ()). Setlistener (New            Animator.animatorlistener () {@Override public void Onanimationstart (Animator Animator) {                    } @Override public void Onanimationend (Animator Animator) {if (V.getx () < 0) {                V.setx ((COUNT-1) * v.getwidth ());            } doautoanimation (v);            } @Override public void Onanimationcancel (Animator Animator) {} @Override    public void Onanimationrepeat (Animator Animator) {}}); }

Swipe left to process the code:

    /**     * Analog left swipe banner     */public    void Doleftanimation () {for        (int i = 0; i < COUNT; i++) {            Doleftanima tion (imgarray[i]);        }    }    /**     * After left slide, will slide to the left unseen ad bit to move to the right end     *     * @param v     *    /public void doleftanimation (final View v) {        v.animate (). x (V.getx ()-v.getwidth ()). Setduration (). Setinterpolator (New Linearinterpolator ()). Setlistener (New Animator.animatorlistener () {            @Override public            void Onanimationstart (Animator Animator) {            }            @Override public            void Onanimationend (Animator Animator) {                if (v.getx () < 0) {                    v.setx ((COUNT-1) * V.get Width ());                }            }            @Override public void            onanimationcancel (Animator Animator) {            }            @Override public            Void Onanimationrepeat (Animator Animator) {            }        });    }

swipe right to process code:  

    /** * Simulation to the right slide banner, * because there is no general setting of a large number, and then take the remainder of the principle of implementing the loop; * This example defaults to n banner ads on the phone screen visible and to the right (the visible area of the screen only shows the current banner) * therefore            Before you swipe right, you need to pre-move the rightmost ad bar to the invisible area on the left side of your phone's screen */public void dorightanimation () {for (int i = 0; i < COUNT; i++) { if (imgarray[i].getx () = = Imgarray[i].getwidth () * (COUNT-1)) {Imgarray[i].setx (-imgarray[i].ge                Twidth ());            Break        }} for (int i = 0; i < COUNT; i++) {dorightanimation (imgarray[i]); }}/** * before you slide right, you need to provide an ad bit to swipe right */public void dorightanimation (final View v) {v.animate (). x (v.ge TX () + v.getwidth ()). Setduration. Setinterpolator (New Linearinterpolator ()). Setlistener (New            Animator.animatorlistener () {@Override public void Onanimationstart (Animator Animator) {            } @Override public void Onanimationend (Animator Animator) {} @Override public void OnaniMationcancel (Animator Animator) {} @Override public void Onanimationrepeat (Animator anim    Ator) {}}); }


Complete sample code download when you need to download the data one day, found that they did not download points, so set up 1 points, there is a need to download the support, thank you.

The principle and practice of realizing banner by using the animation of Android property

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.