您的位置:首页 > 移动开发 > Android开发

android动画

2020-03-31 18:51 1146 查看

一、 View Animation 支持缩放、平移、旋转、透明度基本的动画。 

a.透明度

在anim资源文件定义alpha.xml;

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/anticipate_interpolator">
<alpha
android:duration="5000"
android:fromAlpha="0.0"
android:startOffset="500"
android:toAlpha="1.0" />
</set>

使用如下。

ImageView iv= (ImageView) findViewById(R.id.iv);

Animation animation= AnimationUtils.loadAnimation(this,R.anim.alpha);
iv.startAnimation(animation);

或者直接在java中使用

AlphaAnimation alphaAnimation=new AlphaAnimation(0,1);
alphaAnimation.setDuration(5000);
alphaAnimation.setFillAfter(true);

iv.startAnimation(alphaAnimation);

//alphaAnimation.setRepeatCount(int repeatCount);//设置重复次数 

//alphaAnimation.setStartOffset(long startOffset);//执行前的等待时间 

//动画状态监听

alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Log.e("Tag","onAnimationStart");
}

@Override
public void onAnimationEnd(Animation animation) {
Log.e("Tag","onAnimationEnd");
}

@Override
public void onAnimationRepeat(Animation animation) {
Log.e("Tag","onAnimationRepeat");
}
});

当动画次数设置3次是监听的状态变化。

b.平移translate.xml

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

</translate>

</set>

使用如上

Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
iv.startAnimation(animation);
//由上往下
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f,
Animation.RELATIVE_TO_SELF, 0.0f);

translateAnimation.setDuration(3000);
iv.startAnimation(translateAnimation);

//由下往上,停在上面

TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f);
translateAnimation.setDuration(3000);
translateAnimation.setFillAfter(true);
iv.startAnimation(translateAnimation);

c.缩放动画scale.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="2000"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.3"
android:toYScale="0.3"></scale>
</set>

使用

final ImageView iv = (ImageView) findViewById(R.id.iv);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
animation.setFillAfter(true);
iv.startAnimation(animation);

在java中直接使用

ScaleAnimation animation=new ScaleAnimation(1,0.3f,1,0.3f,0.5f,0.5f);
animation.setDuration(3000);
animation.setFillAfter(true);
iv.startAnimation(animation);

d.旋转动画rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="5000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="1060">

</rotate>

</set>
final ImageView iv = (ImageView) findViewById(R.id.iv);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
animation.setFillAfter(true);
iv.startAnimation(animation);

在java中直接使用,动画停止之后点击开始

final RotateAnimation rotateAnimation = new RotateAnimation(0f, 1360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(5000);
rotateAnimation.setFillAfter(true);
iv.startAnimation(rotateAnimation);

iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (flag) {
Toast.makeText(MainActivity.this, "onClick", Toast.LENGTH_SHORT).show();
iv.startAnimation(rotateAnimation);
}
}
});
rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
flag = false;
}

@Override
public void onAnimationEnd(Animation animation) {

flag = true;
}

@Override
public void onAnimationRepeat(Animation animation) {

}
});

动画结合,可以把上面的动画放到一起。

final ImageView iv = (ImageView) findViewById(R.id.iv);

AnimationSet animationSet = new AnimationSet(true);
Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.alpha);
Animation animation2 = AnimationUtils.loadAnimation(this, R.anim.scale);

animationSet.addAnimation(animation1);
animationSet.addAnimation(animation2);

animationSet.setFillAfter(true);
animationSet.setDuration(3000);

iv.setAnimation(animationSet);

//出现消失无限循环

tvHint   = (TextView)this.findViewById(R.id.tv_hint);
Animation ani = new AlphaAnimation(0f,1f);
ani.setDuration(1500);
ani.setRepeatMode(Animation.REVERSE);
ani.setRepeatCount(Animation.INFINITE);
tvHint.startAnimation(ani);

动画集合

https://www.geek-share.com/detail/2664166763.html

http://pan.baidu.com/s/1eSeRoA2

二、 Drawable Animation 比较有针对性,只是图片的替换。 

 

-三、Property Animation 是通过动画的方式来改变View的属性。

 

转载于:https://my.oschina.net/u/3015461/blog/1098915

  • 点赞
  • 收藏
  • 分享
  • 文章举报
chengniqie9367 发布了0 篇原创文章 · 获赞 0 · 访问量 385 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: