androin各类动画效果实现



Android官网动画说明:http://developer.android.com/intl/es/develop/index.html

 

http://developer.android.com/intl/es/reference/android/view/animation/Animation.html

Animation的几个子类

AlphaAnimation(透明动画), AnimationSet, RotateAnimation(旋转动画), ScaleAnimation,(缩放动画) TranslateAnimation(移动动画)







新建项目LearnAnimationEffect

XML设置动画效果存放在res\anim目录下

aa.xml

<?xml version="1.0" encoding="utf-8"?> <!--透明动画效果--> <alpha     android:fromAlpha="0"     android:toAlpha="1"     android:duration="1000"     xmlns:android="http://schemas.android.com/apk/res/android"> </alpha>

 

Ra.xml
<?xml version="1.0" encoding="utf-8"?> <!--旋转动画效果--> <rotate     android:fromDegrees="0"     android:toDegrees="360"     android:duration="1000"     android:pivotX="50%"     android:pivotY="50%"     xmlns:android="http://schemas.android.com/apk/res/android"> </rotate>
 
Sa.xml
<?xml version="1.0" encoding="utf-8"?> <!--缩放动画效果--> <scale     android:fromXScale="0"     android:toXScale="1"     android:fromYScale="0"     android:toYScale="1"     android:duration="1000"     android:pivotX="50%"     android:pivotY="50%"     xmlns:android="http://schemas.android.com/apk/res/android"> </scale>
 
<?xml version="1.0" encoding="utf-8"?> <!--移动动画效果--> <translate     android:fromXDelta="0"     android:toXDelta="200"     android:fromYDelta="0"     android:toYDelta="200"     android:duration="1000"     xmlns:android="http://schemas.android.com/apk/res/android"> </translate>
 
animation.xml
<?xml version="1.0" encoding="utf-8"?> <!--混合动画效果--> <set     android:shareInterpolator="true"     android:duration="2000"     xmlns:android="http://schemas.android.com/apk/res/android">     <alpha  android:fromAlpha="0"         android:toAlpha="1"/>     <translate         android:fromXDelta="200"         android:toXDelta="0"         android:fromYDelta="200"         android:toYDelta="0"         /> </set>
 
