Android学习笔记-tween动画之java实现

Android动画分为Tween动画和Frame动画,近期学习了,体tween动画,现在讲学习的心得以及相关知识介绍如下。

Tween又称为补间动画,可以把对象进行缩小、放大、旋转和渐变等操作。

   第一: Tween动画四个主要实现类:
1、AlphaAnimation:渐变(颜色)动画,主要控制透明度变化动画类,常使用AlphaAnimation(float fromAlpha, float toAlpha)来构造;
    fromAlpha:动画开始时的透明度(取值范围为0.0到1.0);
    toAlpha:动画结束时的透明度;
2、ScaleAnimation:主要控制大小变化,常使用ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)来构造;
    fromX:动画开始X坐标上的伸缩尺度(是相对于原来图片的大小而言);
    toX:动画结束X坐标上的伸缩尺度;
    fromY:动画开始Y坐标上的伸缩尺度;
    toY:动画结束Y坐标上的伸缩尺度;
    pivotXType:X坐标上的伸缩模式(类型),取值有:Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF:相对于自身, Animation.RELATIVE_TO_PARENT;
    pivotXValue:X坐标上缩放的中心位置;
    pivotYType:Y坐标上的伸缩模式,取值有:Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT;
    pivotYValue:Y坐标上缩放的中心位置;
3、TranslateAnimation:主要控制位置变换的动画实现类,常使用TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)来构造;
    fromXDelta:动画开始的X坐标;
    toXDelta:动画结束的X坐标;
    fromYDelta:动画开始的Y坐标;
    toYDelta:动画结束的Y坐标;
4、RotateAnimation:主要控制旋转的动画实现类,常使用RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)来构造;
    fromDegrees:旋转开始角度;
    toDegrees:旋转结束角度;
    pivotXType, pivotXValue, pivotYType, pivotYValue与尺度变化动画ScaleAnimation类似;
 
第二:所包含的共用的方法
 
//设置播放时间

animation.setDuration(2000);



//设置重复的次数,记着是重复的次数



animation.setRepeatCount(2);



//设置重复的模式,有两种RESTART:重新开始与REVERSE:反向开始



animation.setRepeatMode(AlphaAnimation.REVERSE);



//启动播放



iv.startAnimation(animation);

 

 

第三:实例,

布局文件我们这样写,

<LinearLayout 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:orientation="vertical"



    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="com.ftf.tween.MainActivity" >



 



    <LinearLayout



        android:layout_width="fill_parent"



        android:layout_height="wrap_content"



        android:orientation="horizontal" >



 



        <Button



            android:onClick="click"



            android:text="透明度"



            android:layout_width="wrap_content"



            android:layout_height="wrap_content"



            android:orientation="horizontal" />



        <Button



            android:onClick="click2"



            android:text="缩放"



            android:layout_width="wrap_content"



            android:layout_height="wrap_content"



            android:orientation="horizontal" />



        <Button



            android:onClick="click3"



            android:text="旋转"



            android:layout_width="wrap_content"



            android:layout_height="wrap_content"



            android:orientation="horizontal" />



        <Button



            android:onClick="click4"



            android:text="平移"



            android:layout_width="wrap_content"



            android:layout_height="wrap_content"



            android:orientation="horizontal" />



        <Button



            android:onClick="click5"



            android:text="组合"



            android:layout_width="wrap_content"



            android:layout_height="wrap_content"



            android:orientation="horizontal" />



    </LinearLayout>



 



    <LinearLayout



        android:layout_width="match_parent"



        android:layout_height="match_parent"



        android:gravity="center" >



 



        <ImageView



            android:id="@+id/iv"



            android:src="@drawable/ic_launcher"



            android:layout_width="wrap_content"



            android:layout_height="wrap_content"



             />



    </LinearLayout>



 



</LinearLayout>

 

 

activity中,这样写:
 

// 透明度变化

 

 

public void click(View view) {



AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);



animation.setDuration(2000);



animation.setRepeatCount(2);



animation.setRepeatMode(AlphaAnimation.REVERSE);



iv.startAnimation(animation);



}



 



public void click2(View view) {



ScaleAnimation animation = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f,



Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,



0.5f);



 



animation.setDuration(2000);



animation.setRepeatCount(2);



animation.setRepeatMode(AlphaAnimation.REVERSE);



iv.startAnimation(animation);



 



}



 



public void click3(View view) {



RotateAnimation animation = new RotateAnimation(0, 360,



Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,



0.5f);



 



animation.setDuration(2000);



animation.setRepeatCount(2);



animation.setRepeatMode(AlphaAnimation.REVERSE);



iv.startAnimation(animation);



}



 



public void click4(View view) {



TranslateAnimation animation = new TranslateAnimation(



Animation.RELATIVE_TO_PARENT, 0.2f,



Animation.RELATIVE_TO_PARENT, 1.0f,



Animation.RELATIVE_TO_PARENT, 0.2f,



Animation.RELATIVE_TO_PARENT, 1.0f);



 



animation.setDuration(2000);



animation.setRepeatCount(2);



animation.setRepeatMode(AlphaAnimation.REVERSE);



iv.startAnimation(animation);



}



 



public void click5(View view) {



//设置混合模式,也即多重播放效果放在一起。



AnimationSet set = new AnimationSet(false);



 



TranslateAnimation pa = new TranslateAnimation(



Animation.RELATIVE_TO_PARENT, 0.2f,



Animation.RELATIVE_TO_PARENT, 1.0f,



Animation.RELATIVE_TO_PARENT, 0.2f,



Animation.RELATIVE_TO_PARENT, 1.0f);



 



pa.setDuration(2000);



pa.setRepeatCount(2);



pa.setRepeatMode(AlphaAnimation.REVERSE);



 



RotateAnimation ra = new RotateAnimation(0, 360,



Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,



0.5f);



 



ra.setDuration(2000);



ra.setRepeatCount(2);



ra.setRepeatMode(AlphaAnimation.REVERSE);



 



ScaleAnimation sa = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f,



Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,



0.5f);



 



sa.setDuration(2000);



sa.setRepeatCount(2);



sa.setRepeatMode(AlphaAnimation.REVERSE);



 



set.addAnimation(sa);



set.addAnimation(ra);



set.addAnimation(pa);



 



iv.startAnimation(set);



}

 

 

你可能感兴趣的:(Android学习)