简介
Android 平台提供了两类动画。 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转、平移、放缩和渐变)。
第二类就是 Frame动画,即顺序的播放事先做好的图像,与gif图片原理类似。
下面就讲一下Tweene Animations。
主要类:
Animation 动画
AlphaAnimation 渐变透明度
RotateAnimation 画面旋转
ScaleAnimation 渐变尺寸缩放
TranslateAnimation 位置移动
AnimationSet 动画集
效果实现(位移,透明度,缩放,旋转)
1、直接将效果以代码的形式写在activity中
public class MainActivity extends AppCompatActivity {
private Button mButton;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button);
mImageView = (ImageView) findViewById(R.id.imageview);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);//设置透明度从0-1
TranslateAnimation animation = new TranslateAnimation( 0,mImageView.getMeasuredWidth(), 0, 0);//设置位移,从0,view的宽度到0,0
RotateAnimation animation1 = new RotateAnimation(0,360,mImageView.getMeasuredWidth()/2,mImageView.getMeasuredHeight()/2);//以某点旋转360度
ScaleAnimation animation2 = new ScaleAnimation(0.5f,1,0.5f,1);//设置放大
// ScaleAnimation animation2 = new ScaleAnimation(1,0.5f,1,0.5f);//设置缩小
alphaAnimation.setDuration(3000);//持续时间
animation.setDuration(3000);
animation1.setDuration(3000);
animation2.setDuration(3000);
mImageView.startAnimation(alphaAnimation);//开始动画
mImageView.startAnimation(animation);
mImageView.startAnimation(animation1);
mImageView.startAnimation(set);
//怎样将下面的几种动画效果同时实现呢?
AnimationSet set = new AnimationSet(false);
/*然后将上面的 mImageView.startAnimation(alphaAnimation);
mImageView.startAnimation(animation);
mImageView.startAnimation(animation1);
mImageView.startAnimation(set);*/
// 改为:
set.addAnimation(alphaAnimation);
set.addAnimation(animation);
set.addAnimation(animation1);
set.addAnimation(animation2);
mImageView.startAnimation(set);
2、将动画效果写在xml文件中
步骤:
《1》写animation,在res下新建resource file ,将Resource type改为animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/anticipate_overshoot_interpolator">
<rotate android:duration="1000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="3"></rotate>//以中点为旋转点,旋转4次
<scale
android:duration="1000"
android:fromXScale="0.5"
android:fromYScale="0.5"
android:toXScale="1.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="20%"
android:repeatCount="3"></scale>//以中心点放大两倍
</set>
《2》在activity中
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animation_rotate);
mImageView.startAnimation(animation);
上面是在点击事件中实现的,那么,下面是点击图片是实现动画效果
1.写animator,在res下新建file,将Resource type改为animator
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together">//记得加ordering
//实现放大效果
<objectAnimator
android:duration="3000"
android:propertyName="scaleX"
android:valueFrom="0.5"
android:valueTo="1"></objectAnimator>
<objectAnimator
android:duration="3000"
android:propertyName="scaleY"
android:valueFrom="0.5"
android:valueTo="1"></objectAnimator>
</set>
2.在layout下的布局文件中
<ImageView
android:id="@+id/imageview"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="300dp"
android:onClick="startAnimator"//添加这个属性,点击图片是开始动画
android:src="@mipmap/seng" />
3、在activity中
/*新建类,类名startAnimator要与layout中android:onClick="startAnimator"的名称相同*/
public void startAnimator(View view){
// ObjectAnimator.ofFloat(mImageView,"scaleX",0.0f,1f).setDuration(3000).start();//这是没有写animator时直接在这里写动画效果。上面已经写了animator,所以代码如下:
Animator animator = AnimatorInflater.loadAnimator(getApplicationContext(),R.animator.animator_ani);
animator.setTarget(mImageView);
animator.start();
}
效果如下: