发布于 2016-04-19 07:28:58 | 127 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


在Android应用程序,使用动画效果,能带给用户更好的感觉。做动画可以通过XML或Android代码。本教程中,介绍使用XML来做动画。在这里,介绍基本的动画,如淡入,淡出,旋转等,需要的朋友可以参考下


第一步: 创建anim文件夹放置动画xml文件
在res文件夹下,创建一个anim的子文件夹。

         

第二步: 加载动画
接着在Activity创建一个Animation类,然后使用AnimationUtils类加载动画xml


Animation animFadein; 

@Override
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_fadein); 

txtMessage = (TextView) findViewById(R.id.txtMessage); 
btnStart = (Button) findViewById(R.id.btnStart); 

// 加载动画 
animFadein = AnimationUtils.loadAnimation(getApplicationContext(), 
R.anim.fade_in); 

} 


第三步: 设置动画监听器
如果你要监听动画的事件,如开始,结束等,你需要实现AnimationListener监听器,重写以下方法。
onAnimationEnd(Animation animation) - 当动画结束时调用
onAnimationRepeat(Animation animation) - 当动画重复时调用
onAniamtionStart(Animation animation) - 当动画启动时调用


@Override
public void onAnimationEnd(Animation animation) { 
// 在动画结束后使用 

// check for fade in animation 
if (animation == animFadein) { 
Toast.makeText(getApplicationContext(), "Animation Stopped", 
Toast.LENGTH_SHORT).show(); 
} 

} 

@Override
public void onAnimationRepeat(Animation animation) { 
//当动画重复时使用 

} 

@Override
public void onAnimationStart(Animation animation) { 
//当动画开始使用 

}


最后一步: 让动画动起来啦。可以使用任何UI元素调用startAnimation方法。
以下是一个Textview元素调用的。
txtMessage.startAnimation(animFadein);
完整代码:


FadeInActivity(淡入动画) 
?package com.chaowen.androidanimations; 

import info.androidhive.androidanimations.R; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.view.animation.Animation.AnimationListener; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

/** 
 *  
 * @author chaowen 
 * 
 */
public class FadeInActivity extends Activity implements AnimationListener { 

    TextView txtMessage; 
    Button btnStart; 

    Animation animFadein; 

    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_fadein); 

        txtMessage = (TextView) findViewById(R.id.txtMessage); 
        btnStart = (Button) findViewById(R.id.btnStart); 

        // 加载动画 
        animFadein = AnimationUtils.loadAnimation(getApplicationContext(), 
                R.anim.fade_in); 

        // 设置监听 
        animFadein.setAnimationListener(this); 

        // 按钮 
        btnStart.setOnClickListener(new View.OnClickListener() { 

            @Override
            public void onClick(View v) { 
                txtMessage.setVisibility(View.VISIBLE); 

                // 开始动画 
                txtMessage.startAnimation(animFadein); 
            } 
        }); 

    } 

    @Override
    public void onAnimationEnd(Animation animation) { 
        // 在动画结束后使用 

        // check for fade in animation 
        if (animation == animFadein) { 
            Toast.makeText(getApplicationContext(), "Animation Stopped", 
                    Toast.LENGTH_SHORT).show(); 
        } 

    } 

    @Override
    public void onAnimationRepeat(Animation animation) { 
        //当动画重复时使用 

    } 

    @Override
    public void onAnimationStart(Animation animation) { 
        //当动画开始使用 

    } 

} 


一些重要的XML属性
重要的XML动画属性
android:duration 动画持续时间,时间以毫秒为单位
android:startOffset 动画之间的时间间隔,从上次动画停多少时间开始执行下个动画
android:interpolator 指定一个动画的插入器
android:fillAfter 当设置为true ,该动画转化在动画结束后被应用
android:repeatMode 定义重复的行为
android:repeatCount 动画的重复次数
 
以下是一些基本的动画XML.

Fade In:  淡入
alpha是渐变透明度效果,值由0到1
fade_in.xml 


<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" > 

    <alpha 
        android:duration="1000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" /> 

</set>


Fade Out : 淡出
 以Fade In刚好相反,值由1到0.
fade_out.xml


 <?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" > 

    <alpha 
        android:duration="1000"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="0.0" /> 

</set> 


Cross Fading:  交叉的淡入和淡出
 同时使用Fade in和Fade out可以达到交叉的效果


public class CrossfadeActivity extends Activity implements AnimationListener { 

    TextView txtMessage1, txtMessage2; 
    Button btnStart; 

      
    Animation animFadeIn, animFadeOut; 

    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        // TODO Auto-generated method stub 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_crossfade); 

        txtMessage1 = (TextView) findViewById(R.id.txtMessage1); 
        txtMessage2 = (TextView) findViewById(R.id.txtMessage2); 
        btnStart = (Button) findViewById(R.id.btnStart); 

        // load animations 
        animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(), 
                R.anim.fade_in); 
        animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(), 
                R.anim.fade_out); 

        // set animation listeners 
        animFadeIn.setAnimationListener(this); 
        animFadeOut.setAnimationListener(this); 

        // button click event 
        btnStart.setOnClickListener(new View.OnClickListener() { 

            @Override
            public void onClick(View v) { 

                txtMessage2.setVisibility(View.VISIBLE); 

                txtMessage2.startAnimation(animFadeIn); 

                  
                txtMessage1.startAnimation(animFadeOut); 
            } 
        }); 

    } 

    @Override
    public void onAnimationEnd(Animation animation) { 

  

        if (animation == animFadeOut) { 
            txtMessage1.setVisibility(View.GONE); 
        } 

        if(animation == animFadeIn){ 
            txtMessage2.setVisibility(View.VISIBLE); 
        } 

    } 

    @Override
    public void onAnimationRepeat(Animation animation) { 
        // TODO Auto-generated method stub 

    } 

    @Override
    public void onAnimationStart(Animation animation) { 
        // TODO Auto-generated method stub 

    } 

}


BLink:  若隐若现,酷
blink.xml


 <?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <alpha android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="600"
        android:repeatMode="reverse"
        android:repeatCount="infinite"/> 
</set> 


Zoom In : 放大
zoom_in.xml 


<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" > 

    <scale 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="3"
        android:toYScale="3" > 
    </scale> 

</set> 


Zoom Out: 缩小
zoom_out.xml 


<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" > 

    <scale 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.5"
        android:toYScale="0.5" > 
    </scale> 

</set>


Rotate: 旋转
rotate.xml


 <?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <rotate android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="600"
        android:repeatMode="restart"
        android:repeatCount="infinite"
        android:interpolator="@android:anim/cycle_interpolator"/> 

</set> 


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务