安卓动画阶段总结
来源:互联网 发布:淘宝外贸男装店铺推荐 编辑:程序博客网 时间:2024/06/08 05:24
用的最多最常见的是
补间动画:分为四个种类,分别是alpha,translate,scale,rotate四种。
这类动画的细节适用不再描述,使用方式分为两种,一种是现在xml文件中定义好,然后在java代码中取出使用,demo如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="500"
android:fromAlpha="0"
android:toAlpha="1.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:repeatCount="3"
android:repeatMode="reverse"
/>
</set>
Animation anim = AnimationUtil.loadAnimation(con,R.anim.testAnim);
ImageView imageView = new ImageView(con);
imageView.startAnimation(anim);
以上是最常见的一种用法,另一种是直接在java代码中创建动画,创建完直接使用,即创即用,demo如下:
Animation anim1 = new ScaleAnimation(0.5f, 1.0f, 0.5f, 1.0f, 0.5f, 0.5f);
anim1.setDuration(2000);
anim1.setRepeatCount(3);
anim1.setRepeatMode(Animation.REVERSE);
anim1.setInterpolator(this, interpolator.accelerate_decelerate);
anim1.setFillAfter(true);
new ImaheView.startAnimation(animl);
至此,补间动画介绍完毕,下一种是属性动画(ObjectAnimation),一般来说补间动画以及可以满足大部分的开发,但是有一些需求就没法完成,比如,我要改变一个view的颜色,实现红黄蓝不停变换。属性动画就行,属性动画的使用也不难,先实例化一个ObjectAnimation,然后配置一下直接start就行,简易demo如下
imageView.setOnClickListener(new OnClickListener(){
public void onClick(View view){
ObjectAnimation.ofFloat(view,"rotationX",0.0f,360.0f).setDuration(1000).start();
}
});
ObjectAnimator可以通过ofInt、ofFloat、ofObject三种方式来实例化
例如,通过ofFloat(View view,String mode ,float a,float b)来实例化
作为动画,不外乎四中,alpha(透明度),scale(缩放),rotate(旋转),translate(平移)
比如上面写的
ObjectAnimation.offloat(view,"rotationx",0.0f,360.0f).setDuration(300).start();
说明是针对view进行的动画,是一个旋转操作,旋转的其实角度是0.0f。旋转的结束角度是360.0f
此外,属性动画也可以实现多动画,需要用到AnimationSet类,demo如下
Imageview imageView = new ImageView(this);
// 移动动画
ObjectAnimator transAnimator = ObjectAnimator.ofFloat(mTextView, "translationX", -500f, 300f);
// 旋转动画
ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(mTextView, "rotation", 0f, 360f);
// 淡入淡出
ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(mTextView, "alpha", 1f, 0f, 1f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(rotationAnimator).with(alphaAnimator).after(transAnimator);
animatorSet.setDuration(5000);
animatorSet.start();
属性动画还有完整的监听器,可以监听动画的开始,运行,结束等状态,便于大家使用
一个完整的动画具有start、Repeat、End、Cancel四个过程,Android提供了接口,让我们能够很容易监听到这些事件。示例代码如下:
animatorSet.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Log.e(TAG, "onAnimationStart");
}
@Override
public void onAnimationEnd(Animator animation) {
Log.e(TAG, "onAnimationEnd");
}
@Override
public void onAnimationCancel(Animator animation) {
Log.e(TAG, "onAnimationCancel");
}
@Override
public void onAnimationRepeat(Animator animation) {
Log.e(TAG, "onAnimationRepeat");
}
});
帧动画
到此属性动画也介绍完毕,最后是用的最少但是却很有趣的帧动画,一般用来制作小动画。
Frame动画是一系列图片按照一定的顺序展示的过程,和放电影的机制很相似,我们称为逐帧动画。Frame动画可以被定义在XML文件中,也可以完全编码实现。
xml实现方式demo如下:
现在anim文件夹下建立xml文件叫animation.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/f1" android:duration="300" />
<item android:drawable="@drawable/f2" android:duration="300" />
<item android:drawable="@drawable/f3" android:duration="300" />
<item android:drawable="@drawable/f4" android:duration="300" />
</animation-list>
布局文件的意思很简单,f1,f2,f3,f4 四张图片循环播放,每个播放300毫秒,然后在代码里调用
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frame);
image = (ImageView) findViewById(R.id.frame_image);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
image.setBackgroundResource(R.anim.frame);
AnimationDrawable anim = (AnimationDrawable) image.getBackground();
anim.start();
}
}
这边一定要注意,动画的启动不可以放在onCreate里面,因为此时的window还没有创建完毕
好了,安卓动画总结完毕~
- 安卓动画阶段总结
- 安卓动画总结
- 安卓动画总结
- 安卓动画总结
- 安卓动画总结
- 日志管理系统安卓项目阶段总结安卓
- 安卓动画(Animation)总结
- 安卓阶段学习总结(2016-04-26)
- 安卓属性动画的总结
- 安卓属性动画知识点总结
- 安卓Android多阶段进度条progress bar附带动画效果
- iOS学习阶段总结-b20120920-多媒体动画
- iOS学习阶段总结-b20120920-动画转场
- 安卓动画知识总结 Animation AnimationSet LayoutAnimation
- Android开发之安卓属性动画大总结
- 安卓 动画帧动画基础动画
- 安卓开发-动画
- 安卓 动画效果
- JDK7中的HashMap
- BZOJ——2565最长双回文串
- 数据库连接报ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
- 最实用的 Linux 命令行使用技巧
- Vertx简介
- 安卓动画阶段总结
- 1004. 成绩排名 (20)
- yii 框架分页
- 25、数据加密-RSA
- android studio 快捷键
- STM8S103之外部中断
- 图文混排
- QT控件样式设计,Qt中漂亮的几款QSS
- Python学习(十二)——pandas函数库1