动画分类:
ViewAnimation
补间动画 Tween Animation
帧动画 Frame Animation
PropertyAnimation(3.0)
补间动画,帧动画,属性动画
1.补间动画Tween
alpha rotate scale translate
2.动画监听
AnimationListener
3.帧动画
Frame
首先给大家讲一下补间动画:
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.zking.administrator.g160628_android17_animation.MainActivity">
<ImageView
android:layout_width="300dp"
android:layout_height="200dp"
android:src="@drawable/heart"
android:id="@+id/iv_main_image"
/>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="alpha"
android:onClick="operation"
android:id="@+id/btn_main_alpha"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="rotate"
android:onClick="operation"
android:id="@+id/btn_main_rotate"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="scale"
android:onClick="operation"
android:id="@+id/btn_main_scale"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="translate"
android:onClick="operation"
android:id="@+id/btn_main_translate"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="all"
android:onClick="operation"
android:id="@+id/btn_main_all"
/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Activity文件
package com.zking.administrator.g160628_android17_animation;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ImageView iv_main_image;
private Animation animation;
private Animation animation2;
private Animation animation1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_main_image = (ImageView) findViewById(R.id.iv_main_image);
iv_main_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "点击了图片", Toast.LENGTH_SHORT).show();
}
});
}
public void operation(View view){
switch (view.getId()) {
case R.id.btn_main_alpha:
//加载动画
animation = AnimationUtils.loadAnimation(this, R.anim.iv_alpha);
break;
case R.id.btn_main_rotate:
//加载动画
animation=AnimationUtils.loadAnimation(this,R.anim.iv_rotate);
break;
case R.id.btn_main_scale:
//加载动画
//缩动画
animation1 = AnimationUtils.loadAnimation(this, R.anim.iv_scale);
//放动画
animation2 = AnimationUtils.loadAnimation(this, R.anim.iv_scale2);
//给动画设置监听
animation1.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
iv_main_image.startAnimation(animation2);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
animation2.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
iv_main_image.startAnimation(animation1);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
//给图片开启动画
iv_main_image.startAnimation(animation1);
return;
case R.id.btn_main_translate:
//加载动画
//animation=AnimationUtils.loadAnimation(this,R.anim.iv_translate);
//使用属性动画实现图片的移动
ObjectAnimator.ofFloat(iv_main_image,"translationX",0,200).setDuration(2000).start();
ObjectAnimator.ofFloat(iv_main_image,"translationY",0,200).setDuration(2000).start();
break;
case R.id.btn_main_all:
animation=AnimationUtils.loadAnimation(this,R.anim.iv_all);
break;
}
// iv_main_image.startAnimation(animation);
}
}
再给大家讲一下桢动画:
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.zking.administrator.g160628_android17_animation.FrameActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv_frame_image"
android:background="@drawable/iv_frame"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"
android:onClick="start"
/>
</LinearLayout>
Activity文件:
package com.zking.administrator.g160628_android17_animation;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class FrameActivity extends AppCompatActivity {
private ImageView iv_frame_image;
private AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frame);
iv_frame_image = (ImageView) findViewById(R.id.iv_frame_image);
animationDrawable = (AnimationDrawable) iv_frame_image.getBackground();
}
public void start(View view){
animationDrawable.start();
}
}