自定义动画效果CustomAnimation.java
package com.czg.com.learnanimationeffect; import android.view.animation.Animation; import android.view.animation.Transformation; /**  * Created by Administrator on 2015/12/29.  */ public class CustomAnimation extends Animation {     @Override  //如果需要知道目标对象的宽高,则需要重写该方法,否则可以不重写该方法      //参数是目标对象的宽高、父集容器的宽高。      public void initialize(int width, int height, int parentWidth, int parentHeight) { //        System.out.println("查看initialize运行是否在applyTransformation之前");         super.initialize(width, height, parentWidth, parentHeight);     }     @Override     //interpolatedTime:第一个参数是补侦时间;我们的动画执行完毕interpolatedTime值为 1,在动画效果执行时由  01之间逐渐增加,知道为1时,结束动画      //t是动画的目标对象      protected void applyTransformation(float interpolatedTime, Transformation t) {         //System.out.println("interpolatedTime的值:"+interpolatedTime); //        t.setAlpha(interpolatedTime);         //t.getMatrix().setTranslate(150*interpolatedTime,200);//横向慢慢移动,纵向快速移动,自定义动画效果主要通过这个方法的参数值进行控制          //t.getMatrix().setTranslate((float)(Math.sin(interpolatedTime*20)*50),0);  //摇头动作          //t.getMatrix().setTranslate(0,(float)(Math.sin(interpolatedTime * 20)*50));//点头动作          t.getMatrix().setTranslate((float)(Math.log(interpolatedTime)*50),(float)(Math.log(interpolatedTime)*20));//         super.applyTransformation(interpolatedTime, t);     } }
 
activity_animation_effect.xml实现各类动画的activity
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"     android:orientation="vertical"     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">     <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="向上滑动更多动画效果展示"         android:id="@+id/textView" />     <ScrollView         android:layout_width="match_parent"         android:layout_height="match_parent"         android:id="@+id/scrollView2" >         <LinearLayout             android:orientation="vertical"             android:layout_width="match_parent"             android:layout_height="match_parent">             <!--透明动画-->             <ImageView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:id="@+id/imageView"                 android:layout_gravity="center_horizontal"                 android:onClick="onClickAlphaAnimation"                 android:src="@drawable/btn_state_pressed_true" />             <Button                 android:layout_width="fill_parent"                 android:layout_height="wrap_content"                 android:text="透明动画withXMl"                 android:onClick="onClickAlphaAnimationWithXML"                 android:id="@+id/btnAlphaAnimationWithXML" />             <!--旋转动画-->             <ImageButton                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:id="@+id/btnRotateAnimation"                 android:onClick="onClickRotateAnimation"                 android:src="@drawable/btn_state_pressed_true"                 android:layout_gravity="center_horizontal" />             <Button                 android:layout_width="fill_parent"                 android:layout_height="wrap_content"                 android:text="旋转动画with XML"                 android:onClick="onClickRotateAnimationWithXMl"                 android:id="@+id/btnRotateAnimationWithXML" />             <!-- Translate移动动画效果-->             <Button                 android:layout_width="fill_parent"                 android:layout_height="wrap_content"                 android:text="移动动画效果"                 android:onClick="onClickTranslateAnimation"                 android:id="@+id/btnTranslateAnimation" />             <Button                 android:layout_width="fill_parent"                 android:layout_height="wrap_content"                 android:text="移动动画效果WtihXML"                 android:onClick="onClickTranslateAnimationWithXML"                 android:id="@+id/btnTranslateAnimationWithXML" />             <!--  ScaleAnimation缩放动画效果-->             <Button                 android:layout_width="fill_parent"                 android:layout_height="wrap_content"                 android:text="缩放动画效果"                 android:onClick="onClickScaleAnimation"                 android:id="@+id/btnScaleAnimation" />             <Button                 android:layout_width="fill_parent"                 android:layout_height="wrap_content"                 android:text="缩放动画效果withXML"                 android:onClick="onClickScaleAnimationWithXML"                 android:id="@+id/btnScaleAnimationwithXML" />             <Button                 android:layout_width="fill_parent"                 android:layout_height="wrap_content"                 android:text="混合动画效果"                 android:onClick="onClicknMixtureAnimation"                 android:id="@+id/btnMixtureAnimation" />             <Button                 android:layout_width="fill_parent"                 android:layout_height="wrap_content"                 android:text="混合动画withXMl"                 android:onClick="onClickbtnMixtureAnimationWithXML"                 android:id="@+id/btnMixtureAnimationWithXML" />             <Button                 android:layout_width="fill_parent"                 android:layout_height="wrap_content"                 android:text="动画效果侦听"                 android:onClick="onClickAnimationMonitor"                 android:id="@+id/btnAnimationMonitor" />             <Button                 android:layout_width="fill_parent"                 android:layout_height="wrap_content"                 android:text="自定义动画效果"                 android:onClick="onClickCustomAnimation"                 android:id="@+id/btnCustomAnimation" />         </LinearLayout>     </ScrollView> </LinearLayout>
 
实现各类动画效果的AnimationEffectActivity.java
public class AnimationEffectActivity extends AppCompatActivity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_animation_effect);     } //透明动画      public void onClickAlphaAnimation(View view) {         AlphaAnimation aa=new AlphaAnimation(0,1);//01之间透明明度动画          aa.setDuration(1000);  //时长为1          view.startAnimation(aa);     } //透明动画withXMl     public void onClickAlphaAnimationWithXML(View view) {         view.startAnimation(AnimationUtils.loadAnimation(AnimationEffectActivity.this,R.anim.aa));     } //旋转动画      public void onClickRotateAnimation(View view) {         RotateAnimation ra;         //ra=new RotateAnimation(0,360);//0360读旋转          ra=new RotateAnimation(0,360,100,150);//0360读旋转100,50的中心的像素为中心点选择          ra.setDuration(1000);//这是1          view.startAnimation(ra);     } //旋转动画with XML     public void onClickRotateAnimationWithXMl(View view) {         view.startAnimation(AnimationUtils.loadAnimation(AnimationEffectActivity.this,R.anim.ra));     } //移动动画效果      public void onClickTranslateAnimation(View view) {         TranslateAnimation ta;         ta=new TranslateAnimation(0,200,0,200);  //相对自身的像素          ta.setDuration(1000);         view.startAnimation(ta);     } //移动动画效果WtihXML     public void onClickTranslateAnimationWithXML(View view) {         view.startAnimation(AnimationUtils.loadAnimation(AnimationEffectActivity.this,R.anim.ta));     } //缩放动画效果      public void onClickScaleAnimation(View view) {         ScaleAnimation sa;         //sa=new ScaleAnimation(0,1,0,1);  //左上角开始          sa=new ScaleAnimation(0,1,0,1,100,50);  //左上角开始          sa.setDuration(1000);         view.startAnimation(sa);     } //缩放动画效果withXML     public void onClickScaleAnimationWithXML(View view) {         view.startAnimation(AnimationUtils.loadAnimation(AnimationEffectActivity.this,R.anim.sa));     }     //混合动画效果      public void onClicknMixtureAnimation(View view) {         AnimationSet as;         as=new AnimationSet(true); //动画补侦动画匀速执行或加速执行          as.setDuration(1000);         AlphaAnimation aa=new AlphaAnimation(0,1);         aa.setDuration(1000);         as.addAnimation(aa);         TranslateAnimation ta=new TranslateAnimation(300,0,300,0);         ta.setDuration(1000);         as.addAnimation(ta);         view.startAnimation(as);     }     //混合动画效果      public void onClickbtnMixtureAnimationWithXML(View view) {         view.startAnimation(AnimationUtils.loadAnimation(AnimationEffectActivity.this, R.anim.animation));     }     //动画效果侦听      public void onClickAnimationMonitor(View view) {         Animation a=AnimationUtils.loadAnimation(AnimationEffectActivity.this, R.anim.animation);         a.setAnimationListener(new Animation.AnimationListener() {             @Override  //动画开始              public void onAnimationStart(Animation animation) {                 Toast.makeText(AnimationEffectActivity.this, "动画效果即将开始", Toast.LENGTH_SHORT).show();             }             @Override  //动画结束              public void onAnimationEnd(Animation animation) {                 Toast.makeText(AnimationEffectActivity.this, "动画效果结束", Toast.LENGTH_SHORT).show();             }             @Override  //动画重复              public void onAnimationRepeat(Animation animation) {             }         });         view.startAnimation(a);     }     //自定义动画效果      public void onClickCustomAnimation(View view) {         CustomAnimation ca=new CustomAnimation();         ca.setDuration(1000);         view.startAnimation(ca);     } }
 






你可能感兴趣的:(animation)