Android动画主要分为两类,一种Tween动画,可以实现图片的渐变动画(Alpha),尺寸变化动画(Scale),位置变化(Translate),Roate(旋转动画),也可以将上述动画组合起来。
主要用到的类有:
Animation:动画抽象类,其他几个实现类继承该类
AlpaAnimation:控制透明度变化的动画类
ScaleAnimation: 控制尺寸变化的动画类
RoateAnimation : 控制旋转的动画类
TranslateAnimation:控制位置变化的动画类
AnimationSet:定义动画属性集合类
AnimationUtils:动画工具类
Tween又分为两种一种代码编程,另一种XML配置文件
代码编程:
package com.example.mytween;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
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.ViewSwitcher;
public class MainActivity extends Activity {
private Button btn1,btn2,btn3,btn4,btn5;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView) findViewById(R.id.imageView1);
}
public void onClick(View v){
switch (v.getId()) {
//透明
case R.id.button1:
AlphaAnimation alpha=new AlphaAnimation(0.1f, 1f);
alpha.setDuration(2000);
alpha.setRepeatCount(5);
img.startAnimation(alpha);
break;
//旋转
case R.id.button2:
RotateAnimation rotate=new RotateAnimation(0,180);
rotate.setDuration(5000);
rotate.setRepeatCount(3);
img.startAnimation(rotate);
break;
// 移动
case R.id.button3:
TranslateAnimation trans=new TranslateAnimation(0, 100,0,100);
trans.setDuration(3000);
trans.setRepeatCount(3);
img.startAnimation(trans);
break;
//缩放
case R.id.button4:
ScaleAnimation scale=new ScaleAnimation(0.1f,1.5f,0.1f,1.5f);
scale.setDuration(5000);
scale.setRepeatCount(3);
img.startAnimation(scale);
break;
//组合
case R.id.button5:
AnimationSet set=new AnimationSet(true);
AlphaAnimation alpha1=new AlphaAnimation(0.1f, 1f);
alpha1.setDuration(2000);
alpha1.setRepeatCount(5);
set.addAnimation(alpha1);
RotateAnimation rotate1=new RotateAnimation(0,180);
rotate1.setDuration(5000);
rotate1.setRepeatCount(3);
set.addAnimation(rotate1);
TranslateAnimation trans1=new TranslateAnimation(0, 100,0,100);
trans1.setDuration(3000);
trans1.setRepeatCount(3);
set.addAnimation(trans1);
ScaleAnimation scale1=new ScaleAnimation(0.1f,1.5f,0.1f,1.5f);
scale1.setDuration(5000);
scale1.setRepeatCount(3);
set.addAnimation(scale1);
img.startAnimation(set);
break;
default:
break;
}
}
}
R.layout.activity_main
R.layout.activity_main文件中只有一些一个ImagView和几个Button键
另一种是用XML实现,首先在res下建立个anim文件夹,建立alpa,roate,translate,scale,set等xml文件
alpha
<?xml version="1.0" encoding="utf-8"?>
<alpha android:fromAlpha="1.0"
android:toAlpha="0.3"
android:duration="3000"
android:repeatCount="3"
xmlns:android="http://schemas.android.com/apk/res/android">
</alpha>
<?xml version="1.0" encoding="utf-8"?>
<rotate
android:fromDegrees="45"
android:toDegrees="720"
android:pivotX="50%"
android:pivotY="50%"
android:duration="4000"
android:repeatCount="2"
xmlns:android="http://schemas.android.com/apk/res/android">
</rotate>
<?xml version="1.0" encoding="utf-8"?>
<scale
android:fromXScale="0"
android:toXScale="6"
android:fromYScale="0"
android:toYScale="6"
android:repeatCount="3"
android:duration="6000"
xmlns:android="http://schemas.android.com/apk/res/android">
</scale>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="1.0"
android:toAlpha="0.3"
android:duration="3000"
android:repeatCount="3"
>
</alpha>
<rotate
android:fromDegrees="45"
android:toDegrees="720"
android:pivotX="50%"
android:pivotY="50%"
android:duration="4000"
android:repeatCount="2"
>
</rotate>
<scale
android:fromXScale="0"
android:toXScale="6"
android:fromYScale="0"
android:toYScale="6"
android:repeatCount="3"
android:duration="6000"
></scale>
<translate
android:fromXDelta="0"
android:toXDelta="200"
android:fromYDelta="0"
android:toYDelta="200"
android:duration="6000"
android:repeatCount="3"
>
</translate>
</set>
<?xml version="1.0" encoding="utf-8"?>
<translate
android:fromXDelta="0"
android:toXDelta="200"
android:fromYDelta="0"
android:toYDelta="200"
android:duration="6000"
android:repeatCount="3"
xmlns:android="http://schemas.android.com/apk/res/android">
</translate>
这一个文件,下面在写Java文件
package com.example.mytweenxml;
import android.os.Bundle;
import android.app.Activity;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private Button btn1,btn2,btn3,btn4,btn5;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView) findViewById(R.id.imageView1);
}
public void onClick(View v){
switch (v.getId()) {
//透明
case R.id.button1:
AlphaAnimation alpha=(AlphaAnimation) AnimationUtils.loadAnimation(this, R.anim.alpha);
img.startAnimation(alpha);
break;
//旋转
case R.id.button2:
RotateAnimation rotate=(RotateAnimation) AnimationUtils.loadAnimation(this, R.anim.rotate);
img.startAnimation(rotate);
break;
//
case R.id.button3:
TranslateAnimation trans=(TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.translate);
img.startAnimation(trans);
break;
//缩放
case R.id.button4:
ScaleAnimation scale=(ScaleAnimation) AnimationUtils.loadAnimation(this, R.anim.scale);
img.startAnimation(scale);
break;
//组合
case R.id.button5:
AnimationSet set=(AnimationSet) AnimationUtils.loadAnimation(this, R.anim.set);
img.startAnimation(set);
break;
default:
break;
}
}
}
另一种动画是Frame动画,它是将图片一张一张播放,产生动画效果,
第一步,在res 下建立个文件夹,并且建立个XML文件,用来设置图片资源
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/heshang1"
android:duration="2000"></item>
<item android:drawable="@drawable/heshang2"
android:duration="2000"></item>
<item android:drawable="@drawable/heshang3"
android:duration="2000"></item>
<item android:drawable="@drawable/heshang4"
android:duration="2000"></item>
<item android:drawable="@drawable/heshang1"
android:duration="2000"></item>
</animation-list>
java 代码:
package com.example.myframe;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView img;
private AnimationDrawable drawable;
private Button btn1,btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.framelayout);
img=(ImageView) findViewById(R.id.frame_imageView1);
btn1=(Button) findViewById(R.id.frame_button1);
btn2=(Button) findViewById(R.id.frame_button2);
drawable=(AnimationDrawable) img.getDrawable();
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
drawable.start();
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
drawable.stop();
}
});
}
}
framelayout文件中有两个Button键和一个Imageview,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/frame_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始动画" />
<Button
android:id="@+id/frame_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结束动画" />
<ImageView
android:id="@+id/frame_imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.65"
android:src="@drawable/imagestyle"//这一种是重点
/>
</LinearLayout>
如果你framelayout中Imageview 写成 android:background="@drawable/imagestyle"
那么Java文件中 drawable=(AnimationDrawable) img.getBackground();
这是相对应的