Android fragment, frame animations, patch animations, property animations

Source: Internet
Author: User

Fragment:

In the case of Android phones growing, a screen display shows only one content, will appear empty, the layout is not good enough, the screen is not fully utilized. This is usually the case in the tablet, but since the current phone screen is getting bigger, fragment is introduced after 3.0. You can also use the Support class library for backwards compatibility with lower versions. Its life cycle approach is the same as activity

Using fragment allows us to take more advantage of the phone's screen space, he can have multiple fragment in one activity, and multiple fragment can display different content.

We have a simple demo to understand what is fragment and fragment if you use

In this demo we use Framelayout ( frame layout ), framelayout is seldom used, it is very convenient to use in the demo below , all the views added to this layout are displayed in a cascading way. The first added control is placed at the bottom, and the last view added to the frame layout is displayed at the top level, and the previous control overrides the next layer of control.

Requirement: We have a vertical distribution on the left side of a layout file, three buttons, a framelayout on the right, and a different fragment on the right when we click on a different button.

The first step: write the layout file, the layout file contains three buttons to the left, the right is a framelayout. Write three more fill layout files, these three layout files background colors are not the same, there is a textview to distinguish between different layout files.

The second one does not write three fragment Java classes populate three layout files into corresponding Java classes as content displayed on the screen mother

public class Fragment01 extends Fragment {///Return view objects are displayed as fragment01 content on the screen public View Oncreateview (layoutinflater Inflater, ViewGroup container,bundle savedinstancestate) {View v = inflater.inflate (r.layout.fragment, null); return v;}

The third step is to write three buttons in mainactivity.

When the button is clicked, the right side of the screen shows Fragment01

First step new out of Fragment01 object

The second step gets fragment manager Getfragmentmanager ()

Step three Open things begintransaction ()

Fourth step Show content to frame layout Replace last commit thing commit

public void Click1 (View v) {    fragment01 FG = new fragment01 ();    Fragmentmanager fm  = Getfragmentmanager ();    Fragmenttransaction ft = fm.begintransaction ();    Ft.replace (R.ID.FL,FG);    Ft.commit ();    }

Fragment is after 3.0, we can do backwards compatibility, so that the lower version can be used. This is the time to use the Android support library. We change to the support package when we lead the package. But note that, but in the fragment manager, there is no supporting class library, we can use Getsupportfragmentmanager () to get a compatible version of the manager

Passing data in fragment and activity

  Pass the data in the fragment to the activity practice:

In the fragment XML file, put a edittext. There is a button but when we click on the button, we pass the data to the activity's edittext.

Transfer data from activity to fragment

Steps: 1 in the layout file in fragment, we define an input box that defines a button, but the OnClick property can only be used in context, and cannot be used in fragment

We can set the tap to listen to the button

2 in the Mainactivity layout file, define a TextView to display the data

3 We define a method in Mainactivity to set the displayed data for TextView

4 in the Java class of fragment we get the button to set the button to listen, Get the data in the input box through the Getactivity () method to turn it into mainactivity can get and fragment associated activity, this time can call activity in the method to set the value of TextView.

Note: If the IDs of the EditText and TextView in fragment and activity are set to the same, although Findviewbyid is taken in the respective layout file, However, because the activity in the TextView to set the method is called in the fragment, then the activity of the method will take precedence in the fragment layout file, rather than looking for their own layout files.

If the two IDs are the same, the solution is to take the method variable that takes the layout file to the activity and take it from the activity creation method.

   The practice of passing data from activity to fragment:

Step: 1 Define a TextView in the fragment XML file to display the data.

2 we get the defined TextView in the Java class of fragment and create a public method, which can set the value of TextView in a method.

3 We define a edittext in mainactivity to enter data, define a button, and pass the data when clicked

4 When we click on the button we get the data in the EditText, and new out the object that corresponds to the Java class of the fragment to be passed, and pass the value directly with the method that the fragment object calls it.

The life cycle method of the fragment is the same and binding as the activity's life cycle method, and the old fragment object is destroyed when the fragment is switched, and the new fragment object is created

Frame animations:

    A picture constantly switch to form an animated effect, the Android phone's boot interface is done by frame animation

How to use frame animations yourself:

Step: 1 Copy the footage to the drawable

2 Define an XML file in the Drawable directory (you can see the format and code of the XML file in drawable animation in the API)

<animation-list xmlns:android= "Http://schemas.android.com/apk/res/android" android:oneshot= "false" >// False here means that the loop plays true to show only one time    <item android:drawable= "@drawable/g1" android:duration= "/>//" Represents the length of time each picture is displayed    <item android:drawable= "@drawable/g2" android:duration= "duration"/> <item    android:drawable= "@drawable/g3" android:duration= "/></animation-list>

3 Play Frame animations on the screen

ImageView IV = (ImageView) Findviewbyid (R.ID.IV); First get ImageView to play the animation in imageview//set the animation file to ImageView background        iv.setbackgroundresource (r.drawable.animations);//// The Frame animation resource file is specified as the background of the IV        animationdrawable AD = (animationdrawable) iv.getbackground ();//Get IV of Beijing//Play Animation        Ad.start () ;

  

Motion Tweens:

When the original form becomes a new form, the resulting animation is called motion tween    Displacement, rotation, zooming, and transparency , in order to transition the deformation process.

