安卓三大动画分别为view animation,drawable animation,property animation。下面分别介绍。
view animation(补间动画),通过改变view的透明度,位置,旋转,缩放。。来实现的动画效果,但实质不会改变view的属性,也就是说界面上view的位置的移动并不代表,当前view的位置在那里。下面介绍alpha透明度 rotate旋转 scale缩放 translate位移的里面的属性。
alpha---->fromAlpha初始透明度([0,1]) toAlpha目标透明度([0,1])。1代表原始,0代表消失。其他值相信你懂得
rotate---->fromDegrees初始角度(只要写度数值就可以了,是顺时针) toDegrees目标角度 pivotX旋转的中心点X坐标值(这里的单位有三种,直接写数值代表坐标点,写?%代表当前view的相对位置,比如50%,50%代表view的中心点,?%p代表view的父容器相对位置,推荐些%) pivotY旋转的中心点Y坐标值
scale---->fromXScale初始宽度比例值(1代表原始) toXScale目标宽度比例值 fromYScale初始高度比例值 toYScale目标高度比例值 pivotX pivotY (和上面一样)
translate---->在介绍这个之前,我们需要了解一个知识。当确定了长方形的左上角和右下角后,可以确定这个长方形,这里的view就相当于放在这么一个长方形中。fromXDelta起始X坐标 toXDelta目标X坐标 fromYDelta起始Y坐标 toYDelta目标Y坐标,取值也是三种情况。另外在动画里还有几个公共属性,duration动画播放的时间,repeatCount动画重复的次数(在第一次执行完后),repeatMode:restart下一次执行从头开始,reverse下一次执行来回的形式。还有在根节点set里还有fillAfter属性,当设置为true后,动画会保持最终的状态,虽然子标签也有这个属性,但是不推荐在子标签里使用。
静态创建:在res下新建anim文件夹,在里面新建xml文件,跟标签是set,如:
因为补间动画是对一个view进行动画效果,所以这里我使用默认的TextView,然后在布局里加了一些Button设置了监听,
最后我们在button监听中,
这里第二个参数的就是你想引用的anim对应布局,设置好后,然后让view去启动view。这里除了startAnimation()还有setAnimation(),不过暂时使用不到setAnimation()。这个使用的是设置对象,但是并不代表立即启动动画,如果期间设置了setStartTime()就会延迟动画,但事实动画已经开始,只是没有播放。注意区别。只是单一的效果是不是太单调啦?淡然,这里的动画不会这么简单。后面介绍一个简单的动画组合
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"
/>
<translate
android:fromYDelta="0%"
android:toYDelta="400%"
android:duration="3000"
/>
<alpha
android:startOffset="1000"
android:fromAlpha="1"
android:toAlpha="0.5"
android:duration="2000"
android:repeatCount="1"
android:repeatMode="reverse"
/>
<rotate
android:startOffset="6000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="450%"
android:duration="2000"
/>
<translate
android:startOffset="6000"
android:fromXDelta="0%"
android:toXDelta="100%"
android:duration="2000"
/>
<!-- 此时里面的repeat再次调用会再次等待8秒才会执行 -->
<scale
android:startOffset="8000"
android:fromXScale="1"
android:toXScale="2"
android:fromYScale="1"
android:toYScale="2"
android:pivotX="150%"
android:pivotY="450%"
android:duration="1000"
android:repeatCount="3"
/>
</set></span>
<span style="font-size:14px;">Animation animation = AnimationUtils.loadAnimation(this, R.anim.test);
tv1.startAnimation(animation);</span>
动画默认都是同时播放的,这里设置先后顺序就计算播放时间,然后通过startOffset属性进行延迟播放。后面的动画若设置的重复,也会执行前面的等待时间。代码上已有注释。
期间会有旋转加位移+透明度+旋转加位移+缩放的动画效果(不知道怎么引入动画,效果不能展示,)
前面了解了静态创建,下面我们来了解动态代码创建吧。
动态创建:比较简单,下面代码附上,
public class MainActivity extends Activity implements OnClickListener{
private Animation animation;
//找到控件,因为这里是对view操作 ,必须要有view对象
private TextView tv1;
private Button bt1,bt2,bt3,bt4,bt5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//findViews
tv1 = (TextView) findViewById(R.id.tv1);
bt1 = (Button) findViewById(R.id.bt1);
bt2 = (Button) findViewById(R.id.bt2);
bt3 = (Button) findViewById(R.id.bt3);
bt4 = (Button) findViewById(R.id.bt4);
bt5 = (Button) findViewById(R.id.bt5);
//监听
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this);
bt5.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt1:
animation = new AlphaAnimation(1.0f, 0.1f);
break;
case R.id.bt2:
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1);
break;
case R.id.bt3:
animation = new ScaleAnimation(1, 2, 1, 2,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
break;
case R.id.bt4:
animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
break;
case R.id.bt5:
AnimationSet set = new AnimationSet(false);
Animation animation2 = new AlphaAnimation(1.0f, 0.1f);
animation2.setDuration(2000);
set.addAnimation(animation2);
Animation animation3 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1);
animation3.setStartOffset(2000);
set.addAnimation(animation3);
//设置启动时间,这样后面的setsetAnimation()就会延迟时间显示,但事实动画已经开始了
set.setStartTime(3000);
tv1.setAnimation(set);
break;
default:
break;
}
animation.setRepeatCount(2);
animation.setRepeatMode(Animation.REVERSE);
animation.setFillAfter(true);
animation.setDuration(2000);
tv1.startAnimation(animation);
// animation.setStartTime(2000);
// tv1.setAnimation(animation);
}
}
可以发现,当你想要什么效果,就可以创建对面的动画对象,然后对里面参数进行赋值,这里提到一点,用到%号的尽量写带type的方法。Animation.RELATIVE_TO_SELF是常量就是%号,然后还有一点个人认为比较重要的,当你使用了%号时,记得这里写的值是整数,0.5就代表50%了,千万别放大了太多,后来找不到view还以后没效果,其他的一些参数动态代码也都是有的。