页面布局文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="5dp" android:paddingTop="5dp"> <ImageView android:id="@+id/imageView_b" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/composer_camera"/> <ImageView android:id="@+id/imageView_a" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/composer_music"/> <ImageView android:id="@+id/imageView_c" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/composer_place"/> <ImageView android:id="@+id/imageView_d" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/composer_thought"/> <ImageView android:id="@+id/imageView_e" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/composer_with"/> <ImageView android:id="@+id/imageView_f" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="1dp" android:src="@drawable/composer_button"/> <ImageView android:id="@+id/imageView_g" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="22dp" android:paddingTop="22dp" android:src="@drawable/composer_icn_plus"/> </FrameLayout>
代码:
private int[] res={R.id.imageView_a,R.id.imageView_b,R.id.imageView_c, R.id.imageView_d,R.id.imageView_e,R.id.imageView_f,R.id.imageView_g}; private List<ImageView> imageViewList = new ArrayList<ImageView>(); private Boolean flag = false; private PropertyValuesHolder p1,p2; @Override public void onClick(View v) { switch (v.getId()) { case R.id.imageView_g: if(!flag) startAnimation(); else closeAnimation(); break; default: Toast.makeText(PlusActivity.this, " " + v.getId(), Toast.LENGTH_SHORT).show(); break; } } private void closeAnimation() { ObjectAnimator.ofFloat(imageViewList.get(6),"rotation",20f,360f).setDuration(1000).start(); for (int i=0;i<res.length-2;i++) { ObjectAnimator animator = ObjectAnimator.ofFloat(imageViewList.get(i),"translationY",i*60f,0f); animator.setDuration(1000); animator.setInterpolator(new BounceInterpolator()); animator.setStartDelay(i*300); animator.start(); } flag = false; } /* 开始动画 */ private void startAnimation() { ObjectAnimator.ofFloat(imageViewList.get(6),"rotation",20f,360f).setDuration(1000).start(); for (int i=0;i<res.length-2;i++) { ObjectAnimator animator = ObjectAnimator.ofFloat(imageViewList.get(i),"translationY",0f,i*60f); animator.setDuration(1000); animator.setInterpolator(new BounceInterpolator()); animator.setStartDelay(i*300); animator.start(); } flag = true; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); for(int i=0;i<res.length;i++) { ImageView imageView = (ImageView)findViewById(res[i]); imageView.setOnClickListener(this); imageViewList.add(imageView); } }
附上效果:
一个简单的小例子,属性动画比之前的动画在使用方面要方便很多。但是在下也是刚刚起步,还有很多不完善的地方,希望同道中人指正。
有两点没有琢磨明白怎么实现:
1、如果我想扇形的展开菜单改怎么写呢?
2、我想让rotation 以自身中心为轴,怎么赋值参数呢?
时间: 2024-12-21 05:40:44