1 <?xml version="1.0" encoding="utf-8"?>
2 <animation-list xmlns:android="http://schemas.android.com/apk/res/android">
3 item android:drawable="@drawable/n0" android:duration="300"></item 4 ="@drawable/n1" 5 ="@drawable/n2" 6 ="@drawable/n3" 7 ="@drawable/n4" 8 ="@drawable/n5" 9 ="@drawable/n6"10 ="@drawable/n7"11 ="@drawable/n8"12 ="@drawable/n9"13 </animation-list>
private AnimationDrawable drawable;
2
3 @Override
protected void onCreate(Bundle savedInstanceState) {
5 super.onCreate(savedInstanceState);
6 setContentView(R.layout.activity_drawable);
7 ImageView imageView= (ImageView) this.findViewById(R.id.ivLetter);
8 drawable= (AnimationDrawable) imageView.getBackground();
9 drawable.start();
10 }
11
12 13 public boolean onTouchEvent(MotionEvent event) {
14 if(event.getAction()==MotionEvent.ACTION_DOWN){
15 if(drawable.isRunning()) {
16 drawable.stop();
17 }else{
18 drawable.start();
19 }
20 }
21 return .onTouchEvent(event);
22 }
1 /**
* 平移
* @param v
4 */
protected transfer_click(View v){
6
7 //参数是平移的起始坐标和结束坐标(起始X轴位置,结束X轴位置,起始Y轴位置,结束Y轴位置)的改变量。
8 TranslateAnimation trans=new TranslateAnimation(0.0f,300f,0.0f,300f);
9 fromXType 动画平移改变量的类型
10 Animation.RELATIVE_TO_SELF,0 表示控件现在的坐标+0*控件本身的宽度或高度
11 12 Animation.RELATIVE_TO_PARENT 相对于父控件,计算方式和Animation.RELATIVE_TO_SELF一样
13 fromXValue 起始坐标值的改变量,如果类型是ABSOLUTE,则此值为绝对数字,否则则表示百分比(0-1)之间。
14 TranslateAnimation trans=new TranslateAnimation(Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_SELF,1,1);
15 trans.setDuration(2000);设置周期
16 trans.setFillAfter(true);当结束时保持结束位置
17 trans.setRepeatCount(2);设置重复次数
18 trans.setRepeatMode(Animation.REVERSE);重复模式
19 ivTaichi.startAnimation(trans);启动
21
22 23 * 旋转
24 25 26 rotate_click(View v){
27 参数是旋转的起始偏移量(度数),结束度数,旋转中心点(相对x轴 位置和y轴位置)。
28 RotateAnimation rotate=new RotateAnimation(0.0f,90.f,100.0f,100.0f);
29 RotateAnimation rotate =new RotateAnimation(0.0f,360.0f,0.5f,0.5f30 rotate.setFillAfter(true31 rotate.setDuration(200032 rotate.setRepeatCount(233 rotate.setRepeatMode(Animation.REVERSE);
34 ivTaichi.startAnimation(rotate);35 36
37 38 * 缩放
39 40 41 scale_click(View v){
42 fromX toX 动画起始和结束时的X轴水平缩放因子
43 fromY toY 动画起始和结束时的Y轴水平缩放因子
44 ScaleAnimation scale=new ScaleAnimation(0.5f,1.5f,1.5f45 scale.setFillAfter(46 scale.setDuration(200047 scale.setRepeatCount(248 scale.setRepeatMode(Animation.REVERSE);
49 ivTaichi.startAnimation(scale);50 51
52 53 * 透明度动画
54 55 56 alpha_click(View v){
57 fromAlpha toAlpha 动画起始和结束时的透明度。范围(0,1)
58 AlphaAnimation alpha=new AlphaAnimation(0,1)">59 alpha.setFillAfter(60 alpha.setDuration(200061 alpha.setRepeatCount(262 alpha.setRepeatMode(Animation.REVERSE);
63 ivTaichi.startAnimation(alpha);64 65
66 67 * 集合动画
68 69 70 set_click(View v){
71 AnimationSet set=new AnimationSet(72 TranslateAnimation animation1=new TranslateAnimation(0.0f,300.0f,300.0f);
73 RotateAnimation animation2 =74 ScaleAnimation animation3=new ScaleAnimation(0.0f,1.0f,1.0f75 AlphaAnimation animation4=76 set.addAnimation(animation1);
77 set.addAnimation(animation2);
78 set.addAnimation(animation3);
79 set.addAnimation(animation4);
80 set.setFillAfter(81 set.setDuration(200082 set.setRepeatCount(283 set.setRepeatMode(Animation.REVERSE);
84 ivTaichi.startAnimation(set);85 }
6 target 属性动画的目标控件
propertyName 产生动画的属性,所有的属性必须拥有set,get方法
values 属性动画的范围集合
9 ObjectAnimator objectAnimator =ObjectAnimator.ofFloat(ivTaichi,"translationX",200,-200,010 objectAnimator.setDuration(200011 objectAnimator.setRepeatCount(2 objectAnimator.setRepeatMode(Animation.REVERSE);
objectAnimator.start();
14 15
16 17 19 20 21 ObjectAnimator objectAnimator =ObjectAnimator.ofFloat(ivTaichi,"rotationX",18022 objectAnimator.setDuration(200023 objectAnimator.setRepeatCount(225 26 27
28 29 30 31 32 33 ObjectAnimator objectAnimator =ObjectAnimator.ofFloat(ivTaichi,"scaleX",1)">34 objectAnimator.setDuration(200035 objectAnimator.setRepeatCount(236 37 39
40 41 * 透明度
42 43 44 45 ObjectAnimator objectAnimator =ObjectAnimator.ofFloat(ivTaichi,"alpha",1)">46 objectAnimator.setDuration(200047 objectAnimator.setRepeatCount(249 57 AnimatorSet set=new AnimatorSet();
58 List<Animator> list=new ArrayList<Animator>() ;
59 ObjectAnimator objectAnimator1 =ObjectAnimator.ofFloat(ivTaichi,20060 ObjectAnimator objectAnimator2 =ObjectAnimator.ofFloat(ivTaichi,1)">61 ObjectAnimator objectAnimator3 =ObjectAnimator.ofFloat(ivTaichi,1)">62 ObjectAnimator objectAnimator4 =ObjectAnimator.ofFloat(ivTaichi,1)">63 list.add(objectAnimator1);
list.add(objectAnimator2);
65 list.add(objectAnimator3);
66 list.add(objectAnimator4);
67 播放一序列的动画对象
set.playSequentially(list);
69 //
70 set.start();
71 }