Transparency gradients, rotate animations, zoom animations, comment animations for Android animations

Source: Internet
Author: User

This is one of the little things I did when I was learning Android. The rotation, panning, zooming, and transparency of the image can be achieved.

First, we're going to create an Android project.

Add your own pictures to your drawable-mdpi

Then in the Res directory, create a directory with a name of Anim (animation), and implement the operation of the picture in that directory

First, Anim_alpha.xml defines an animation that implements a transparent gradient. The animation is completely opaque--fully transparent ———— > completely opaque

<pre name= "code" class= "HTML" ><?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "/http Schemas.android.com/apk/res/android "><alpha android:fromalpha=" 1 "    android:toalpha=" 0 "    android: Fillafter= "true"    android:repeatmode= "reverse"    android:repeatcount= "1"    android:duration= "$"/> </set>

Then you create a anim_rotate.xml file that defines an animation that implements rotation, from zero to 720 and from 360 to zero.

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" ><rotate     android:interpolator= "@android: Anim/accelerate_interpolator"    android:fromdegrees= "0"    android:todegrees= "720"    android:pivotx= "50%"    android:pivoty= "50%"    android:duration= ">" </rotate><rotate     android:interpolator= "@android: Anim/accelerate_interpolator"    android: startoffset= "android:fromdegrees="    android:todegrees= "0"    android:pivotx= "50%    " Android:pivoty= "50%"    android:duration= "></rotate></set>"

Create a file with a name of Anim_scale.xml defines an animation that implements scaling, which first enlarges the original image by twice times and then shrinks it to its original size;

<?xml version= "1.0" encoding= "Utf-8"? ><set  xmlns:android= "http://schemas.android.com/apk/res/ Android "><scale android:fromxscale=" 1 "    android:interpolator=" @android: Anim/decelerate_interpolator "    android:fromyscale= "1"    android:toxscale= "2.0"    android:toyscale= "2.0"    android:pivotx= "50%"    android:pivoty= "50%"    android:fillafter= "true"    android:repeatcount= "1"    android:repeatmode= " Reverse "    android:duration=" "/></set>"

Create a file with a name that is anim_translate.xml the translation of the picture moves from the left to the right and then from the right to the left

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" ><translate     android:fromxdelta= "0"    android:toxdelta= "860"    android:fromydelta= "0"    Android : Toydelta= "0"    android:fillafter= "true"    android:repeatmode= "reverse"    android:repeatcount= "1"    android:duration= "></translate> </set>"

So we're going to write main next. Xml:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:id=" @+id/linearlayout1 "android:layout_width=" fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "> <linearlayout android:id=" @+id/linearlayout2 "Android:orienta tion= "Horizontal" android:layout_width= "Match_parent" android:layout_height= "wrap_content" > <Butto        n android:id= "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"        android:text= "Rotate"/> <button android:id= "@+id/button2" android:layout_width= "Wrap_content"        android:layout_height= "wrap_content" android:text= "Pan"/> <button android:id= "@+id/button3" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Zoom"/> &L T Button Android:id= "@+id/buTton4 "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:text=" Transparency gradient " /> </LinearLayout> <imageview android:id= "@+id/imageview1" Android:layout_width= "W Rap_content "android:layout_height=" wrap_content "android:layout_marginleft=" 50px "android:src=" @draw Able/cat "/></linearlayout>

And then modify Mainactivity.java.

Package Com.mingrisoft;import Android.app.activity;import Android.os.bundle;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 {@Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.main); final Animation rotate=animationutils.loadanimation (this, r.anim.anim_rotate);//get "rotate" animation resources final Animation translate=animationutils.loadanimation (this, r.anim.anim_ translate);//get "Pan" animation resources final Animation scale=animationutils.loadanimation (this, r.anim.anim_scale);//get "zoom" Animation Resources Final Animation alpha=animationutils.loadanimation (this, r.anim.anim_alpha);//get "Transparency change" animation resource final ImageView iv= ( ImageView) Findviewbyid (R.ID.IMAGEVIEW1);//Get the Imageviewbutton button1= (Button) Findviewbyid (r.id.button1) to apply the animation effect ;//Get the "Rotate" button button1.setonclicklistener (new OnclicklIstener () {public void OnClick (View v) {iv.startanimation (rotate);//play "rotate" animation}); Button button2= (button) Findviewbyid (r.id.button2);//Get the pan button Button2.setonclicklistener (new Onclicklistener () { public void OnClick (View v) {iv.startanimation (translate);//play "Pan" animation}); Button button3= (button) Findviewbyid (r.id.button3);//Gets the Zoom button Button3.setonclicklistener (new Onclicklistener () { public void OnClick (View v) {iv.startanimation (scale);//play "Zoom" animation}); Button button4= (button) Findviewbyid (r.id.button4);//Gets the Transparency gradient button Button4.setonclicklistener (new Onclicklistener () { public void OnClick (View v) {iv.startanimation (alpha);//play "Transparency gradient" animation});}}

So our little animation is done.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Transparency gradients, rotate animations, zoom animations, comment animations for Android animations

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.