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

Android属性动画一

2016-02-20 16:16 477 查看

1.传统动画的局限性

如下布局:


<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img"
android:onClick="click"
/>
<Button
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="move"
android:onClick="move"/>


希望点击image弹出toast,点击button移动image

public void click(View view){
Toast.makeText(this,"img",Toast.LENGTH_SHORT).show();
}
public void move(View view){
ImageView img = (ImageView) findViewById(R.id.img);
TranslateAnimation animation = new TranslateAnimation(0,200,0,0);
animation.setDuration(1000);
animation.setFillAfter(true);//动画结束后停留
img.startAnimation(animation);
}


于是这样:


现在问题来了,动画的确实现了,但是现在点击图片发现没有反应,而点击原来的位置弹出Toast,这是因为传统动画只适合做动画,不适合做与用户交互的动画,这就用到属性动画。

2.ObjectAnimator

对于上面的动画,只需一句代码就好:

ObjectAnimator.ofFloat(img,"translationX",0f,200f).setDuration(1000).start();


并且点击动画有Toast

3.动画集

可以看到ofFloat方法的第二个参数,下面还可以这样:

ObjectAnimator.ofFloat(img,"translationX",0f,200f).setDuration(1000).start();
ObjectAnimator.ofFloat(img,"translationY",0f,200f).setDuration(1000).start();
ObjectAnimator.ofFloat(img,"rotation",0f,200f).setDuration(1000).start();//旋转


结果:


没错,他们是同时进行的,只花了一秒钟。

注意:为什么是歪的呢?因为旋转的参数只设置了200度,没到360.

当然,还可以用 PropertyValuesHolder 实现:

PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("translationX",0f,200f);
PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationY",0f,200f);
PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("rotation",0f,200f);
ObjectAnimator.ofPropertyValuesHolder(img,p1,p2,p3).setDuration(1000).start();


效果都是一样的。

不过,还可以像以前一样放在动画集中:

ObjectAnimator a1 =  ObjectAnimator.ofFloat(img,"translationX",0f,200f);
ObjectAnimator a2 =  ObjectAnimator.ofFloat(img,"translationY",0f,200f);
ObjectAnimator a3 =  ObjectAnimator.ofFloat(img,"rotation",0f,200f);
AnimatorSet set = new AnimatorSet();
set.playTogether(a1,a2,a3);
set.setDuration(1000);
set.start();


既然有set.playTogether()方法,就还有其它方法了:

set.playSequentially(a1,a2,a3);


当然还有:

set.play(a1).with(a2);
set.play(a3).after(a2);
set.play(a1).before(a3);


4.动画监听事件

看代码:

ObjectAnimator a = ObjectAnimator.ofFloat(view,"alpha",0f,1f);//透明度变化
a.setDuration(1000);
a.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {

}

@Override
public void onAnimationEnd(Animator animation) {
Toast.makeText(MainActivity.this,"yes",Toast.LENGTH_SHORT).show();
}

@Override
public void onAnimationCancel(Animator animation) {

}

@Override
public void onAnimationRepeat(Animator animation) {

}
});
a.start();


一般用的比较多的是End方法,所以可以单独拿出来:

a.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Toast.makeText(MainActivity.this,"yes",Toast.LENGTH_SHORT).show();
}
});


5.总结

简单的了解了一下属性动画,属性动画可以改变动画的属性,不仅仅是改变位置,下次将会带来一个小的菜单应用。

源代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: