直接调用xml文件中定义的动画方法
主页面
package com.example.lx_bujiandonghua; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.AnimationUtils; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button mpy; private Button mxz; private Button mjb; private Button msf; private Button mpwx; private Button myq; private ImageView mimg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mpy = findViewById(R.id.py); mxz = findViewById(R.id.xz); mjb = findViewById(R.id.jb); msf = findViewById(R.id.sf); mpwx = findViewById(R.id.pwx); myq = findViewById(R.id.yq); mimg = findViewById(R.id.img); mxz.setOnClickListener(this); mjb.setOnClickListener(this); msf.setOnClickListener(this); mpwx.setOnClickListener(this); myq.setOnClickListener(this); mpy.setOnClickListener(this); mimg.setOnClickListener(this); } //使用xml文件中定义来实现动画 @Override public void onClick(View v) { switch (v.getId()){ //平移 case R.id.py: Animation animation3 = AnimationUtils.loadAnimation(this, R.anim.anim_translate); mimg.startAnimation(animation3); break; //渐变 case R.id.jb: Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha); mimg.startAnimation(animation); break; //缩放 case R.id.sf: Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.anim_scale); mimg.startAnimation(animation1); break; //旋转 case R.id.xz: Animation animation2 = AnimationUtils.loadAnimation(this, R.anim.anim_rotate); mimg.startAnimation(animation2); break; //抛物线 case R.id.pwx: break; //一起执行 case R.id.yq: AnimationSet animationSet = (AnimationSet) AnimationUtils.loadAnimation(this,R.anim.anim_zuhe); mimg.startAnimation(animationSet); break; //点击图片执行所有动画 case R.id.img: AnimationSet animationSet1 = (AnimationSet) AnimationUtils.loadAnimation(this,R.anim.anim_zuhe); mimg.startAnimation(animationSet1); break; } } // 直接在代码中调用 // @Override // public void onClick(View v) { // switch (v.getId()){ // case R.id.py: // TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 200); // translateAnimation.setDuration(3000); // mimg.startAnimation(translateAnimation); // break; // case R.id.xz: // //如果不设置中心点则默认左上角 // //RotateAnimation rotateAnimation = new RotateAnimation(0, 360); // RotateAnimation rotateAnimation = new RotateAnimation(0, 360,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); // rotateAnimation.setDuration(3000); // mimg.startAnimation(rotateAnimation); // break; // case R.id.jb: // AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f); // animation.setDuration(3000); // //动画结束后保持当前位置 // animation.setFillAfter(false); // //直接使用动画 // mimg.startAnimation(animation); // break; // case R.id.sf: // ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 1.5f, 0.0f, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, // Animation.RELATIVE_TO_SELF,0.5f); // scaleAnimation.setDuration(3000); // //如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态 // //scaleAnimation.setFillAfter(true); // //如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态 // //scaleAnimation.setFillBefore(false); // //设置循环次数,为-1时无线循环 // //scaleAnimation.setRepeatCount(-1); //// scaleAnimation.setRepeatMode(Animation.RESTART); //// scaleAnimation.setStartOffset(0); // mimg.startAnimation(scaleAnimation); // break; // case R.id.pwx: // break; // case R.id.yq: // getquanbudonghua(); // break; // case R.id.img: // getquanbudonghua(); // Toast.makeText(this,"点击图片进行补间动画的全部展示",Toast.LENGTH_SHORT).show(); // break; // } // } // // public void getquanbudonghua() { // //定义一个AnimationSet把所有动画方法全部放进去 // AnimationSet animationSet = new AnimationSet(true); // //渐变 // AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 1.0f); // //动画时间 // alphaAnimation.setDuration(3000); // //缩放 // ScaleAnimation scaleAnimation1 = new ScaleAnimation(0.0f, 1.5f, 0.0f, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); // scaleAnimation1.setDuration(3000); //// scaleAnimation1.setRepeatMode(Animation.REVERSE); //// scaleAnimation1.setStartOffset(0); //// scaleAnimation1.setInterpolator(this, android.R.anim.decelerate_interpolator);//设置动画插入器 // //移动 // TranslateAnimation translateAnimation1 = new TranslateAnimation(0, 200, 0, 200); // translateAnimation1.setDuration(3000); // //旋转 // RotateAnimation rotateAnimation1 = new RotateAnimation(0, 360,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); // rotateAnimation1.setDuration(3000); // animationSet.addAnimation(alphaAnimation); // animationSet.addAnimation(scaleAnimation1); // animationSet.addAnimation(translateAnimation1); // animationSet.addAnimation(rotateAnimation1); // mimg.startAnimation(animationSet); // return ; // } }
布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" > <ImageView android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center" android:id="@+id/img" android:src="@drawable/qwe"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal"> <Button android:id="@+id/xz" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋转" /> <Button android:id="@+id/py" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="平移" /> <Button android:id="@+id/sf" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="缩放" /> <Button android:id="@+id/jb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="渐变" /> <Button android:id="@+id/pwx" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="抛物线" /> <Button android:id="@+id/yq" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一起执行" /> </LinearLayout> </RelativeLayout>
xml中的五个类
全部动画的类
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:duration="3000" android:fromAlpha="1.0" android:toAlpha="0.0" /> <rotate android:duration="500" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="-360"/> <scale android:duration="500" android:fromXScale="0.0" android:fromYScale="0.0" android:interpolator="@android:anim/decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse" android:startOffset="0" android:toXScale="1.5" android:toYScale="1.5" /> <translate android:duration="500" android:fromXDelta="100" android:fromYDelta="0" android:interpolator="@android:anim/cycle_interpolator" android:toXDelta="0" android:toYDelta="0"/> </set>
平移
<?xml version="1.0" encoding="utf-8"?> <!--fromXDelta,fromYDelta 起始时X,Y座标,屏幕右下角的座标是X:320,Y:480 toXDelta, toYDelta 动画结束时X,Y的座标--> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:fromXDelta="100" android:fromYDelta="0" android:toXDelta="0" android:toYDelta="0" />
缩放
<?xml version="1.0" encoding="utf-8"?> <!-- fromXDelta,fromYDelta 起始时X,Y座标,屏幕右下角的座标是X:320,Y:480 toXDelta, toYDelta 动画结束时X,Y的座标 interpolator 指定动画插入器 fromXScale,fromYScale, 动画开始前X,Y的缩放,0.0为不显示, 1.0为正常大小 toXScale,toYScale, 动画最终缩放的倍数, 1.0为正常大小,大于1.0放大 pivotX, pivotY 动画起始位置,相对于屏幕的百分比,两个都为50%表示动画从自身中间开始 startOffset, 动画多次执行的间隔时间,如果只执行一次,执行前会暂停这段时间,单位毫秒 duration,一次动画效果消耗的时间,单位毫秒,值越小动画速度越快 repeatCount,动画重复的计数,动画将会执行该值+1次 repeatMode,动画重复的模式,reverse为反向,当第偶次执行时,动画方向会相反。restart为重新执行,方向不变--> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:fromXScale="0.0" android:fromYScale="0.0" android:interpolator="@android:anim/decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse" android:startOffset="0" android:toXScale="1.5" android:toYScale="1.5" />
渐变
<?xml version="1.0" encoding="utf-8"?> <!-- fromAlpha:开始时透明度 toAlpha: 结束时透明度 duration:动画持续时间 fillAfter:设置动画结束后保持当前的位置 --> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:fillAfter="false" android:fromAlpha="1.0" android:toAlpha="0.0" />
旋转
<?xml version="1.0" encoding="utf-8"?> <!--fromDegrees 动画开始时的角度 toDegrees 动画结束时物件的旋转角度,正代表顺时针 pivotX 属性为动画相对于物件的X坐标的开始位置 pivotY 属性为动画相对于物件的Y坐标的开始位置--> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:fromDegrees="0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toDegrees="-360" />