Android Properties Animated Listener event with an example of a menu

Source: Internet
Author: User

Simple Listener Events

Package Com.example.animation;import Android.animation.animator;import Android.animation.animator.animatorlistener;import Android.animation.animatorlisteneradapter;import Android.animation.animatorset;import Android.animation.objectanimator;import Android.animation.propertyvaluesholder;import Android.app.activity;import Android.os.bundle;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import android.view.animation.Animation; Import Android.view.animation.animationset;import Android.view.animation.translateanimation;import Android.widget.imageview;import Android.widget.toast;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_    Main);} public void Click (View view) {Toast.maketext (This, "click", Toast.length_short). Show ();} public void Move (view view) {        ImageView imageview= (ImageView) Findviewbyid (R.id.imageview); Objectanimator AnimaTor=new objectanimator (). Offloat (ImageView, "Translationx", 0f,200f);        Animator.setduration (1000); /** * Add all Listener Events *///Animator.addlistener (new Animatorlistener () {////@Override//public void Onanimat Ionstart (Animator arg0) {//}////@Override//public void Onanimationrepeat (Animator arg0) {//}////@Override//public void Onanimationend (Animator arg0) {//toast.maketext (Mainactivity.this, "Animator is end!", Toast.length_short). Show (                );//}////@Override//public void Onanimationcancel (Animator arg0) {//}//}); /** * Add as needed */Animator.addlistener (new Animatorlisteneradapter () {@Override public V OID Onanimationend (Animator animation) {//TODO auto-generated method stub super.onanimationend (animation);        Toast.maketext (Mainactivity.this, "Animator is end!", Toast.length_short). Show ();        }});     Animator.start ();} }

To see a simple example

This is the XML

<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 "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: context= "Com.example.animator.MainActivity" > <imageview android:id= "@+id/imageview2" android:layou T_width= "50DP" android:layout_height= "50DP" android:src= "@drawable/image_b"/> <imageview an Droid:id= "@+id/imageview3" android:layout_width= "50DP" android:layout_height= "50DP" android:src= "@dra Wable/image_c "/> <imageview android:id=" @+id/imageview4 "android:layout_width=" 50DP "Androi d:layout_height= "50DP" android:src= "@drawable/image_d"/&GT        <imageview android:id= "@+id/imageview5" android:layout_width= "50DP" android:layout_height= "50DP" android:src= "@drawable/image_e"/> <imageview android:id= "@+id/imageview6" Android:layout_wid Th= "50DP" android:layout_height= "50DP" android:src= "@drawable/image_f"/> <imageview Android : id= "@+id/imageview7" android:layout_width= "50DP" android:layout_height= "50DP" android:src= "@drawable /image_g "/> <imageview android:id=" @+id/imageview8 "android:layout_width=" 50DP "Android:lay        out_height= "50DP" android:src= "@drawable/image_h"/> <imageview android:id= "@+id/imageview1" Android:layout_width= "50DP" android:layout_height= "50DP" android:src= "@drawable/image_a"/> </ Framelayout>

This is the main program.

Package Com.example.animator;import Java.util.arraylist;import Java.util.list;import Android.animation.objectanimator;import Android.app.activity;import Android.os.bundle;import Android.view.View; Import Android.view.view.onclicklistener;import Android.view.animation.bounceinterpolator;import Android.widget.imageview;import Android.widget.toast;public class Mainactivity extends Activity implements Onclicklistener {private int[] res = {r.id.imageview1, r.id.imageview2, R.ID.IMAGEVIEW3,R.ID.IMAGEVIEW4, R.ID.IMAGEVIEW5, R.id.imageview6, r.id.imageview7,r.id.imageview8};p rivate list<imageview> imageViewList = new Arraylist<imageview> ();p rivate Boolean isOpen = false; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); for (int i = 0; i < Res.length; i++) {ImageView ImageView = (ImageView) Findviewbyid (Res[i]); Imageview.setonclicklistener (this); Imageviewlist.add ( ImageView);}} @Overridepublic VoiD OnClick (view view) {switch (View.getid ()) {case R.id.imageview1:if (!isopen) {Startanim ();} else {Closeanim ();} Break;default:toast.maketext (This, ' you click item! ', Toast.length_short). Show (); break;}} private void Startanim () {for (int i = 1; i < res.length; i++) {Objectanimator animator = Objectanimator.offloat (Imagev Iewlist.get (i), "Translationy", 0f, I *); Animator.setduration (500);//animation difference, interpolator//is used to modify the animation effect, define the rate of change in animation, Can make the existing animation effect accelerated (acceleration), decelerated (deceleration), repeated (repetition), bounced (bounce) and so on. Animator.setinterpolator (New Bounceinterpolator ()); Animator.setstartdelay (i *); Animator.start (); isOpen = True;}} private void Closeanim () {for (int i = 1; i < res.length; i++) {Objectanimator animator = Objectanimator.offloat (Imagev Iewlist.get (i), "Translationy", I *, 0f); animator.setduration; Animator.setinterpolator (new Bounceinterpolator ()); Animator.setstartdelay (i *); Animator.start (); isOpen = false;}}}



Android Properties Animated Listener event with an example of a menu

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.