Android FAQs Summary (iv)

Source: Internet
Author: User

Previous Blog Portal: Android Frequently Asked Questions summary (iii)


16. What are the types of animations, and what are their characteristics?
Today there are three main animations of Android, namely frame animation, tween (Tween) animation, property animation
Frames-per-frame animation is the most easily understood animation, it requires us to the animation process of every static picture is ready, and then display, using the human eye "visual persistence" principle to form an animated effect.
Example: Fat wave dancing?
Material Preparation (total 27 frames):

Fat_po.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><animation-list xmlns:android= "http://schemas.android.com/apk/res/     Android "Android:oneshot =" false "> <item android:drawable=" "@drawable/fat_po_f01" android:duration= "/>" <item android:drawable= "@drawable/fat_po_f02" android:duration= "/> <item android:drawable=" @drawable/F    At_po_f03 "android:duration=" "/> <item android:drawable=" @drawable/fat_po_f04 "android:duration="/> <item android:drawable= "@drawable/fat_po_f05" android:duration= "/> <item android:drawable=" @drawable/    Fat_po_f06 "android:duration=" "/> <item android:drawable=" @drawable/fat_po_f07 "android:duration="/> <item android:drawable= "@drawable/fat_po_f08" android:duration= "/> <item android:drawable=" @drawable/    fat_po_f09 "android:duration=" "/> <item android:drawable=" @drawable/fat_po_f10 "android:duration="/> <item android:drawable= "@drawable/fat_Po_f11 "android:duration=" "/> <item android:drawable=" @drawable/fat_po_f12 "android:duration=",/> &lt Item android:drawable= "@drawable/fat_po_f13" android:duration= "/> <item android:drawable=" @drawable/fat_ Po_f14 "android:duration=" "/> <item android:drawable=" @drawable/fat_po_f15 "android:duration=",/> &lt Item android:drawable= "@drawable/fat_po_f16" android:duration= "/> <item android:drawable=" @drawable/fat_ Po_f17 "android:duration=" "/> <item android:drawable=" @drawable/fat_po_f18 "android:duration=",/> &lt Item android:drawable= "@drawable/fat_po_f19" android:duration= "/> <item android:drawable=" @drawable/fat_ Po_f20 "android:duration=" "/> <item android:drawable=" @drawable/fat_po_f21 "android:duration=",/> &lt Item android:drawable= "@drawable/fat_po_f22" android:duration= "/> <item android:drawable=" @drawable/fat_ Po_f23 "android:duration="/> &Lt;item android:drawable= "@drawable/fat_po_f24" android:duration= "/> <item android:drawable=" @drawable/    Fat_po_f25 "android:duration=" "/> <item android:drawable=" @drawable/fat_po_f26 "android:duration="/> <item android:drawable= "@drawable/fat_po_f27" android:duration= "/> </animation-list>

The OneShot property of the animation-list is used to set whether the animation plays only once, and true is that false indicates loop playback.
Activity Layout xml:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_ Parent "    android:layout_height=" match_parent "    android:gravity=" center ">    <imageview         Android:id= "@+id/image_view"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content"        android:background= "@animator/fat_po"/></linearlayout>

Sets the frame animation to the background of the ImageView.
Java code for activity:
public class MyActivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); ImageView ImageView = (ImageView) Findviewbyid ( R.id.image_view);//Start animation, default Stop ((animationdrawable) Imageview.getbackground ()). Start ();}}

Because the frame by step animation is stopped by default, we need to call its Start method to play the animation.
The effect is as follows:

Tween (Tween) animations and frame-by-frames animation requires that we list each frame of the animation, and that the motion tween only requires us to specify "keyframes" such as the start of the animation, the end of the animation, and the "intermediate frame" in which the animation changes to be completed by the system.
Example: Petal Opening and closing
Animation Material:

Petal Animation XML File:
Close.xml
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android"    android:interpolator= "@android: Anim/linear_interpolator" >        <scale android:fromxscale= "1.0"        android:toxscale= "0.01"        android:fromyscale= "1.0"        android:toyscale= "0.01"        android:pivotx= "50%"        Android:pivoty= "50%"        android:fillafter= "true"        android:duration= "" "/>        <alpha         android: Fromalpha= "1"        android:toalpha= "0.05"        android:duration= "the"/>    <rotate         android: fromdegrees= "0"        android:todegrees= "1800"        android:pivotx= "50%"        android:pivoty= "50%"        Android :d uration= "/></set>"

Petals Close XML file, using scale (size change), alpha (transparency change), rotate (rotation change) Three kinds of animation, the speed of execution is linear linear
Open.xml
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android"    android:interpolator= "@android: Anim/linear_interpolator" >        <scale android:fromxscale= "0.01"        Android:toxscale= "1.0"        android:fromyscale= "0.01"        android:toyscale= "1.0"        android:pivotx= "50%"        Android:pivoty= "50%"        android:fillafter= "true"        android:duration= "" "/>        <alpha         android: Fromalpha= "0.05"        android:toalpha= "1"        android:duration= "" "/>    <rotate         android: fromdegrees= "1800"        android:todegrees= "0"        android:pivotx= "50%"        android:pivoty= "50%"        Android :d uration= "/></set>"

Similar to the close animation.
XML file for the interface:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_ Parent "    android:layout_height=" match_parent "    android:gravity=" center ">    <imageview         Android:id= "@+id/image_view"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content"        android:src= "@drawable/flower"/></linearlayout>

