Android Development fragment, frame animation, motion tween, attribute animation introduction

Source: Internet
Author: User
Tags commit xmlns

Fragment:

Android runs on a variety of devices, with small-screen phones, oversized screens and even TVs. For the screen size gap, in many cases, is the first mobile phone to develop a set of apps, and then copy, modify the layout to adapt to the plate God horse super big screen. Can't it be that an app can adapt to both the phone and the tablet at the same time, of course, must have AH. Fragment's appearance is to solve such a problem. You can think of fragment as an integral part of an activity's interface, and even the interface of the activity can be composed entirely of different fragment, and the more handsome fragment has its own lifecycle and the events that receive and process the user, This eliminates the need to write a bunch of code for the event handling of the control in the activity. More importantly, you can dynamically add, replace, and remove a fragment.


Fragment life cycle, fragment must be dependent and activity, so the life cycle of activity will directly affect the fragment life cycle.


Using fragment allows us to use the phone's screen space more fully, he can have multiple fragment in an activity, 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 generally rarely used, in the following demo is very convenient to use, all the views added to this layout are displayed in a cascading manner. 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 controls on the previous level overwrite the next level of control.

Requirements: We are vertically distributed to the left of a layout file, three button, and the right side is a framelayout, and when we click on a different button, the right side shows a different fragment.

Step one: Write the layout file, the layout file contains three buttons to the left, and a framelayout on the right. Write three more fill layout files, the colors of background in these three layout files are different, and a textview is used to differentiate the layout files.

The second one does not write three fragment Java classes populate the corresponding Java classes with three layout files and display them as content on the screen mother

public class Fragment01 extends Fragment {

The returned view object appears on the screen as the contents of the fragment01
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 button handles in mainactivity.

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

The first step to new FRAGMENT01 objects

Step two gets the Fragment manager Getfragmentmanager ()

The third step opens the Thing BeginTransaction ()

The fourth step is to display the contents to the frame layout replace the 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 out after 3.0, we can do backward compatibility, so that the lower version can be used. This is the time to use an Android support class library. We're going to change it to support's bag when we're in the fuse. But note that, but in the fragment manager, there is no support class library, we can use Getsupportfragmentmanager () to obtain a compatible low version of the manager


Passing data in fragment and activity

Transferring data from fragment to activity:
In the fragment XML file, place a edittext. There's also a button, but when we click on the button, we pass the data to the edittext of the activity.
Transfer data from activity to fragment

Step: 1 in the layout file in fragment, we define an input box that defines a button, but the onclick attribute can only be used in the context and is not available in fragment

We can set the button to click to listen

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 the TextView

4 in the Java class of fragment we get button to set up the monitor, when the button clicks, 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 the activity in the method to TextView set the value.

Note: If the ID of the EditText and TextView in the fragment and the activity is set to the same, although Findviewbyid is taken in the respective layout file, However, because the TextView setting in the activity is worth calling in fragment, the methods in the activity are prioritized in the fragment layout file and are not found in their own layout files.
If two IDs are the same, the solution is to take the method variable of the layout file in the activity and get it in the activity creation method.

The practice of transferring 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 Fragment Java class, create a public method that can be TextView set in a value

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 the new object that corresponds to the fragment Java class to be passed, and the method of the fragment object to invoke its value directly.



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



Frame Animation:

A picture of the continuous switch to form an animation effect, Android phone's boot interface is made by frame animation

How to use frame animations yourself:

Step: 1 Copy the footage into the drawable

2 Define an XML file in the Drawable directory (the format and use code of the XML file can be seen in the Drawable animation in the API)

<animation-list xmlns:android= "Http://schemas.android.com/apk/res/android" android:oneshot= "false" >// The false here indicates that the loop plays true to play only once
<item android:drawable= "@drawable/g1" android:duration= "/>//" says duration indicates the length of time each picture shows
<item android:drawable= "@drawable/g2" android:duration= "/>"
<item android:drawable= "@drawable/g3" android:duration= "/>"
</animation-list>



3 Play frame animation on the screen

ImageView IV = (ImageView) Findviewbyid (R.ID.IV); First get ImageView to play the animation in ImageView
Set the animation file to the ImageView background
Iv.setbackgroundresource (r.drawable.animations); Specify the background of the Frame animation resource file as IV
Animationdrawable ad = (animationdrawable) iv.getbackground ();//Get IV's Beijing
Play animation
Ad.start ();



Motion Tween:

When the original form becomes a new form, in order to transition the deformation process, the generated animation is called motion tween displacement, rotation, zooming, transparency

Displacement

/* Displacement:
Parameter 10 refers to the beginning coordinates of X, but not to the position of the screen x coordinates 10, but to the true X + 10 of the ImageView.
Parameter 150 refers to the endpoint coordinates of x, whose value is the true x + 150 of ImageView.

Create a displacement animation object that sets the initial position and end position of the animation
Translateanimation ta = new Translateanimation (10, 150, 20, 140);
* The starting position of the X coordinate, if relative to oneself, pass 0.5f, then the starting point coordinates is true x + 0.5 * IV width
* The end position of the X coordinate, if passed in 2, then the endpoint coordinates are true X + 2 * IV width
* The starting position of the y-coordinate, if the 0.5f is passed in, then the starting coordinate is the true y + 0.5 * IV height
* The end position of the y-coordinate, if passed in 2, then the endpoint coordinates are 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);
The number of times the animation plays repeatedly
Ta.setrepeatcount (1);
Mode of animation repeat playback
Ta.setrepeatmode (Animation.reverse);
After the animation has finished playing, the component stays at the end of the animation
Ta.setfillafter (TRUE);
Play animation
Iv.startanimation (TA); IV indicates where the animated display shows up by Findviewbyid.



Rotating

The public void rotate (View v) {<br>//Represents a rotation from 0 to 720 representing half of the width of the true x+ picture in the center
Rotateanimation ra = new Rotateanimation (0, 720, animation.relative_to_self, 0.5f,
Set animation duration animation.relative_to_self, 0.5f);
Ra.setduration (Watts);<br>//Set the number of times to send a repeat playback 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 = new 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 of 2, which indicates that the animation end width is twice times the true width. The following parameter represents the position of the rotation center point by default is to sit on the upper corner
Sa.setduration (2000);
Fills the end position of an animation
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 (2000);
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 one's own call-pair
Set.addanimation (TA);
Set.addanimation (SA);
Set.addanimation (RA);
Set.addanimation (AA);

Iv.startanimation (set);
}

  



Property Animation
tweened animation, just an animation effect, the component is actually in the original position, XY did not change. And the attribute animation is the XY real change, the attribute animation is 3.0 after adding the characteristic. No down support class library

Displacement:

public void translate (View v) {

Target: Animation to which component the first parameter target specifies the component to display the animation the second parameter propertyname specifies which property of the component you want to change the third parameter values are variable parameters, which is the new value assigned 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 (2000);
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, 180, 90, 360); rotation specifies that clockwise rotation

Property specified as RotationX is a vertical flip
Property specified as RotationY is a horizontal flip

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

All animations play together

Create an 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 ();

To define a property animation using an XML file

<?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= "200"
Android:repeatcount= "1"
Android:repeatmode= "Reverse"
android:valuefrom= "-100"
android:valueto= "100"
>

</objectAnimator>
</set>


public void XML (View v) {
Animator at = Animatorinflater.loadanimator (this, r.animator.objanimator); Loading the specified XML
Set which component to act on
At.settarget (iv);
At.start ();
}



About Android screen adaptation fragment and various types of animation type introduction, we are here

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.