动画(逐帧动画,补间动画,属性动画 )

FrameAnimation(DrawableAnimation):逐帧动画

动画(逐帧动画,补间动画,属性动画 )_第1张图片
image.png

动画(逐帧动画,补间动画,属性动画 )_第2张图片
image.png

首先在布局定义------------------------------
···
xmlns:tools=" http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >

···
在res 下的anim 文件夹中 定义xml
···
xmlns:android="http://schemas.androi
d.com/apk/res/android"
android:oneshot="true">




···
*************Mactivtiy****************
···
package com.example.yframanimation;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class MainActivity extends Activity {

private ImageView img;
private AnimationDrawable ad;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   img = (ImageView) findViewById(R.id.iv_img);
  //获取动画的图片
   img.setBackgroundResource(R.anim.donghua);
   //给ImageView 设置背景图片
   ad = (AnimationDrawable) img.getBackground();
  
   findViewById(R.id.bt_1).setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View v) {
          ad.start();//播放
        // ad.stop();//暂停
         
    }
});
findViewById(R.id.bt_2).setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View v) {
        ad.stop();//暂停
    }
});



}

}

···



      补间动画
动画(逐帧动画,补间动画,属性动画 )_第3张图片
image.png

动画(逐帧动画,补间动画,属性动画 )_第4张图片
image.png

AlphaAnimation : 透明动画


image.png

ScaleAnimation :缩放动画
动画(逐帧动画,补间动画,属性动画 )_第5张图片
image.png

TranslateAnimation: 位移动画


动画(逐帧动画,补间动画,属性动画 )_第6张图片
image.png

动画(逐帧动画,补间动画,属性动画 )_第7张图片
image.png

RoatateAnimation:旋转动画
动画(逐帧动画,补间动画,属性动画 )_第8张图片
image.png

AnimationSet :动画集合
动画(逐帧动画,补间动画,属性动画 )_第9张图片
image.png

xml
···
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >

···
设置动画的两种方式一种是纯代码
一种是xml( res 下创建anim 里面有透明,缩放,位移,旋转,动画集合 )
透明xml
···

android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000"
xmlns:android="http://schemas.android.com/apk/res/android">

···
缩放xml
···

android:fromXScale="0.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="0.5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000"
android:repeatCount="2"
xmlns:android="http://schemas.android.com/apk/res/android">

···
旋转xml
···

android:fromDegrees="0"
android:toDegrees="90"
android:duration="2000"
android:pivotX="50%"
android:pivotY="50%"
xmlns:android="http://schemas.android.com/apk/res/android">

···
位移xml
···

android:fromXDelta="0"
android:toXDelta="200"
android:fromYDelta="0"
android:toYDelta="200"
android:fillAfter="true"
android:duration="2000"
xmlns:android="http://schemas.android.com/apk/res/android">

···
//开启Activity动画


动画(逐帧动画,补间动画,属性动画 )_第10张图片
image.png

动画(逐帧动画,补间动画,属性动画 )_第11张图片
image.png

动画(逐帧动画,补间动画,属性动画 )_第12张图片
image.png

动画(逐帧动画,补间动画,属性动画 )_第13张图片
image.png

***********************Mactivity************
···
package com.example.ytest4_tweenanimation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

private ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   img = (ImageView) findViewById(R.id.img);
    //透明动画
  findViewById(R.id.bt_1).setOnClickListener(new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            //方式一

// //1.创建透明动画对象 0.0--1.0
// AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
// //2.设置动画时间
// alphaAnimation.setDuration(5000);
//
// //3.设置循环次数
// // alphaAnimation.setRepeatCount(1);
//
// //4. 设置循环模式
// //alphaAnimation.setRepeatMode(AlphaAnimation.REVERSE);
// //5.设置动画执行完之后的状态
// alphaAnimation.setFillAfter(true);
// //6. 开启动画
// img.startAnimation(alphaAnimation);
//
//方式二
Animation loadAnimation = new AnimationUtils().loadAnimation(MainActivity.this, R.anim.alpha);
//执行动画
img.startAnimation(loadAnimation);

        }
    });