  Displacement

/* Displacement: parameter 10 refers to the start coordinate of X, but does not refer to the position of the X coordinate of the screen, but the true x + 10 parameter 150 of the ImageView is the end coordinate of x, its value is imageview true x + 150//created as the displacement animation object, set the initial animation Start position and end position//translateanimation ta = new Translateanimation (x, y, n, +); * The starting position of the × coordinate, if relative to yourself, 0.5f, then the starting coordinates are real x + 0.5 * I  V Width * The end position of the x-coordinate, if passed in 2, then the end coordinate is the real x + 2 * IV width * The starting point of the y-coordinate, if passed in 0.5f, then the starting coordinate is the real y + 0.5 * IV height * y coordinate of the end position, if passed in 2, then the terminal coordinates is true Y + 2 * IV height */translateanimation TA = new Translateanimation (animation.relative_to_self, 0.5f, Animation.relative_to_self, 2 , Animation.relative_to_self, 0.5f, animation.relative_to_self, 2);//Set animation duration    ta.setduration (2000);//animation repeats the number of times    Ta.setrepeatcount (1);//animation repeats the mode    ta.setrepeatmode (animation.reverse);//The component stays at the end of the    animation after the animation has finished playing Ta.setfillafter (TRUE);//Play    the animated iv.startanimation (TA);//iv indicates where the animated display is displayed by Findviewbyid

  Rotating  

public void rotate (View v) {
The representation from 0 to 720 rotates from half of the width of the real x+ picture in the center of rotation rotateanimation RA = new Rotateanimation (0, 720, ANIMATION.R Elative_to_self, 0.5f,//Set animation duration animation.relative_to_self, 0.5f); Ra.setduration (2000);
Set the number of times the animation is re-sent to play two times ra.setrepeatcount (1); Ra.setrepeatmode (Animation.reverse); Iv.startanimation (RA);}

 Scaling

public void scale (View v) {//sa = new scaleanimation (FromX, ToX, FromY, ToY, Iv.getwidth ()/2, Iv.getheight ()/2); sa = NE W scaleanimation (0.5f, 2, 0.1f, 3, Animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f);  O.5F indicates that the starting width of the animation is 0.5 times times the true width 2 means that the end width of the animation is twice times the actual width of the  last parameter indicates that the position of the center point of rotation is seated by default at the upper corner Sa.setduration (2000); The end position of the fill animation is Sa.setrepeatcount (1); Sa.setrepeatmode (Animation.reverse); Sa.setfillafter (true); Iv.startanimation (SA);

 Transparent

public void Alpha (View v) {AA = new alphaanimation (0, 1);//0 means full transparency. 1 means opaque aa.setduration (sa.setrepeatcount); (1); iv.startanimation (AA);}

We can set all the animations to play together

public void Fly (View v) {Animationset set = new Animationset (false);//false means using his own call-to-device set.addanimation (TA); Set.addanimation (SA); Set.addanimation (RA); set.addanimation (AA); iv.startanimation (set);}

  

Property animations
tweened animation, just an animation effect, the component is actually in the original position, XY has not changed. The property animation is the XY real change, the property animation is added after 3.0 features. Class Library not supported down

Displacement:

    

public void translate (View v) {//target: Which component the animation works on  The first parameter target specifies the component to be animated the  second parameter propertyname specifies which property  of the component to change The third parameter, values, is the variable parameter, which is the new value given to the property objectanimator OA = Objectanimator.offloat (iv, "Translationx", 10, 70, 20, 100); He can move back and forth. We can move the set and get to see what the property is Oa.setduration (); Oa.setrepeatcount (1); Oa.setrepeatmode ( Valueanimator.reverse); Oa.start ();}

  Scaling

    

public void scale (View v) {objectanimator OA = Objectanimator.offloat (iv, "ScaleX", 1, 1.6f, 1.2f, 2); Oa.setduration (2000) ; Oa.start ();}

  

Transparent:

public void Alpha (View v) {objectanimator OA = Objectanimator.offloat (iv, "Alpha", 0, 0.5f, 0.2f, 1); Oa.setduration (2000); Oa.start ();}

Rotating

public void rotate (View v) {objectanimator OA = Objectanimator.offloat (iv, "RotationY", 0,, n);//rotation Specifies clockwise rotation Turn

Property specified as RotationX is inverted vertically
Property specified as RotationY is flipped horizontally

Oa.setduration (+); Oa.setrepeatcount (1); Oa.setrepeatmode (Valueanimator.reverse); Oa.start ();}

Play all animations together 

Create Animator Collection Animatorset set = new Animatorset ();//Set the component to play the animation Set.settarget (BT);//All animations have sequential playback//set.playsequentially (OA , OA2, OA3, OA4);//All animations play together Set.playtogether (OA, OA2, OA3, OA4); Set.start ();

Defining property animations Using XML files

<?xml version= "1.0" encoding= "Utf-8"?>  under Animator file resource type Select Property Animator root element is set< Set xmlns:android= "Http://schemas.android.com/apk/res/android" ><objectanimator     android:propertyname= " Translationx "    android:duration=" "    android:repeatcount=" 1 "    android:repeatmode=" reverse    " Android:valuefrom= " -100"    android:valueto= ">"    </objectAnimator></set>

  

public void XML (View v) {Animator @ = Animatorinflater.loadanimator (this, r.animator.objanimator);//load specified xml// Set which component At.settarget (iv); At.start ();}

  

Android fragment, frame animations, patch animations, property animations

Related Article

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.