android 动画效果,android动画
动画资源
一、分类:
(一)、概要:
3.0以前,android支持两种动画模式,补间动画(tween animation),帧动画(frame animation),在android3.0中又引入了一个新的动画系统:属性动画(property animation)。
这三种动画模式在SDK中被称为view animation,drawable animation,property animation。
(二)、动画资源分类:二、补间动画: View Animation就是一系列View形状的变换,如大小的缩放、透明度的改变、水平位置的改变、旋转位置改变,动画的定义既可以用java代码定义也可以用XML定义。建议用XML定义。 用XML定义的动画放在/res/anim/文件夹内,XML文件的根元素为<set> , 二级节点可为<alpha>,<scale>,<translate>,<rotate>。 (一)、用xml资源实现补间动画:
publicclass MainActivity extends Activity {(二)、用java代码实现补间动画:
private ImageView imageView_main;
private Animation animation = null;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView_main = (ImageView) findViewById(R.id.imageView_main);
}
publicvoid clickButton(View view) {
switch (view.getId()) {
case R.id.button_main_alpha:
animation = new AlphaAnimation(0.0f, 1.0f);
break;
case R.id.button_main_scale:
animation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 1.0f);
break;
case R.id.button_main_translate:
animation = new TranslateAnimation(0, 150, 0, 0);
break;
case R.id.button_main_rotate:
animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
break;
default:
break;
}
animation.setDuration(3000);
imageView_main.setAnimation(animation);
}
}
publicclass MainActivity extends Activity {三、帧动画: Frame Animation(AnimationDrawable对象):帧动画,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果。 必须以<animation-list>为根元素,以<item>表示要轮换显示的图片,duration属性表示各项显示的时间。XML文件要放在/res/anim/或者/res/animator目录下。 (一)、实例代码:
private ImageView imageView_main;
private Animation animation = null;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView_main = (ImageView) findViewById(R.id.imageView_main);
}
publicvoid clickButton(View view) {
switch (view.getId()) {
case R.id.button_main_alpha:
animation = new AlphaAnimation(0.0f, 1.0f);
break;
case R.id.button_main_scale:
animation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 1.0f);
break;
case R.id.button_main_translate:
animation = new TranslateAnimation(0, 150, 0, 0);
break;
case R.id.button_main_rotate:
animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
break;
default:
break;
}
animation.setDuration(3000);
imageView_main.setAnimation(animation);
}
}
一、res/anim/frame_animation.xml的代码:
<animation-listxmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<itemandroid:drawable="@drawable/anim1"android:duration="50"/>
<itemandroid:drawable="@drawable/anim2"android:duration="50"/>
<itemandroid:drawable="@drawable/anim3"android:duration="50"/>
<itemandroid:drawable="@drawable/anim4"android:duration="50"/>
<itemandroid:drawable="@drawable/anim5"android:duration="50"/>
<itemandroid:drawable="@drawable/anim6"android:duration="50"/>
<itemandroid:drawable="@drawable/anim7"android:duration="50"/>
<itemandroid:drawable="@drawable/anim8"android:duration="50"/>
<itemandroid:drawable="@drawable/anim9"android:duration="50"/>
<itemandroid:drawable="@drawable/anim10"android:duration="50"/>
<itemandroid:drawable="@drawable/anim11"android:duration="50"/>
<itemandroid:drawable="@drawable/anim12"android:duration="50"/>
</animation-list>
二、MainActivity.java代码:
public class MainActivity extends Activity {
private Im