//位移动画
    findViewById(R.id.bt_2).setOnClickListener(new OnClickListener() {
        
        @Override
        public void onClick(View v) {                                         

// //100f
// TranslateAnimation translateAnimation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 100f);
// translateAnimation.setDuration(3000);
// img.startAnimation(translateAnimation);

            Animation loadAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
            img.startAnimation(loadAnimation);
        }
    });
    //旋转
    findViewById(R.id.bt_3).setOnClickListener(new OnClickListener() {
        
        @Override
        public void onClick(View v) {

// // RotateAnimation rotateAnimation = new RotateAnimation(0, 360);
//
// RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//
// //旋转次数 360--720 两圈
// rotateAnimation.setRepeatCount(10);
//
//
// rotateAnimation.setDuration(2000);
// //旋转后停留的位置
// rotateAnimation.setFillAfter(true);
// img .startAnimation(rotateAnimation);
//

            Animation loadAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
        img.startAnimation(loadAnimation);
        
        }
    });
    //缩放动画
    findViewById(R.id.bt_4).setOnClickListener(new OnClickListener() {
        
        @Override
        public void onClick(View v) {

// //1.创建对象 0.0f :缩小到没有 1.0f:本身大小 2.0本身大小的2倍 以总布局进行缩放
// // ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 2.0f,1.0f,2.0f);
//
// //自身位置缩放
// ScaleAnimation scaleAnimation= new ScaleAnimation
// (1.0f, 2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//
// //2.执行时间
// scaleAnimation.setDuration(2000);
// //停留在动画缩放完后的状态
// scaleAnimation.setFillAfter(true);
// //3.开启动画
// img.startAnimation(scaleAnimation);
//
//方式二
Animation loadAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);
img.startAnimation(loadAnimation);

        }
    });
    //动画集合
    findViewById(R.id.bt_5).setOnClickListener(new OnClickListener() {
        
        @Override
        public void onClick(View v) {

// //1.创建集合动画对象
// AnimationSet animationSet = new AnimationSet(true);
// //2.创建动画
// //2.1创建透明动画对象
// AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f, 1.0f);
// //alphaAnimation.setDuration(2000);
// //旋转动画
// RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
// // rotateAnimation.setDuration(2000);
// //缩放
// ScaleAnimation scaleAnimation = new ScaleAnimation(0.1f, 1.0f, 1.0f, 2.0f);
//
//
// animationSet.addAnimation(alphaAnimation);
// animationSet.addAnimation(rotateAnimation);
// animationSet.addAnimation(scaleAnimation);
// animationSet.setDuration(2000);//集体设置时间
// //开启动画
// img.startAnimation(animationSet);
//

        Animation loadAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.set);  
            img.startAnimation(loadAnimation);
            
        }
    });
    //动画插入器
findViewById(R.id.bt_6).setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View v) {
        //位移                                                                                                                                                    x轴 从0 - 200        y轴不变
        TranslateAnimation translateAnimation = new  TranslateAnimation(0.0f, 0.0f, 0.0f, 200.0f);
        translateAnimation.setDuration(2000);
        //动画插入器
        //translateAnimation.setInterpolator(new AccelerateInterpolator());
    
        //正弦变化次数
        translateAnimation.setInterpolator(new CycleInterpolator(2));
        //减速
        translateAnimation.setInterpolator(new DecelerateInterpolator());
       img.startAnimation(translateAnimation);
    
    
    }
}); 
//开启Activity动画
findViewById(R.id.bt_7).setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View v) {
    startActivity(new Intent(MainActivity.this, HomeActivity.class));   
       //位移切换Activity
    overridePendingTransition(R.anim.enter, R.anim.exit);
    
    }
});
  img.setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this, "点击了", Toast.LENGTH_SHORT).show();
    }
});
}

}

···
属性动画: PropertyAnimation

逐帧动画与属性动画的区别:
逐帧动画的实际图片位置不改变
属性动画改变

动画(逐帧动画,补间动画,属性动画 )_第14张图片
image.png

动画(逐帧动画,补间动画,属性动画 )_第15张图片
image.png

MainActivity********************
···package com.example.ytest5_propertyanimation;

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends Activity {

private ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   img = (ImageView) findViewById(R.id.img);
   //透明
   findViewById(R.id.bt_1).setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View v) {
        //方式一

// ObjectAnimator ofFloat = ObjectAnimator.ofFloat(img, "alpha", 0.0f,0.5f);
// ofFloat.setDuration(2000);
// ofFloat.start();
// 方式二 xml方式
ObjectAnimator loadAnimation = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.object);
loadAnimation.setTarget(img);
loadAnimation.start();
}
});
//位移
findViewById(R.id.bt_2).setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        ObjectAnimator ofFloat = ObjectAnimator.ofFloat(img, "translationX", 0,50,-100,300);            
        ofFloat.setDuration(5000);
        ofFloat.start();
        
    }
});
//旋转
findViewById(R.id.bt_3).setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View v) {
        ObjectAnimator ofFloat = ObjectAnimator.ofFloat(img, "rotation", 0,90,360);         
        ofFloat.setDuration(5000);
        ofFloat.start();
        
    }
});
//缩放
findViewById(R.id.bt_4).setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View v) {
        ObjectAnimator ofFloat = ObjectAnimator.ofFloat(img, "scaleX", 1.0f,2.0f,0.5f);         
        ofFloat.setDuration(5000);
        ofFloat.start();
        //更新的监听
        ofFloat.addUpdateListener(new AnimatorUpdateListener() {
            
            public void onAnimationUpdate(ValueAnimator animation) {
                String string = animation.getAnimatedValue().toString();
            Log.e("TAG", "string " +string);
            
            }
        });
    }
});
//集合
findViewById(R.id.bt_5).setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View v) {
        AnimatorSet animatorSet = new AnimatorSet();
        ObjectAnimator ofFloat = ObjectAnimator.ofFloat(img, "rotation", 0,90,360);         
        ObjectAnimator ofFloat2 = ObjectAnimator.ofFloat(img, "scaleX", 1.0f,2.0f,0.5f);            
        ObjectAnimator ofFloat3 = ObjectAnimator.ofFloat(img, "translationX", 0,50,-100,300);
        animatorSet.setDuration(5000);
        
        //playSequentially 单个执行
        animatorSet.playSequentially(ofFloat,ofFloat2,ofFloat3);
        
        //playTogether 一起执行
        //animatorSet.playTogether(ofFloat,ofFloat2,ofFloat3);
        animatorSet.start();
        
    }
});

}

}

···

你可能感兴趣的:(动画(逐帧动画,补间动画,属性动画 ))