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

android动画Animation

2016-08-05 18:31 288 查看
首先我们要知道一共有哪几种动画,这个面试有可能被问哦^_^。

变换动画(透明度、缩放、平移、旋转)、逐帧动画、布局动画和属性动画

一、变换动画

我们可以通过XML文件设置动画也可以通过java代码设置动画,当动画的状态是动态获取的,就是比较灵活的时候,我们选java代码的,否则选择XML的更加方便。

1、透明度

1)通过xml文件设置

alphademo.xml(在res/anim文件夹下)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!---透明度的取值时0.0(完全透明)~1.0(完全不透明)->
<alpha
//动画持续的时间,毫秒为单位
android:duration="2000"
//动画开始时的透明度,浮点型
android:toAlpha="1.0"
//动画结束时的透明度
android:fromAlpha="0.1"
/>
</set>


如何启动动画呢?

Animation alphaAnimation = AnimationUtils.loadAnimation(this,R.anim.alphademo);
imageView.startAnimation(alphaAnimation);


2)通过java代码实现

//参数一:动画开始时透明度,参数二:动画结束时透明度
AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f,0.8f);
//动画持续的时间
alphaAnimation.setDuration(3000);
//开启动画
imageView.startAnimation(alphaAnimation);


2、缩放动画

1)xml加载动画

scaledemo.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
//动画持续的时间
android:duration="2000"
//X开始的大小(小于1表示缩小,大于1表示拉伸)
android:fromXScale="0.2"
//X结束时大小
android:toXScale="1.2"
//Y从多大开始
android:fromYScale="0.0"
//Y到多大结束
android:toYScale="1"
//以X轴的那点开始缩放
android:pivotX="50%"
//以Y轴的那点为基准点开始缩放
android:pivotY="50%"
//动画插入器
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
//动画结束后是否被应用
android:fillAfter="true"
/>

</set>


开启动画

Animation animation = AnimationUtils.loadAnimation(this,R.anim.scaledemo);
imageView.startAnimation(animation);


2)java代码开启动画

//创建缩放动画的对象,参数一:X开始的大小,参数二:X结束时大小,参数三:Y开始大小,参数四:Y结束大小
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f,2.0f,0.0f,1.0f);
//设置动画持续的时间,毫秒为单位
scaleAnimation.setDuration(2000);
//动画结束后是否被应用
scaleAnimation.setFillAfter(true);
//开启动画
imageView.startAnimation(scaleAnimation);


3、平移动画

1)xml文件启动动画

translatedemo.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<translate
android:duration="2000"
android:fromXDelta="10"
android:toXDelta="200"
android:fromYDelta="0"
android:toYDelta="0"
android:fillAfter="false"
/>
</set>


几乎都是差不多的

Animation animation = AnimationUtils.loadAnimation(this,R.anim.translatedemo);
imageView.startAnimation(animation);


2)java代码实现

TranslateAnimation translateAnimation = new TranslateAnimation(10.0f,200f,30f,210f);
translateAnimation.setDuration(2000);
translateAnimation.setFillAfter(true);
imageView.startAnimation(translateAnimation);


4、旋转动画

1)xml文件实现

rotatedemo.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="2000"
android:fromDegrees="45"
android:toDegrees="60"
android:fillAfter="true"/>
<!--fromDegrees 属性为动画起始时物件的角度
toDegrees   属性为动画结束时物件旋转的角度 可以大于360度

说明:
当角度为负数——表示逆时针旋转
当角度为正数——表示顺时针旋转
(负数from——to正数:顺时针旋转)
(负数from——to负数:逆时针旋转)
(正数from——to正数:顺时针旋转)
(正数from——to负数:逆时针旋转)

pivotX     属性为动画相对于物件的X坐标的开始位置
pivotY     属性为动画相对于物件的Y坐标的开始位置

说明:        以上两个属性值 从0%-100%中取值
50%为物件的X或Y方向坐标上的中点位置
-->
</set>


启动的代码还是一样的

Animation animation = AnimationUtils.loadAnimation(this,R.anim.rotatedemo);
imageView.startAnimation(animation);


2)java代码实现

RotateAnimation rotateAnimation = new RotateAnimation(0f,-30f);
rotateAnimation.setDuration(2000);
rotateAnimation.setFillAfter(true);
imageView.startAnimation(rotateAnimation);


