Android Getting Started note-Interface development-Animation

Source: Internet
Author: User

Today we look at the animation effect in Android, the example is relatively simple:

    • Alphaanimation: Transparency Animation
    • Scaleanimation: Scaling Animations
    • Translateanimation: Moving position animations
    • Rotateanimation: Rotation angle Animation

Paste the code First:

This instance is fully implemented in code, and of course it can be implemented using an XML file, which we'll look at first:

Package Com.example.demo5_03_animation;import Android.os.bundle;import Android.annotation.suppresslint;import Android.app.activity;import Android.view.menu;import Android.view.view;import Android.view.animation.alphaanimation;import Android.view.animation.animation;import Android.view.animation.rotateanimation;import Android.view.animation.scaleanimation;import Android.view.animation.translateanimation;import Android.widget.button;import Android.widget.ImageView;public Class Mainactivity extends Activity {private ImageView mimg; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); mimg = (ImageView) Findviewbyid ( R.ID.IMG); Button Btn_alpha = (button) Findviewbyid (R.id.btn_alpha); Btn_alpha.setonclicklistener (new View.onclicklistener () {@ overridepublic void OnClick (View v) {Animation Animalpha = new Alphaanimation (1.0f, 0.0f); Animalpha.setduration (1000*3) ; Mimg.startanimation (Animalpha);}}); Button Btn_scale =(Button) Findviewbyid (R.id.btn_scale), Btn_scale.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {Animation Animscale = new Scaleanimation (1.0f, 1.3f, 1.0f, 1.3f); Animscale.setduration (1000*3); Mimg.startanimation (Animscale);}}); Button btn_translate = (button) Findviewbyid (r.id.btn_translate); Btn_translate.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {Animation Animalpha = new Translateanimation (- Mimg.getwidth (), 0, 0, 0); Animalpha.setduration (1000*1); mimg.startanimation (Animalpha);}); Button btn_rotate = (button) Findviewbyid (r.id.btn_rotate); Btn_rotate.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {Animation Animalpha = new Rotateanimation (0.0f, 90.0f); Animalpha.setduration ( 1000*3); mimg.startanimation (Animalpha);});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu. Main, menu); return true;}} 
There is nothing to talk about, here is mainly to provide examples for everyone.

Then let's take a look at how the XML file is implemented: 1. Using XML to configure animation, you first need to create a anim/folder under Res/, creating the desired animation profile, such as: Res/anim/anim_alpha.xml

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" >    <alpha        android:duration= "" "        android:fromalpha=" 1.0 "        android:toalpha=" 0.5 "/></ Set>

2. The animation is then loaded in the activity:

Button Btn_alpha = (button) Findviewbyid (R.id.btn_alpha); Btn_alpha.setonclicklistener (new View.onclicklistener () {@ overridepublic void OnClick (View v) {//animation Animalpha = new Alphaanimation (1.0f, 0.0f);//animalpha.setduration ( 1000*3);//mimg.startanimation (Animalpha); Animation Animationalpha = animationutils.loadanimation (Mcontext, R.anim.anim_alpha); Mimg.startanimation ( Animationalpha);});
This is a comment on the transparency animation in the previous example, and then it is written in XML loading mode.

Well, the animation is actually very simple to use, other people please do their own research!!

Android Getting Started note-Interface development-Animation

Related Article

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.