Use of Android Development animations (ii)

Source: Internet
Author: User

Use of Android Development animations (ii)

This blog is mainly about the use of animation animation in Android development, and the previous blog post is different, the four animation effect, the main use of XML file implementation, improve the reusability of code and maintainability.

The basic steps to use are as follows:

1, first create a Anim folder under the Res folder
2. Create an XML file of four animated effects in this folder (Scale.xml,rotate.xml,alpha.xml,translate.xml)
3. Create a animation object directly in code using Animationutils call static method Loadanimation load
4. Run Animation object


The instance code is as follows:

Package com.example.animationtest2;


Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import android.view.animation.Animation;
Import Android.view.animation.AnimationUtils;
Import Android.widget.Button;
Import Android.widget.ImageView;




public class Mainactivity extends Activity {


Private ImageView ImageView = null;
Private Button Scalebutton = null;
Private Button Translatebutton = null;
Private Button Rotatebutton = null;
Private Button Alphabutton = null;



@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

ImageView = (ImageView) Findviewbyid (r.id.myimage);
Scalebutton = (Button) Findviewbyid (R.id.scalebutton);
Translatebutton = (Button) Findviewbyid (R.id.translatebutton);
Rotatebutton = (Button) Findviewbyid (R.id.rotatebutton);
Alphabutton = (Button) Findviewbyid (R.id.alphabutton);

Scalebutton.setonclicklistener (New Setscalelistener ());
Translatebutton.setonclicklistener (New Settranslatelistener ());
Rotatebutton.setonclicklistener (New Setrotatelistener ());
Alphabutton.setonclicklistener (New Setalphalistener ());

}

Animation Zoom Effect Listener
Class Setscalelistener implements onclicklistener{


@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Animation Animation = animationutils.loadanimation (Mainactivity.this, R.anim.scale);

Imageview.startanimation (animation);
}

}

Animation Move Effect Listener
Class Settranslatelistener implements onclicklistener{


@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Animation Animation = animationutils.loadanimation (Mainactivity.this, r.anim.translate);

Imageview.startanimation (animation);
}

}

Rotate animation effect Listener
Class Setrotatelistener implements onclicklistener{


@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Animation Animation = animationutils.loadanimation (Mainactivity.this, r.anim.rotate);

Imageview.startanimation (animation);
}

}


Fade-in fade-out animation effect listener
Class Setalphalistener implements onclicklistener{


@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Animation Animation = animationutils.loadanimation (Mainactivity.this, R.anim.alpha);

Imageview.startanimation (animation);
}

}





@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}


}




Main layout file Main.xml:

<relativelayout 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:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Mainactivity ">


<textview
Android:id= "@+id/mytext"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>

<linearlayout
Android:id= "@+id/imglayout"
android:layout_below= "@id/mytext"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:padding= "55DP"
>
<imageview
Android:id= "@+id/myimage"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:src= "@drawable/ic_launcher"
/>
</LinearLayout>


<button
Android:id= "@+id/scalebutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@id/imglayout"
android:text= "@string/scale"
/>

<button
Android:id= "@+id/translatebutton"
android:layout_below= "@id/scalebutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/translate"
/>

<button
Android:id= "@+id/rotatebutton"
android:layout_below= "@id/translatebutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/rotate"
/>

<button
Android:id= "@+id/alphabutton"
android:layout_below= "@id/rotatebutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/alpha"
/>


</RelativeLayout>






The four animation effects of the XML file are as follows:

Scale.xml:

<?xml version= "1.0" encoding= "Utf-8"?
<set android: Shareinterpolator= "true" xmlns:android= "http://schemas.android.com/apk/res/android";
    < SCALE&NBSP
        android:fromxscale= "1"
        android:toxscale= "0"
        android:fromyscale= "1"
        android:toyscale= "0"
  & nbsp     android:pivotx= "50%"
        android:pivoty= "50%"
        android:duration=
       />
<!--50%p   represents the middle position of the relative parent control-->
   <!--50%   represents the middle position of the relative itself--
   <!--  absolute position-->
</set>


Rotate.xml:

<?xml version= "1.0" encoding= "Utf-8"?>
<set android:shareinterpolator= "true" xmlns:android= "Http://schemas.android.com/apk/res/android" >
<rotate
android:fromdegrees= "0"
android:todegrees= "-360"
android:pivotx= "50%"
Android:pivoty= "50%"
Android:duration= "2000"
/>
<!--50%p represents the middle position of the relative parent control--
<!--50% means the middle position relative to itself--
<!--50 means absolute position--
<!--360 means clockwise-360 for counterclockwise rotation--
</set>


Alpha.xml:


<?xml version= "1.0" encoding= "Utf-8"?>
<set android:shareinterpolator= "true" xmlns:android= "Http://schemas.android.com/apk/res/android" > <!-- Set equals Animationset Object--
<alpha
Android:fromalpha= "1.0"
Android:toalpha= "0.1"
Android:duration= "2000"
/>
</set>


Translate.xml:

<?xml version= "1.0" encoding= "Utf-8"?>
<set android:shareinterpolator= "true" xmlns:android= "Http://schemas.android.com/apk/res/android" >
<translate
Android:fromxdelta= "0%"
Android:toxdelta= "50%"
Android:fromydelta= "0%"
Android:toydelta= "100%"
Android:duration= "2000"
/>

<!--50%p represents the middle position of the relative parent control--
<!--50% means the middle position relative to itself--
<!--50 means absolute position--

</set>


The implementation results are as follows:



Press the relevant button to see the relevant effects



Use of Android Development animations (ii)

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.