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);
动画集合
二、 Drawable Animation 比较有针对性,只是图片的替换。
-三、Property Animation 是通过动画的方式来改变View的属性。
转载于:https://my.oschina.net/u/3015461/blog/1098915
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- android 动画的使用(小总结)
- Android动画之Frame Animation(逐帧动画)
- android仿安卓微信、云播雷达扫描动画效果
- Android-视图动画
- Android 属性动画 源码解析 深入了解其内部实现
- android动画特效之解决解决移动后闪烁现象,解决输入法弹出后位置回复原状,解决两个动画叠加
- Android动画之Tween Animation
- Android XML动画资源文件详细讲解(上)
- [转载]Android ViewPager多页面滑动切换以及动画效果
- 用动画实现android app启动界面的渐变效果
- Android-属性动画&转场动画
- Android 动画效果
- Android动画进阶之一:Drawable Animation
- [Android] android 实现加速小火箭动画效果 位移+透明渐变 动画
- Android自定义语音音频对话识别翻译动画控件
- Android 动画AlphaAnimation类方法
- Android爆炸散落动画:ExplosionField开源
- Android PowerImageView实现,可以播放动画的强大ImageView
- Android自定义view渐变圆形动画
- 【读书笔记】【Android 开发艺术探索】第 7 章 Android 动画深入分析