<span id="Label3"></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">Explain how to make a hollow heart to a solid heart-shaped transition animation, and then explain the inverse of the Animation. The effect is as Follows:</p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"></p></p>Picture sequence<p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">The principle of frame animation is simple: like the old-fashioned movie film, quickly skim over a few columns of pictures, "frame" is actually a picture, so the first step to create a custom frame animation is to set up a picture sequence.</p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">We have two options: using XML drawable (such as shape Drawable) or using actual images. For simplicity, We use some of the following PNG images directly:</p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"></p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">In Product-level applications, We also need to ensure that the image size can be adapted to different screen resolutions. But now, we're going to throw all the pictures in the res/drawable-mdpi directory. I recommend the name of the image in a self-describing way, such as ic_heart_0.png, ic_heart_1.png and so on ... This way we don't need to look at the pictures to know the order of the Pictures.</p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">But who called me a dick? I chose to name the picture as a percentage of the Fill.</p></p>XML drawable<p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">Now that we have the image to be swept, the next step is to define an XML drawable for the animation, and we have two options: animation-list and Animated-selector.</p></p>Animation-list<p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">Animation-list is the default choice for frame animation because it is available at API 1, and it is very simple. is simply a sequence of pictures passing through the specified sequence and Duration.</p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">Here is an example of a animation-list filled with a solid effect, in Res/drawable/animation_list_filling.xml:</p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"></p></p><pre code_snippet_id="1653660" snippet_file_name="blog_20160419_1_753599" name="code" class="html"><pre code_snippet_id="1653660" snippet_file_name="blog_20160419_1_753599" name="code" class="html"><?xml version= "1.0" encoding= "utf-8"? ><animation-list xmlns:android= "http://schemas.android.com/apk/res/ Android " android:oneshot=" true "> <item android:duration=" @ " android:drawable=" @ Drawable/ic_heart_0 "/> <item android:duration=" android:drawable= " @drawable/ic_heart_25" /> <item android:duration= "$" android:drawable= "@drawable/ic_heart_50"/> <item android:duration= " android:drawable=" @drawable/ic_heart_75 "/> <item android: duration= "android:drawable=" @drawable/ic_heart_100 "/></animation-list></pre></pre><br><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">Each item in the list points to a picture in the picture sequence. We only need to put them in the correct order and add a millisecond for the duration of the Unit.</p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">The following is a animation-list that implements a hollow effect, in Res/drawable/animation_list_emptying.xml:</p></p><pre code_snippet_id="1653660" snippet_file_name="blog_20160419_2_8310600" name="code" class="html"><pre code_snippet_id="1653660" snippet_file_name="blog_20160419_2_8310600" name="code" class="html"><?xml version= "1.0" encoding= "utf-8"? ><animation-list xmlns:android= "http://schemas.android.com/apk/res/android" android:oneshot= "true" > <item android:duration= "android:drawable=" @drawable/ic_heart_10 0 "/> <item android:duration=" android:drawable= "@drawable/ic_heart_75"/> <item android:duration= "android:drawable=" @drawable/ic_heart_50 "/> <item android:duration=" 500 " android:drawable= "@drawable/ic_heart_25"/> <item android:duration= "android:drawable=" @drawa Ble/ic_heart_0 "/></animation-list> </pre></pre><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">You may have noticed that there are android:oneshot= "true" in two code snippets, which is a property of animation-list that stops the animation after the animation has finished Playing. If this property value is set to "false", the animation repeats.</p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">In the actual product, 500 milliseconds is too long, but as a demonstration, I deliberately exaggerated this time. also, a 5-frame picture is not enough to produce a smooth Transition. The number of frames used and the time each frame is displayed depends on the Individual. As a reference, I think the 15-frame picture of 15 milliseconds can be very smooth.</p></p>Animated-selector<p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">The animated-selector is slightly more complicated because it is state-based. Depending on the state of the view (such as checked and active), selector will use the provided transition to transition to the correct State. Animated-selector only works on lollipop, so we need to define an XML in THE-V21 package as Well.</p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; Color:rgb (64,64,64); Font-family: ' Microsoft Yahei ', verdana,sans-serif,simsun; FONT-SIZE:14PX "> Below is an example of a animated-selector, placed in Res/drawable-v21/selector.xml: </p></p><pre code_snippet_id="1653660" snippet_file_name="blog_20160419_3_6217049" name="code" class="html"><pre code_snippet_id="1653660" snippet_file_name="blog_20160419_3_6217049" name="code" class="html"><?xml version= "1.0" encoding= "utf-8"? ><animated-selector xmlns:android= "http://schemas.android.com/apk/ Res/android "> <item android:id=" @+id/on " android:state_activated=" true "> <bitmap android:src= "@drawable/ic_heart_100"/> </item> <item android:id= "@+id/off" > <bitmap android:src= "@drawable/ic_heart_0"/> </item> <transition android:fromid= "@+id/on" android:toid= "@+id/off" android:drawable= "@drawable/animation_emptying" > </transition> <transition android:fromid= "@id/off" android:toid= "@id/on" android:drawable= "@drawable/animation_filling" > </transition></animated-selector></pre></pre><br><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">Take a closer look at how it refers to the previously defined animation-list as Transition.</p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">There is no problem with this animated-selector, but we need to consider Non-lollipop Devices. We define a selector with no animations in res/drawable/selector.xml:</p></p><pre code_snippet_id="1653660" snippet_file_name="blog_20160419_4_9979039" name="code" class="html"><pre code_snippet_id="1653660" snippet_file_name="blog_20160419_4_9979039" name="code" class="html"><?xml version= "1.0" encoding= "utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:state_activated=" true "> <bitmap android:src=" @drawable/ic_heart _100 "/> </item> <item android:state_activated=" false "> <bitmap android:src=" @ Drawable/ic_heart_0 "/> </item></selector></pre></pre><br><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">Now our selector can work on any device. Because we only use the general selector, if on lollipop previous device, Animated-selector will skip the transition animation directly to the end State. Of course lollipop devices are the transition effects we define in Animated-selector.</p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">In the code snippet above, Animated-selector only focuses on the android:state_activated Property. Like the general selector, I define different item for different states, but the difference is that we also define the transition of the animation transitions between different states. In this example, I direct transition to the previously defined Animation-list.</p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">Now we have four XML files: an XML filled with a solid effect, solid to hollow xml, two XML that switches between hollow solids.</p></p>Set ImageView<p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">Now you can set up some pictures to play With. We have three imageview that correspond to the three XML drawable defined Earlier. Put the following code in the layout of your activity:</p></p><pre code_snippet_id="1653660" snippet_file_name="blog_20160419_5_3217643" name="code" class="html"><pre code_snippet_id="1653660" snippet_file_name="blog_20160419_5_3217643" name="code" class="html"><imageview android:id= "@+id/imageview_animation_list_filling" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:background= "@drawable/animation_list_filling" /> <imageview android:id= "@+id/imageview_animation_list_emptying" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:background= "@drawable/animation_list_emptying" /> <imageview android:id= "@+id/imageview_animated_selector" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:background= "@drawable/selector" /></pre></pre><br><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">This is just a few IDs unique, background for the XML drawable we define IMAGEVIEW.</p></p>Start animation<p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">The way to start the animation is different in the two implementations, let's start with animation-list:</p></p>Animation-list<p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">In activity, we get a reference to ImageView and start the Animation. As follows:</p></p><pre code_snippet_id="1653660" snippet_file_name="blog_20160419_6_8931481" name="code" class="java"><pre code_snippet_id="1653660" snippet_file_name="blog_20160419_6_8931481" name="code" class="java">ImageView mimageviewfilling = (ImageView) Findviewbyid (r.id.imageview_animation_list_filling);((animationdrawable) Mimageviewfilling.getbackground ()). Start ();</pre></pre><br><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">Here are the Effects:</p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"></p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">Next is its partner-the reverse process (except The ID is the Same)</p></p><pre code_snippet_id="1653660" snippet_file_name="blog_20160419_7_9934829" name="code" class="java"><pre code_snippet_id="1653660" snippet_file_name="blog_20160419_7_9934829" name="code" class="java">ImageView mimageviewemptying = (ImageView) Findviewbyid (r.id.imageview_animation_list_emptying);(( Animationdrawable) mimageviewemptying.getbackground ()). Start ();</pre></pre><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">The effect is as Follows:</p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"></p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">The code can be placed in OnCreate (which starts automatically when the activity starts) or a onclicklistener (waiting for the user to trigger), depending on yourself!</p></p>Animated-selector<p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">When using animated-selector, the animation will be triggered when the state condition satisfies Selector. In our simple example, we add a click Listener for ImageView in the OnCreate method of the Activity:</p></p><pre code_snippet_id="1653660" snippet_file_name="blog_20160419_8_9236718" name="code" class="java"><pre code_snippet_id="1653660" snippet_file_name="blog_20160419_8_9236718" name="code" class="java">Mimageviewselector.setonclicklistener (new view.onclicklistener () { @Override public void OnClick (View V) { mimageviewselector.setactivated (!mimageviewselector.isactivated ());} );</pre></pre><br><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px">When the user clicks on the heart, it toggles between the solid and the hollow according to the current state, and here is a GIF graph of my heart-shaped loops:</p></p><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"><p style="margin-top:10px; margin-bottom:10px; line-height:25.2px; color:rgb(64,64,64); font-family:‘Microsoft YaHei‘,Verdana,sans-serif,SimSun; font-size:14px"></p></p><p><p></p></p><p><p>Android--frame Animation</p></p></span>
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