android学习(属性动画扇形菜单栏)

  1 public class TestActivity extends Activity implements OnClickListener{
  2     private int[] res={R.id.imageView1,R.id.imageView2,R.id.imageView3,R.id.imageView4,R.id.imageView5,R.id.imageView6};
  3     private List<ImageView> list=new ArrayList<ImageView>();
  4     private boolean flag;
  5     @Override
  6     protected void onCreate(Bundle savedInstanceState) {
  7         // TODO Auto-generated method stub
  8         super.onCreate(savedInstanceState);
  9         setContentView(R.layout.tests);
 10         for(int i=0;i<res.length;i++){
 11             ImageView iv=(ImageView) findViewById(res[i]);
 12             iv.setOnClickListener(this);
 13             list.add(iv);
 14         }
 15         
 16     }
 17 /**
 18 public void move(View view){
 19     ImageView iv=(ImageView) findViewById(R.id.imageView1);
 20     //属性动画
 21 //    ObjectAnimator.ofFloat(iv, "translationX", 0,200f).setDuration(1000).start();//X轴移动 
 22     
 23     //第2种 优化
 24 //    PropertyValuesHolder p1=PropertyValuesHolder.ofFloat("translationX", 0,200f);
 25 //    PropertyValuesHolder p2=PropertyValuesHolder.ofFloat("translationY", 0,200f);
 26 //    PropertyValuesHolder p3=PropertyValuesHolder.ofFloat("rotation", 0,360f);
 27 //    ObjectAnimator.ofPropertyValuesHolder(iv, p1,p2,p3).setDuration(1000).start();
 28     
 29     //结合 
 30     AnimatorSet set=new AnimatorSet();
 31     ObjectAnimator oa1=ObjectAnimator.ofFloat(iv, "translationX", 0,200f);
 32     ObjectAnimator oa2=ObjectAnimator.ofFloat(iv, "translationY", 0,200f);
 33     ObjectAnimator oa3=ObjectAnimator.ofFloat(iv, "rotation", 0,360f);
 34 //    set.playTogether(oa1,oa2,oa3);//一起
 35 //    set.playSequentially(oa1,oa2,oa3);//顺序播放
 36     set.play(oa1).with(oa2);
 37     set.play(oa3).after(oa2);//平移在一起,最后在旋转
 38     set.setDuration(1000);
 39     set.start();
 40 }
 41 */
 42 
 43     @Override
 44     public void onClick(View v) {
 45         switch (v.getId()) {
 46         case R.id.imageView1:
 47             if(flag){
 48             startAnimat();
 49             }else{
 50                 closeAnimat();
 51             }
 52             break;
 53 
 54         default:
 55             Toast.makeText(this, "click"+v.getId(), Toast.LENGTH_SHORT).show();
 56             break;
 57         }
 58         
 59     }
 60 private void closeAnimat() {
 61     for(int i=1;i<res.length;i++){
 62         AnimatorSet set=new AnimatorSet();
 63         ObjectAnimator oa1=ObjectAnimator.ofFloat(list.get(i), "translationX",(float)Math.sin(Math.toRadians((i-1)*90/(5-1)))*200 ,0);
 64         ObjectAnimator oa2=ObjectAnimator.ofFloat(list.get(i), "translationY",(float)Math.cos(Math.toRadians((i-1)*90/(5-1)))*200 ,0);
 65         set.playTogether(oa1,oa2);
 66         set.setDuration(3000);
 67         set.setInterpolator(new BounceInterpolator());
 68 //        set.setStartDelay(i*300);
 69         set.start();
 70         flag=true;
 71         
 72     }
 73     
 74 }
 75 /**
 76  * 菜单展开
 77  */
 78 private void startAnimat() {
 79     // TODO Auto-generated method stub
 80     for(int i=1;i<res.length;i++){
 81         AnimatorSet set=new AnimatorSet();
 82         ObjectAnimator oa1=ObjectAnimator.ofFloat(list.get(i), "translationX",0,(float)Math.sin(Math.toRadians((i-1)*90/(5-1)))*200 );
 83         ObjectAnimator oa2=ObjectAnimator.ofFloat(list.get(i), "translationY",0,(float)Math.cos(Math.toRadians((i-1)*90/(5-1)))*200 );
 84         set.playTogether(oa1,oa2);
 85         set.setDuration(3000);
 86 //        set.setStartDelay(i*300);
 87         set.start();
 88         flag=false;
 89         
 90     }
 91 }
 92     
 93     /**
 94      * 监听事件
 95      */
 96 //    public void move(View view){
 97 //        ObjectAnimator oa1=ObjectAnimator.ofFloat(view, "alpha", 0,1f);
 98 //        oa1.setDuration(1000).start();
 99 //        oa1.addListener(new AnimatorListenerAdapter() {
100 //            @Override
101 //            public void onAnimationEnd(Animator animation) {
102 //                Toast.makeText(TestActivity.this,"click", Toast.LENGTH_SHORT).show();
103 //            }
104 //        });
105 //    }
106 }

布局文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     
 7 
 8     <ImageView
 9         android:id="@+id/imageView3"
10         android:paddingLeft="5dp"
11         android:paddingTop="5dp"
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         android:src="@drawable/c" />
15 
16     <ImageView
17         android:id="@+id/imageView4"
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:paddingLeft="5dp"
21         android:paddingTop="5dp"
22         android:src="@drawable/d" />
23 
24     <ImageView
25         android:id="@+id/imageView5"
26         android:paddingLeft="5dp"
27         android:paddingTop="5dp"
28         android:layout_width="wrap_content"
29         android:layout_height="wrap_content"
30         android:src="@drawable/e" />
31 
32     <ImageView
33         android:id="@+id/imageView6"
34         android:layout_width="wrap_content"
35         android:layout_height="wrap_content"
36         android:paddingLeft="5dp"
37         android:paddingTop="5dp"
38         android:src="@drawable/f" />
39 
40     
41 
42     <ImageView
43         android:id="@+id/imageView2"
44         android:layout_width="wrap_content"
45         android:layout_height="wrap_content"
46         android:paddingLeft="5dp"
47         android:paddingTop="5dp"
48         android:src="@drawable/b" />
49 <ImageView
50         android:id="@+id/imageView1"
51         android:layout_width="wrap_content"
52         android:layout_height="wrap_content"
53         android:src="@drawable/a" />
54 </FrameLayout>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

mark

posted on 2016-05-05 22:56  oldcowstruggle  阅读(1089)  评论(0编辑  收藏  举报