代码中定义动画示例
public class MainActivity extends ListActivity {
private ImageView iv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] array = { "alpha", "trans", "scale", "rotate", "set",//
"只要 fillAfter = true,不管其他怎么设置,都是使用最后一帧",//
"只要 fillAfter = false,不管其他怎么设置,都是使用第一帧(没有进行任何缩放)", };
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>(Arrays.asList(array))));
iv = new ImageView(this);
iv.setImageResource(R.drawable.icon);
getListView().addHeaderView(iv);
}
@Override
protected void onListItemClick(ListView l, View view, int position, long id) {
switch (position) {
case 0:
Toast.makeText(this, "我是图片", Toast.LENGTH_SHORT).show();
break;
case 1:
alpha(iv);
break;
case 2:
trans(iv);
break;
case 3:
scale(iv);
break;
case 4:
rotate(iv);
break;
case 5:
set(iv);
break;
case 6:
scale(iv, new Random().nextBoolean(), new Random().nextBoolean(), true);//最后一帧,即fillBefore的值无效,fillAfter的值有效
break;
case 7:
scale(iv, new Random().nextBoolean(), new Random().nextBoolean(), false);//第一帧(没有进行任何缩放)
break;
}
}
//透明度动画
public void alpha(View view) {
AlphaAnimation aa = new AlphaAnimation(1.0f, 0.1f);//开始、结束时的透明度。1为全不透明,0为全透明
aa.setDuration(2000);//播放时间
aa.setRepeatCount(1);//重复次数,默认为0。【播放次数=重复次数+1】。设为Animation.INFINITE = -1 表示不停止的播放
aa.setRepeatMode(Animation.RESTART);//【REVERSE】倒序重复播放,【RESTART】重新开始执行(默认)
aa.setInterpolator(new AccelerateInterpolator());//加速
view.startAnimation(aa);
}
//位移动画
public void trans(View view) {
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0.5f,//
Animation.RELATIVE_TO_SELF, -1f, Animation.RELATIVE_TO_PARENT, 1f); //fromXType, fromXValue, toXType, toXValue【开始/结束】时【相对谁】的距离
ta.setDuration(1000);
ta.setRepeatCount(Animation.INFINITE);
ta.setRepeatMode(Animation.REVERSE);
ta.setInterpolator(new BounceInterpolator());//动画结束的时候弹起
view.startAnimation(ta);
}
//缩放动画
public void scale(View view) {
ScaleAnimation sa = new ScaleAnimation(0.5f, 2.5f, 0.5f, 1.5f, //【开始/结束时x/y的缩放比例】
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //【x/y缩放时所使用的模式和中心点】
sa.setDuration(2000);
sa.setInterpolator(new AccelerateDecelerateInterpolator());//先加速后减速
view.startAnimation(sa);
}
//旋转动画
public void rotate(View view) {
RotateAnimation ra = new RotateAnimation(0, 360 * 5, //【开始/结束时旋转的角度】
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //【x/y旋转时所使用的模式和中心点】
ra.setDuration(1000);
ra.setInterpolator(new LinearInterpolator());//匀速
view.startAnimation(ra);
}
//组合动画
public void set(View view) {
AnimationSet set = new AnimationSet(true);//是否使用共同的插值器
//位移
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, //
Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
ta.setDuration(500);
//缩放
ScaleAnimation sa = new ScaleAnimation(1f, 2f, 1f, 1.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
sa.setDuration(500);
sa.setStartOffset(1000);//延迟时间 When this Animation should start, in milliseconds from the start time of the root AnimationSet
//旋转
RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
ra.setDuration(500);
ra.setStartOffset(2000);
//将上面这些动画放到集合中
set.addAnimation(ta);
set.addAnimation(sa);
set.addAnimation(ra);
// set.setFillEnabled(true);
set.setFillAfter(true);
// set.setFillBefore(false);
view.startAnimation(set);
}
public void scale(View view, boolean fillEnable, boolean fillBefore, boolean fillAfter) {
ScaleAnimation sa = new ScaleAnimation(0.2f, 3f, 0.2f, 3f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
sa.setDuration(200);
sa.setFillEnabled(fillEnable);
sa.setFillBefore(fillBefore);
sa.setFillAfter(fillAfter);
view.startAnimation(sa);
}
}
XML中定义动画示例
public class SecondActivity extends ListActivity {
int[] array_id = { R.anim.alpha_in, R.anim.disappear_bottom_right_in, R.anim.disappear_bottom_right_out, R.anim.disappear_top_left_in,
R.anim.disappear_top_left_out, R.anim.drawroll_ani_in, R.anim.drawroll_ani_out, R.anim.fade_in, R.anim.fade_out,
R.anim.flip_horizontal_in, R.anim.flip_horizontal_out, R.anim.flip_vertical_in, R.anim.flip_vertical_out, R.anim.gallery_in, R.anim.grow_from_top,
R.anim.left_in, R.anim.left_out, R.anim.mi_laucher_alpha, R.anim.mi_laucher_del_done, R.anim.mi_laucher_del_down, R.anim.mi_laucher_out,
R.anim.mi_laucher_scale_in, R.anim.mi_laucher_scale_out, R.anim.pophidden_anim, R.anim.popshow_anim, R.anim.push_left_in, R.anim.push_left_out,
R.anim.push_up_in, R.anim.push_up_out, R.anim.rbm_in_from_left, R.anim.rbm_out_to_left, R.anim.refreshable_list_rotate, R.anim.right_in,
R.anim.right_out, R.anim.shrink_from_bottom, R.anim.slide_down_out, R.anim.slide_left, R.anim.slide_right, R.anim.slide_up_in, R.anim.small_2_big,
R.anim.umeng_fb_slide_in_from_left, R.anim.umeng_fb_slide_in_from_right, R.anim.umeng_fb_slide_out_from_left, R.anim.umeng_fb_slide_out_from_right,
R.anim.umeng_socialize_fade_in, R.anim.umeng_socialize_fade_out, R.anim.umeng_socialize_shareboard_animation_in,
R.anim.umeng_socialize_shareboard_animation_out, R.anim.umeng_socialize_slide_in_from_bottom, R.anim.umeng_socialize_slide_out_from_bottom,
R.anim.unzoom_in, R.anim.unzoom_out, R.anim.welcome_fade_in_scale, R.anim.welcome_fade_out, R.anim.zoom_enter, R.anim.zoom_exit };
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] array_name = new String[array_id.length];
for (int i = 0; i < array_id.length; i++) {
array_name[i] = getResources().getResourceName(array_id[i]).replace(getPackageName() + ":anim/", "");
}
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>(Arrays.asList(array_name))));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
v.setAnimation(AnimationUtils.loadAnimation(this, array_id[position]));
}
}
XML示例
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" >
<translate
android:duration="1000"
android:fillAfter="true"
android:fromYDelta="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toYDelta="100" />
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>