There is only one simple imageview.
Java code for activity:
public class MyActivity extends Activity {private Handler Handler = new Handler () {public void Handlemessage (android.os.Me Ssage msg) {if (flag) imageview.startanimation (close); elseimageview.startanimation (open); flag =!flag;};}; Private Animation Open, close;private ImageView imageview;boolean flag = true; Petal state, True open, false @overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate) ; Setcontentview (r.layout.activity_main); ImageView = (ImageView) Findviewbyid (R.id.image_view);//petals open open = Animationutils.loadanimation (this, r.anim.open);//retains the state Open.setfillafter (true) after the animation has changed;//petals off close = Animationutils.loadanimation (this, r.anim.close)///retains the state Close.setfillafter (true) after the animation has changed;//sets the repeating task new Timer (). Schedule (new TimerTask () {@Overridepublic void Run () {//Send message by handler refresh Uihandler.sendemptymessage (0);}}, 0, 3500);}}

After the close and open animations are loaded, the recurring task of the timer sends the message to handler and plays the animation through handler.
The effect is as follows:

For tweened (Tween) animations We just need to prepare the keyframes for the animation to start and end, where the "middle frame" is generated using multiple transformations (Scale,alpha,rotate,translate) provided by Android.
Property animation
From a certain point of view, property animation is an enhanced version of the motion tween, the powerful property animation is mainly embodied in two aspects:
    1. Motion tweens can only define two keyframes in the "Transparency", "rotation", "scale", "displacement" 4 aspects of the change, but the property animation may define any property changes
    2. Motion tweens animate only the UI component, but property animations can animate almost any object
Because the property animation is more complex, this article does not open a discussion, recommended two great God blog for your reference:Android Properties Animation (property Animation) Full Parse (top)
Android Properties Animation (property Animation) Full Parse (bottom)

Handler, Looper Message Queuing model, the role of each part.
Simple mention of handler, Looper model of the role of the various parts, mainly the following three parts:
    • MessageQueue: Message Queuing, storing pending messages
    • Looper: Encapsulates Message Queuing with handler, thread binding, looping messages using the Loop method
    • Handler: The auxiliary class for message processing, which encapsulates a series of operations such as message posting, processing, and fetching.
For more information, see my previous blog post: Android Looper and handler summary

18. How to Quit app termination
Test: Open 3 activity (ACTIVITYA,ACTIVITYB,ACTIVITYC) in turn and terminate Appsystem.exit (0) in the 3rd activity to override the application method:
public class MyApplication extends application {public void exit () {system.exit (0);}}

Remember to modify the application used in Androidmanifest
The method is called in a third activity:
public class Activityc extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity); Button button = (button) Findviewbyid (R.ID.BTN), Button.settext ("Finish app"); Button.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//Exit app ((MyApplication) getapplication ()). exit ();}});}}

failed, the application process is killed, but the app restarts ... And Activitya and Activityb were "resurrected", killing only ACTIVITYC.
Android.os.Process.killProcess (Android.os.Process.myPid ())
failed, like System.exit (0), although the process was killed, it was restarted, and both Activitya and Activityb were "resurrected".
Activitymanager am= (Activitymanager) This.getsystemservice (Context.activity_service);
Am.killbackgroundprocesses (This.getpackagename ());
failed, no response.
Custom baseactivity Maintenance Activity list Customize a baseactivity:
public class Baseactivity extends activity {//Maintain a list of activity soft references private static list<softreference<activity> > list = new arraylist<softreference<activity>> (), @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); List.add (new softreference<activity> (This));} @Overrideprotected void OnDestroy () {Super.ondestroy (); List.remove (new softreference<activity> (This));} /** * Close all Activity */public void Finishall () {for (softreference<activity> sr:list) {if (Sr.get ()! = null) {sr.ge T (). Finish ();}}}

For Activitya, Activityb, and ACTIVITYC to inherit baseactivity instead of activity, call the Activityc method in Finishall to close all the activity and then exit the app.
Finishaffinity () directly shuts down the activity used in the same task stack, with the same effect as the previous method, but it comes with Android, which is much more convenient.
In summary, the test results are as follows:
    1. System.exit (0): can only close the current activity, the shutdown process may cause data storage problems, not recommended
    2. Android.os.Process.killProcess (Android.os.Process.myPid ()): Ibid.

    3. Activitymanager am= (Activitymanager) This.getsystemservice (Context.activity_service);
      Am.killbackgroundprocesses (This.getpackagename ()): Invalid test

    4. Custom Baseactivity Maintenance Activity list: You can turn off the activity you started, and then quit the entire app

    5. Finishaffinity (): It is convenient to close all activity,android in the same task stack


What is the difference between the asset directory and the Res directory?
Both the asset directory and the RES directory are directories used in Android to store resources, including:
    • Resources stored in the asset directory represent native resources that cannot be accessed directly by the application, and the application needs to read the resource in the form of a binary stream via Assetmanager
    • Resources under the Res directory can be accessed through the R resource Manifest class, and the Android SDK will create corresponding index entries for them in the R class at compile time

How Android speeds up activity
Personally, there are two main areas that affect the activation time of the activity:
    1. Execution time of callback methods such as OnCreate, OnStart, Onresume, etc.
    2. Inflate time of the interface for activity
For the 1th, we should try to minimize the time-consuming operations in these callback methods (involving databases, pictures, etc.), and if you must perform time-consuming operations, consider the new threading process. For the 2nd, we should reasonably use the various XML optimization tags, and the interface to reduce the number of nested layers of view and drawing time. (You can refer to the Android FAQ (iii) in question 14)

Android FAQs Summary (iv)

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.