总:
1.通过xml定义文件动画和通过Java代码定义动画。下面介绍的是通过XML定义
2.在res定义anim文件,在里面创建动画xml
动画有:
1.旋转动画 : rotate
2.缩放动画 : scale
3.透明度动画 : alpha
4.移位动画 : translate
5.复合动画
1.旋转布局
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="180" android:pivotX="50%" android:pivotY="50%" android:duration="6000" android:fillBefore="true"> </rotate>
2.缩放动画
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="6000" android:fillBefore="true"> </scale>
3.透明度动画
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.9" android:toAlpha="0.1" android:duration="4000" android:fillAfter="true"> </alpha>
4.移位动画
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="-50%p" android:toXDelta="150%p" android:fromYDelta="0" android:toYDelta="0" android:duration="6000" android:fillBefore="true"> </translate>
5.复合动画
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="6000" android:fillAfter="true"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0"/> <scale android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%"/> <rotate android:fromDegrees="0" android:toDegrees="720" android:pivotX="50%" android:pivotY="50%" /> </set>
主要属性及说明:
repeatCount: 重复次数
repeatMode:重复模式,restart默认(重启),reverse反向
fromDegrees:为动画起始时物件的角度
toDegrees:为动画结束时物件旋转的角度
pivotX,pivotY 为动画相对于物件的X、Y坐标的开始位.说明:以上两个属性值从0%-100%中取值,50%为物件的X或Y方向坐标上的中点位置。
startOffset:为动画皮偏移时间
duration:为动画的持续时间
fillAfter:动画结束后,是否保持现状
fillBefore:动画结束后,是否恢复到未动画之前
fromXScale,fromYScale:一开始就是动画的几倍(缩放)
toXScale,toYScale:到动画的几倍
fromAlpha:一开始动画的透明度(透明度)
toAlpha:到动画的透明度
fromXDelta,fromYDelta:动画起始的位置(移动)
toXDelta,to YDelta:动画结束的位置
6.java文件,为图片设置透明度动画
ImageView imgb;
Animation ma1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_main);
imgb = (ImageView) this.findViewById(R.id.img1);
ma1 = AnimationUtils.loadAnimation(this, R.anim.my_alpha);
imgb.startAnimation(ma1);
}
7.使用动画监听
public class MyActivity extends Activity implements Animation.AnimationListener { ImageView imgb; Animation ma1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_main); imgb = (ImageView) this.findViewById(R.id.img1); ma1 = AnimationUtils.loadAnimation(this, R.anim.my_alpha); ma1.setAnimationListener(this); imgb.startAnimation(ma1); } //动画监听,实现,onAnimationStart,onAnimationRepeat,onAnimationEnd三个方法 @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { //动画结束时结束欢迎界面并转到软件的主界面 Intent intent = new Intent("com.rj141.sb.imganimation02.MainActivity"); intent.addCategory(Intent.CATEGORY_DEFAULT); this.startActivity(intent); this.finish(); } @Override public void onAnimationRepeat(Animation animation) { } }
8.MAinActivity界面,布局定义按钮和图片
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ Button rotate,scale,alpha,translate,set; ImageView img; Animation mr,ms,ma,mt,mset; Intent it; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rotate=(Button)this.findViewById(R.id.rotate); rotate.setOnClickListener(this); scale=(Button)this.findViewById(R.id.scale); scale.setOnClickListener(this); alpha=(Button)this.findViewById(R.id.alpha); alpha.setOnClickListener(this); translate=(Button)this.findViewById(R.id.translate); translate.setOnClickListener(this); set=(Button)this.findViewById(R.id.set); set.setOnClickListener(this); img=(ImageView)this.findViewById(R.id.img); mr= AnimationUtils.loadAnimation(this,R.anim.my_rotate); ms= AnimationUtils.loadAnimation(this,R.anim.my_scale); ma= AnimationUtils.loadAnimation(this,R.anim.my_alpha); mt= AnimationUtils.loadAnimation(this,R.anim.my_translate); mset= AnimationUtils.loadAnimation(this,R.anim.my_set); } public void onClick(View v){ int id=v.getId(); switch (id){ case R.id.rotate: img.startAnimation(mr); break; case R.id.alpha: img.startAnimation(ma); break; case R.id.scale: img.startAnimation(ms); break; case R.id.translate: img.startAnimation(mt); break; case R.id.set: img.startAnimation(mset); break; } } }