zoukankan      html  css  js  c++  java
  • Android中的帧动画与补间动画的使用

    前言


    在日常开发中,我们有时候须要一些好看的动画效果,这时能够充分利用Android提供的这几种动画来实现。


    Android提供了3种类型的动画:


    补间动画:补间动画能够应用于View,让你能够定义一系列关于位置、大小、旋转和透明度的改变。从而让


    View的内容动起来。


    逐帧动画:传统的基于单元格的动画,每一帧显示一个不同的Drawable。

    逐帧动画能够在一个View中显示,并


    使用它的Canvas作为投影屏幕。


    属性动画:属性动画系统差点儿能够让应用程序中的不论什么对象动起来,它是一个框架。在一定时间内,通过使用指


    定的内插技术来影响随意的对象属性。


    补间动画


    补间动画通过对场景中的对象不断进行图像变换(透明度、平移、缩放、旋转)产生的动画效果。

    针对不同的图


    像变换动画,Android提供了AlphaAnimation、ScaleAnimation、RotateAnimation、TranslateAnimation等4个类的支


    持。


    补间动画经经常使用于:


    (1)Activity间的转换。


    (2)Activity内的布局间的转换。


    (3)同样View中的不同内容见的转换。


    (4)为用户提供反馈。比如提示进度、通过“晃动”输入框来说明错误或者无效的数据输入。


    补间动画的分类


    补间动画是使用Animation类创建的,以下的列表说明了可用的动画类型:


    (1)AlphaAnimation:能够改变View的透明度。


    (2)RotateAnimation:能够在XY平面上旋转选中的View。


    (3)ScaleAnimation:同意缩放选中的View。


    (4)TranslateAnimation:能够在屏幕上移动选中的View。


    创建移动补间动画


    移动是最常见的动画效果。能够通过配置动画文件或编写Java代码来实现补间动画的移动效果。

    补间动画文件需


    要放在resanim文件夹中,在动画文件里通过<translate>标签设置移动效果。首先在resanim文件夹下建一个动画文件


    translate_tween.xml,该文件的内容例如以下:


    <?xml version="1.0" encoding="utf-8"?>
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="5000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toXDelta="60"
        android:toYDelta="60" >
    
    </translate>


    public class TranslateTweenActivity extends Activity {
    
    	private ImageView iv_traslate;
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.traslate_layout);
    		initView();
    	}
    	
    	private void initView(){
    		iv_traslate=(ImageView) findViewById(R.id.iv_traslate);
    	}
    	
    	@Override
    	public void onWindowFocusChanged(boolean hasFocus) {
    		Animation animation=AnimationUtils.loadAnimation(this, R.anim.translate_tween);
    		iv_traslate.startAnimation(animation);
    	}
    	
    }


    执行的效果例如以下:




    从上面的配置代码能够看出。<translate>标签中设置了6个属性,这6个属性的含义例如以下:


    (1)android:interpolator:表示动画渲染器。通过android:interpolator属性能够设置动画渲染器。比方:


    accelerate_interpolator(动画加速器)、decelerate_interpolator(动画减速器)和accelerate_decelerate_interpolator(动


    画加速减速器)。动画加速器使动画在開始时速度最慢,然后逐渐加速。动画减速器使动画在開始时速度最快。然后逐


    渐加速。

    动画加速减速器使动画在開始和结束时速度最慢,但在前半部分開始加速,在后半部分開始减速。


    (2)android:fromXDelta:动画起始位置的横坐标。


    (3)android:toXDelta:动画结束位置的横坐标。


    (4)android:fromYDelta:动画起始位置的纵坐标。


    (5)android:toXDelta:动画结束位置的纵坐标。


    (6)android:duration:动画的持续时间。单位是毫秒。也就是说。动画要在android:duration属性指定的时间内


    从起始点移动到结束点。


    装载补间动画文件须要使用android.view.animation.AnimationUtils.loadAnimation方法,该方法的定义例如以下:


    public static Animation loadAnimation(Context context, int id)throws NotFoundException


    当中id表示动画文件的资源ID。装载translate_tween.xml文件的代码例如以下:


    Animation animation=AnimationUtils.loadAnimation(this, R.anim.translate_tween);

    在布局文件里放一个ImageView控件,将补间动画应用到ImageView控件上的方式有例如以下两种。


    (1)使用startAnimation方法。例如以下:

    iv_traslate.startAnimation(animation);
    </pre><pre name="code" class="java">

    
    

    (2)使用Animation.start方法。例如以下:

    iv_traslate.setAnimation(animation);
    animation.start();


    使用上面两种方式開始补间动画都仅仅运行一次。假设想循环显示动画,须要使用例如以下代码将动画设置成循环状态:

    animation.setRepeatCount(Animation.INFINITE);

    假设想通过Java代码实现移动补间动画,能够创建android.view.animation.TranslateAnimation对象,例如以下面代


    码:

    public class TranslateTweenActivity extends Activity {
    
    	private ImageView iv_traslate;
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.traslate_layout);
    		initView();
    	}
    	
    	private void initView(){
    		iv_traslate=(ImageView) findViewById(R.id.iv_traslate);
    	}
    	
    	@Override
    	public void onWindowFocusChanged(boolean hasFocus) {
    		TranslateAnimation mTranslateAnimation=new TranslateAnimation(0, 50, 10, 80);
    		mTranslateAnimation.setInterpolator(new LinearInterpolator());
    		mTranslateAnimation.setDuration(3000);
    		iv_traslate.setAnimation(mTranslateAnimation);
    		mTranslateAnimation.start();
    	}
    	
    }

    效果例如以下:




    通过TranslateAnimation类的构造方法能够设置动画起始位置和结束位置的坐标。

    在创建TranslateAnimation对象


    后。能够通过TranslateAnimation类的例如以下方法设置移动补间动画的其它属性。


    (1)setInterpolator:设置动画渲染器。

    该方法的參数类型是Interpolator,在Android SDK中提供了一些动画渲染


    器。比如LinearInterpolator、AccelerateInterpolator等。当中部分动画渲染器能够在动画文件的<translate>标签的


    android:interpolator属性设置。而有的动画渲染器须要使用Java代码设置。


    (2)setDuration:设置动画的持续时间,该方法相当于设置了<translate>标签的android:duration属性。

    创建缩放补间动画


    通过<scale>标签能够定义缩放补间动画,例如以下代码:


    scale_tween.xml

    <?

    xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" android:fromXScale="0.2" android:toXScale="1.0" android:fromYScale="0.2" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="3000" > </scale>


    public class ScaleActivity extends Activity {
    
    	private ImageView iv_scale;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.scale_layout);
    		initView();
    	}
    
    	private void initView() {
    		iv_scale = (ImageView) findViewById(R.id.iv_scale);
    	}
    
    	@Override
    	public void onWindowFocusChanged(boolean hasFocus) {
    		Animation scaleAnimation = AnimationUtils.loadAnimation(this,
    				R.anim.scale_tween);
    		iv_scale.startAnimation(scaleAnimation);
    	}
    }
    

    效果例如以下:



    <scale>标签和<translate>标签中有些属性同样的,而有些属性是<sclae>标签特有的,这些属性的含义例如以下。


    (1)android:fromXScale:表示沿x轴缩放的起始比例。


    (2)android:toXScale:表示沿x轴缩放的结束比例。


    (3)android:fromYScale:表示沿y轴缩放的起始比例。


    (4)android:toYScale:表示沿y轴缩放的结束比例。


    (5)android:pivotX:表示沿x轴方向缩放的支点位置。假设该属性值为50%,则支点在沿x轴的中心位置。


    (6)android:pivotY:表示沿y轴方向缩放的支点位置。假设该属性值为50%,则支点在沿y轴的中心位置。


    当中前4个属性的取值规则例如以下:


    (1)0.0:表示收缩到没有。


    (2)1.0:表示正常到不收缩。


    (3)大于1.0:表示将控件放大到对应的比例。比如值为1.5,表示放大到原控件的1.5倍。


    (4)小于1.0:表示将控件缩小到对应的比例,比如值为0.5,表示缩小到原控件的50%。


    假设想通过Java代码实现缩放补间动画,能够创建android.view.animation.ScaleAnimation对象,代码例如以下(效果


    同上):


    public class ScaleActivity extends Activity {
    
    	private ImageView iv_scale;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.scale_layout);
    		initView();
    	}
    
    	private void initView() {
    		iv_scale = (ImageView) findViewById(R.id.iv_scale);
    	}
    
    	@Override
    	public void onWindowFocusChanged(boolean hasFocus) {
    		ScaleAnimation mScaleAnimation = new ScaleAnimation(0.2f, 1.0f, 0.2f,
    				1.0f, Animation.RELATIVE_TO_SELF, 0.5f,
    				Animation.RELATIVE_TO_SELF, 0.5f);
    		mScaleAnimation.setInterpolator(new DecelerateInterpolator());
    		mScaleAnimation.setDuration(3000);
    		iv_scale.startAnimation(mScaleAnimation);
    	}
    }
    


    创建旋转补间动画


    通过<rotate>标签能够定义旋转补间动画。代码例如以下:


    <?

    xml version="1.0" encoding="utf-8"?

    > <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="5000" android:repeatMode="restart" android:repeatCount="infinite" > </rotate>



    public class RotateActivity extends Activity{
    	
    	private ImageView iv_rotate;
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.rotate_layout);
    		initView();
    	}
    	
    	private void initView(){
    		iv_rotate=(ImageView) findViewById(R.id.iv_rotate);
    	}
    
    	@Override
    	public void onWindowFocusChanged(boolean hasFocus) {
    		Animation rotateAnimation=AnimationUtils.loadAnimation(this, R.anim.rotate_tween);
    		iv_rotate.startAnimation(rotateAnimation);
    	}
    	
    }

    效果例如以下:



    当中<rotate>标签有两个特殊的属性。

    它们的含义例如以下:


    (1)android:fromDegrees:表示旋转的起始角度。


    (2)android:toDegress:表示旋转的结束角度。


    在<rotate>标签中还使用例如以下两个属性设置旋转的次数和模式。


    (1)android:repeatCount:设置旋转的次数。该属性须要设置一个整数值。假设该值为0。表示不反复播放动


    画。也就是说。对于上面的旋转补间动画。仅仅从0度到360度。动画就会停止。假设属性值大于0,动画会再播放该属


    性指定的次数。

    比如。假设 android:repeatCount属性值为1,动画除了正常播放一次外。还会再播放一次。也就是


    说,前面的旋转补间动画会顺序针旋转两周。

    假设想让补间动画永不停止,能够将android:repeatCount属性值设为


    infinite或-1。该属性的默认值是0。


    (2)android:repeatMode:设置反复的模式,默认值是restart。该属性仅仅有当android:repeatCount属性值大于0


    或是infinite时才起作用。

    android:repeatMode属性值除了能够是restart外。还能够设为reverse,表示偶数次显示动画时


    会做与动画文件定义的方向相反的动作。假设想使用Java代码来设置。能够使用Animation.setRepeatMode方法,该


    方法仅仅接收一个int类型的參数,可取的值是Animation.RESTART和Animation.REVERSE。


    假设想通过Java代码实现旋转补间动画,能够创建android.view.animation.RotateAnimation对象。代码例如以下(效


    果同上):


    public class RotateActivity extends Activity {
    
    	private ImageView iv_rotate;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.rotate_layout);
    		initView();
    	}
    
    	private void initView() {
    		iv_rotate = (ImageView) findViewById(R.id.iv_rotate);
    	}
    
    	@Override
    	public void onWindowFocusChanged(boolean hasFocus) {
    		RotateAnimation mRotateAnimation = new RotateAnimation(0f, 360f,
    				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
    				0.5f);
    		mRotateAnimation.setDuration(5000);
    		mRotateAnimation.setRepeatCount(-1);
    		mRotateAnimation.setRepeatMode(Animation.RESTART);
    		iv_rotate.startAnimation(mRotateAnimation);
    	}
    
    }


     创建透明度补间动画


    通过<alpha>标签能够定义透明度补间动画。代码例如以下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <ImageView
            android:id="@+id/iv_alpha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:layout_gravity="center" />
    
    </LinearLayout>



    public class AlphaActivity extends Activity{
    
    	private ImageView iv_alpha;
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.alpha_layout);
    		initView();
    	}
    	
    	private void initView(){
    		iv_alpha=(ImageView) findViewById(R.id.iv_alpha);
    	}
    	
    	@Override
    	public void onWindowFocusChanged(boolean hasFocus) {
    		Animation alphaAnimation=AnimationUtils.loadAnimation(this, R.anim.alpha_tween);
    		iv_alpha.startAnimation(alphaAnimation);
    	}
    	
    }

    效果例如以下:





    当中android:fromAlpha和android:toAlpha属性分别表示起始透明度和结束透明度,这两个属性的值都在0.0~1.0。


    属性值为0.0表示全然透明,属性值为1.0表示全然不透明。


    假设想通过Java代码实现透明度补间动画,能够创建android.view.animation.AlphaAnimation对象,代码例如以下


    (效果同上):

    public class AlphaActivity extends Activity{
    
    	private ImageView iv_alpha;
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.alpha_layout);
    		initView();
    	}
    	
    	private void initView(){
    		iv_alpha=(ImageView) findViewById(R.id.iv_alpha);
    	}
    	
    	@Override
    	public void onWindowFocusChanged(boolean hasFocus) {
    		AlphaAnimation mAlphaAnimation=new AlphaAnimation(1.0f, 0.1f);
    		mAlphaAnimation.setInterpolator(new AccelerateInterpolator());
    		mAlphaAnimation.setDuration(3000);
    		iv_alpha.startAnimation(mAlphaAnimation);
    	}
    	
    }

    使用动画监听器

    AnimationListener能够用于创建一个事件处理程序,当动画開始或者结束的时候触发它。这样就能够在动画開始之前或者结束之后运行某些操作。下面代码监听了动画的開始到结束的事件:
    public class RotateActivity extends Activity {
    
    	private ImageView iv_rotate;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.rotate_layout);
    		initView();
    	}
    
    	private void initView() {
    		iv_rotate = (ImageView) findViewById(R.id.iv_rotate);
    	}
    
    	@Override
    	public void onWindowFocusChanged(boolean hasFocus) {
    		RotateAnimation mRotateAnimation = new RotateAnimation(0f, 360f,
    				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
    				0.5f);
    		mRotateAnimation.setDuration(5000);
    		mRotateAnimation.setRepeatCount(-1);
    		mRotateAnimation.setRepeatMode(Animation.RESTART);
    		mRotateAnimation.setAnimationListener(new AnimationListener() {
    			
    			@Override
    			public void onAnimationStart(Animation animation) {
    				//在动画開始时运行处理
    			}
    			
    			@Override
    			public void onAnimationRepeat(Animation animation) {
    				//在动画反复时运行处理
    			}
    			
    			@Override
    			public void onAnimationEnd(Animation animation) {
    				//在动画完毕后运行处理
    			}
    		});
    		iv_rotate.startAnimation(mRotateAnimation);
    	}
    
    }


    
    

    逐帧动画

    帧动画实际上就是由若干个图像组成的动画,这些图像会以一定的时间间隔进行切换,帧动画有两个比較重要的

    属性,一个是android:oneshot属性,用于设置播放模式(是单次播放还是循环播放)。一个是android:duration属

    性。用于设置每帧的持续时间。单位为毫秒。

    下面代码实现怎样使用帧动画:

    <?

    xml version="1.0" encoding="utf-8"?

    > <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/zzlx1" android:duration="150"/> <item android:drawable="@drawable/zzlx2" android:duration="150"/> <item android:drawable="@drawable/zzlx3" android:duration="150"/> <item android:drawable="@drawable/zzlx4" android:duration="150"/> <item android:drawable="@drawable/zzlx5" android:duration="150"/> <item android:drawable="@drawable/zzlx6" android:duration="150"/> <item android:drawable="@drawable/zzlx7" android:duration="150"/> <item android:drawable="@drawable/zzlx8" android:duration="150"/> </animation-list>


    通过上面的配置代码能够看出,提供了一组图像,没个图像的持续时间为150毫秒。


    public class AnimtionTestActivity extends Activity{
    	
    	private ImageView iv_show;
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.anim_layout1);
    		initView();
    	}
    	
    	private void initView(){
    		iv_show=(ImageView) findViewById(R.id.iv_show);
    	}
    	
    	@Override
    	public void onWindowFocusChanged(boolean hasFocus) {
    		final AnimationDrawable anim=(AnimationDrawable) iv_show.getDrawable();
    		iv_show.post(new Runnable() {
    			
    			@Override
    			public void run() {
    				anim.start();
    			}
    		});
    	}
    }
    

    效果例如以下:









    -------------------------------------------------------------------------------------------------------------------------------------------------------

    转载请注明出处:http://blog.csdn.net/hai_qing_xu_kong/article/details/47659179情绪控





  • 相关阅读:
    Oracle分析函数大全
    Docker容器与容器云之Docker单机集群部署案例
    hive中的几个参数:元数据配置、仓库位置、打印表字段相关参数
    启用hive hwi方法
    hive进行词频统计
    Docker在centos上的安装
    Hive日志(Hive Logging)--hive GettingStarted翻译
    【RMAN】RMAN-05001: auxiliary filename conflicts with the target database
    简单示例用例(Simple Example Use Cases)--hive GettingStarted用例翻译
    hive分析nginx日志之UDF清洗数据
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7084233.html
Copyright © 2011-2022 走看看