Adapterviewanimator: displays an animation when switching between views.
Android: animatefirstview
Specifies whether to apply an animation to the current view when viewanimation is first displayed.
Android: inanimation
Specifies the animation used to display the view.
Android: loopviews
Defines whether to cyclically execute an animation to the first view after it is executed at the end of the list.
Android: outanimation
Specifies the animation used to hide a view.
Code
// Fade-in and fade-out effect
Picflipper. setinanimation (this, Android. R. Animator. fade_in );
Picflipper. setoutanimation (this, Android. R. Animator. fade_out );
Enter the screen from left to right
<? XML version = "1.0" encoding = "UTF-8"?>
<Set xmlns: Android = "http://schemas.android.com/apk/res/android">
<Translate
Android: duration= "3000"
Android: fromxdelta = "-100% P"
Android: toxdelta = "0"/>
</Set>
Go out from left to right
<? XML version = "1.0" encoding = "UTF-8"?>
<Set xmlns: Android = "http://schemas.android.com/apk/res/android">
<Translate
Android: duration= "3000"
Android: fromxdelta = "0"
Android: toxdelta = "100% P"/>
</Set>
Enter the screen from right to left
<? XML version = "1.0" encoding = "UTF-8"?>
<Set xmlns: Android = "http://schemas.android.com/apk/res/android">
<Translate
Android: duration= "3000"
Android: fromxdelta = "100% P"
Android: toxdelta = "0"/>
</Set>
Exit screen from right to left
<? XML version = "1.0" encoding = "UTF-8"?>
<Set xmlns: Android = "http://schemas.android.com/apk/res/android">
<Translate
Android: duration= "3000"
Android: fromxdelta = "100% P"
Android: toxdelta = "0"/>
</Set>
Stackview:
Stack display
Do not put many large images at a time, and the memory will overflow.
You can drag and drop to remove the view from the stack.
Android: loopviews defines whether to display view cyclically.
Example:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: orientation = "horizontal"> <stackview Android: Id = "@ + ID/mstackview" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: loopviews = "true"/> <linearlayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Orientation = "vertical"> <button Android: id = "@ + ID/btn_pre" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "previous" tools: ignore = "hardcodedtext"/> <button Android: Id = "@ + ID/btn_next" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "Next" tools: Ignore = "hardcodedtext"/> </linearlayout>
Package COM. light. android. study; import android. app. activity; import android. content. context; import android. graphics. color; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. view. viewgroup; import android. widget. baseadapter; import android. widget. button; import android. widget. linearlayout; import android. widget. stackview; public class mainactivity ext Ends activity {private stackview; private button next, pre; private int [] mcolors = {color. blue, color. cyan, color. gray, color. green, color. red}; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); Init (); initlistener ();} private void Init () {stackview = (stackview) findviewbyid (R. id. mstackview); // fade-in and fade-out effect stackvi Ew. setinanimation (this, android. r. animator. fade_in); stackview. setoutanimation (this, android. r. animator. fade_out); next = (button) findviewbyid (R. id. btn_next); Pre = (button) findviewbyid (R. id. btn_pre);} private void initlistener () {coloradapter adapter = new coloradapter (this, mcolors); stackview. setadapter (adapter); // the next one. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (View V) {stackview. shownext () ;}}); // The previous pre. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {stackview. showprevious () ;}});} public class coloradapter extends baseadapter {private context mcontext; private int [] mcolors; Public coloradapter (context, int [] colors) {mcontext = context; mcolors = colors;} public int getcount () {return mcolors = NULL? 0: mcolors. length;} public object getitem (INT position) {return mcolors = NULL? Null: mcolors [position];} public long getitemid (INT position) {return position;} public view getview (INT position, view cacheview, viewgroup parent) {linearlayout. layoutparams colorlayoutparams = new linearlayout. layoutparams (100,100); linearlayout colorlayout = new linearlayout (mcontext); colorlayout. setbackgroundcolor (mcolors [position]); colorlayout. setlayoutparams (colorlayoutparams); Return colorlayout ;}}}
Effect:
Adapterviewflipper:
Viewflipper is mainly used to automatically switch views.
Android: autostart
Automatic playback "true" or "false"
Android: flipinterval
Set the view switching interval. The parameter is millisecond.
Setadapter (adapter)
Set the adapter used to provide the data for display for the view of the widget.
Shownext ()
Displays the next subview.
Showprevious ()
Displays the previous subview.
Startflipping ()
Switch the view.
Stopflipping ()
Stop view switching
<? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: Id = "@ + ID/layout" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Orientation = "vertical"> <adapterviewflipper Android: Id = "@ + ID/Flipper" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: flipinterval = "5000" Android: layout_aligntop = "@ ID/layout"> </adapterviewflipper> <button Android: id = "@ + ID/btn_pre" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignbottom = "@ ID/Flipper" Android: layout_alignleft = "@ ID/layout" Android: text = "previous" tools: Ignore = "hardcodedtext"/> <button Android: Id = "@ + ID/btn_next" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignbottom = "@ ID/Flipper" Android: layout_torightof = "@ ID/btn_pre" Android: TEXT = "Next" tools: Ignore = "hardcodedtext"/> <togglebutton Android: Id = "@ + ID/btn_start" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: layout_alignbottom = "@ ID/Flipper" Android: layout_torightof = "@ ID/btn_next" Android: textoff = "Stop automatic playback" Android: texton = "start automatic playback" tools: Ignore = "hardcodedtext"/> </relativelayout>
Layout file of the flipper project:
<?xml version="1.0" encoding="utf-8"?><FrameLayout 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" > <ImageView android:id="@+id/iv_pic" android:layout_width="match_parent" android:layout_height="match_parent" tools:ignore="ContentDescription" /></FrameLayout>
Package COM. light. android. study; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. layoutinflater; import android. view. view; import android. view. view. onclicklistener; import android. view. viewgroup; import android. widget. adapterviewflipper; import android. widget. baseadapter; import android. widget. button; import android. widget. compoundbutton; import android. widget. compoundbutton. oncheckedchangelistener; import android. widget. imageview; import android. widget. togglebutton; public class mainactivity extends activity {private adapterviewflipper picflipper; private button next, pre; private togglebutton start; private int [] images = new int [] {R. drawable. lijiang, R. drawable. qiao, R. drawable. shuangta, R. drawable. shui, R. drawable. xiangbi}; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); Init (); initlistener ();} private void Init () {picflipper = (adapterviewflipper) findviewbyid (R. id. flipper); // picflipper. setinanimation (this, android. r. animator. fade_in); picflipper. setoutanimation (this, android. r. animator. fade_out); next = (button) findviewbyid (R. id. btn_next); Pre = (button) findviewbyid (R. id. btn_pre); Start = (togglebutton) findviewbyid (R. id. btn_start);} private void initlistener () {myflipperadapter adapter = new myflipperadapter (this, images); picflipper. setadapter (adapter); // the next one. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {picflipper. shownext (); picflipper. stopflipping () ;}}); // The previous pre. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {picflipper. showprevious (); picflipper. stopflipping () ;}}); // you can specify start for automatic playback. setoncheckedchangelistener (New oncheckedchangelistener () {@ overridepublic void oncheckedchanged (compoundbutton buttonview, Boolean ischecked) {If (ischecked) {picflipper. startflipping ();} else {picflipper. stopflipping () ;}});} private class myflipperadapter extends baseadapter {private layoutinflater Inflater; private context c; private int [] images; Public myflipperadapter (context, int [] images) {This. C = context; this. images = images; Inflater = layoutinflater. from (c) ;}@ overridepublic int getcount () {return images. length ;}@ overridepublic object getitem (INT position) {// return the image return images [position];} @ overridepublic long getitemid (INT position) {return position ;} @ overridepublic view getview (INT position, view convertview, viewgroup parent) {If (convertview = NULL) {convertview = Inflater. inflate (R. layout. flipper_layout, null);} imageview image = (imageview) convertview. findviewbyid (R. id. iv_pic); image. setimageresource (images [position]); image. setscaletype (imageview. scaletype. center_crop); Return convertview ;}}}
Effect:
Java. Lang. runtimeexception: Unknown animator name: Alpha
Later, I found something worth noting.
After studying fragment animation, we found that the following code is not useful:
Fragmenttransaction Ft = getfragmentmanager ()
. Begintransaction ();
Ft. setcustomanimations (Android. R. anim. fade_in,
Android. R. anim. fade_out );
An error occurs.
Java. Lang. runtimeexception: Unknown animator name: Alpha
After studying it for half a day, we found that fragment animation cannot be used in anim.
Later changed
Fragmenttransaction Ft = getfragmentmanager ()
. Begintransaction ();
Ft. setcustomanimations (Android. R. Animator. fade_in,
Android. R. Animator. fade_out );
Click "fade_in" in anim and "fade_in" in animator to find that:
Fade_in.xml file under anim
Label is Alpha
Fade_in.xml file under animator
The label is objectanimator.
Fragment animation only supports the animation effect labeled objectanimator.
Write the animation type in the propertyname field.