5、每种动画都会了,来一个组合的吧

RotateAnimation rotateAnimation = new RotateAnimation(0f, -30f,0.5f,0.5f);
rotateAnimation.setDuration(2000);
rotateAnimation.setFillAfter(true);
//设置启动的时间
//        rotateAnimation.setStartOffset(2000);
imageView.startAnimation(rotateAnimation);
rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
//动画开始的时候执行
@Override
public void onAnimationStart(Animation animation) {

}
//动画结束后执行
@Override
public void onAnimationEnd(Animation animation) {
TranslateAnimation translateAnimation = new TranslateAnimation(0f, 200f, 0f, 50f);
translateAnimation.setFillAfter(true);
translateAnimation.setDuration(3000);
imageView.startAnimation(translateAnimation);
}
//动画重复的时候执行
@Override
public void onAnimationRepeat(Animation animation) {

}
});


参考的



6、我们可能会有这样的需求:多个动画同时执行

1)xml文件方式

togeranimation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<translate
android:duration="2000"
android:fromXDelta="0.0"
android:toXDelta="200.0"
android:fromYDelta="0.0"
android:toYDelta="30.0"
android:fillAfter="true"
/>
<rotate
android:duration="3000"
android:fromDegrees="0.0"
android:toDegrees="30.0"
android:fillAfter="true"
/>

</set>


启动的方式还是一样的

Animation animation = AnimationUtils.loadAnimation(this,R.anim.togeranimation);
imageView.startAnimation(animation);


2)使用java代码有些不同

AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation = new TranslateAnimation(0f, 200f, 0f, 50f);
translateAnimation.setDuration(3000);
//一定要把动画添加到动画集中
animationSet.addAnimation(translateAnimation);

RotateAnimation rotateAnimation = new RotateAnimation(0f, -30f,0.5f,0.5f);
rotateAnimation.setDuration(2000);
animationSet.addAnimation(rotateAnimation);
//启动方式一样,只不过传入的是AnimationSet对象了
imageView.startAnimation(animationSet);


二、帧动画

与上面那种动画不同的是,xml文件放到了drawable文件夹下

animation.xml

注意哦这里的根元素是animation-list ,子元素是item

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@mipmap/loading1"
android:duration="200" />
<item
android:drawable="@mipmap/loading2"
android:duration="200" />
<item
android:drawable="@mipmap/loading3"
android:duration="200" />
<item
android:drawable="@mipmap/loading4"
android:duration="200" />
<item
android:drawable="@mipmap/loading5"
android:duration="200" />
<item
android:drawable="@mipmap/loading6"
android:duration="200" />
<item
android:drawable="@mipmap/icon"
android:duration="200" />

</animation-list>


接下来就是java代码了

public class Animationtoger extends AppCompatActivity {

private AnimationDrawable animationDrawable = null;
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animationtoger);
imageView = (ImageView) findViewById(image_view);
//初始化AnimationDrawable
animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.animation);
//将imageView的背景设置为AnimationDrawable
imageView.setBackground(animationDrawable);

}
public void startAnimation(View view){
//开启动画,停止用stop
animationDrawable.start();
}
}


三、布局动画

我这里面实现的效果是,ListView中每个条目一次从右滑出并可见。

布局动画使用的是LayoutAnimationController,它是用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置动画效果。

public class LayoutAnimationActivity extends AppCompatActivity {

private ListView listView;
private List<String> list;
private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout_animation);
listView = (ListView) findViewById(R.id.listview);

list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
list.add("你好黑啊+" + i);
}
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
AnimationSet set = new AnimationSet(true);

Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(2000);
set.addAnimation(animation);

animation = new TranslateAnimation(100f,10f,0,0);
animation.setDuration(2000);
set.addAnimation(animation);
//得到布局动画控制器
LayoutAnimationController layoutAnimationController = new LayoutAnimationController(set);
//设置控件显示的顺序
layoutAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
listView.setLayoutAnimation(layoutAnimationController);
//写不写都可以,不写也会出现同样的效果
listView.startLayoutAnimation();

}
}


四、属性动画

http://blog.csdn.net/lmj623565791/article/details/38067475

http://blog.csdn.net/guolin_blog/article/details/43536355
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: