Android使用属性动画,实现分散式弹出菜单
布局文件:
<LinearLayout android:orientation="horizontal" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:background="#f00" android:id="@+id/mIvIcon1" android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/mIvIcon2" android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/mIvIcon3" android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/mIvIcon4" android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/mIvIcon5" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/mIvIcon6" android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> mainActivity方法:public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private ImageView mIvIcon1; private ImageView mIvIcon2; private ImageView mIvIcon3; private ImageView mIvIcon4; private ImageView mIvIcon5; private ImageView mIvIcon6; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setImageListener(); } /** * 初始化视图 */ private void initView() { setContentView(R.layout.activity_main); mIvIcon1 = (ImageView) findViewById(R.id.mIvIcon1); mIvIcon2 = (ImageView) findViewById(R.id.mIvIcon2); mIvIcon3 = (ImageView) findViewById(R.id.mIvIcon3); mIvIcon4 = (ImageView) findViewById(R.id.mIvIcon4); mIvIcon5 = (ImageView) findViewById(R.id.mIvIcon5); mIvIcon6 = (ImageView) findViewById(R.id.mIvIcon6); } /** * 添加时间监听 */ private void setImageListener() { mIvIcon1.setOnClickListener(this); mIvIcon2.setOnClickListener(this); mIvIcon3.setOnClickListener(this); mIvIcon4.setOnClickListener(this); mIvIcon5.setOnClickListener(this); mIvIcon6.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.mIvIcon1: Toast.makeText(this, "icon1", Toast.LENGTH_LONG).show(); break; case R.id.mIvIcon2: Toast.makeText(this, "icon2", Toast.LENGTH_LONG).show(); break; case R.id.mIvIcon3: Toast.makeText(this, "icon3", Toast.LENGTH_LONG).show(); break; case R.id.mIvIcon4: Toast.makeText(this, "icon4", Toast.LENGTH_LONG).show(); break; case R.id.mIvIcon5: Toast.makeText(this, "icon5", Toast.LENGTH_LONG).show(); break; case R.id.mIvIcon6: //点击最外层icon,展开icon动画 showIcon(); break; default: break; } } /** * 动画实现,因为除了沿X,Y轴的图标,另外3个都有角度,所有,要有三角函数计算 * 使图标位移距离相等,实现扇形效果 */ @SuppressLint("NewApi") private void showIcon() { //设置动画时间 int duration = 500; //动画距离,屏幕宽度的60% float distance = getScreenWidth()*0.6f; //相邻ImageView运动角度式22.5度 float angle1 = (float)(22.5f*Math.PI/180); float angle2 = (float)(45f*Math.PI/180); float angle3 = (float)(67.5f*Math.PI/180); //icon1 PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("TranslationX", 0f, distance); //icon2 PropertyValuesHolder p21 = PropertyValuesHolder.ofFloat("TranslationX", 0f, (float)(distance*Math.cos(angle1))); PropertyValuesHolder p22 = PropertyValuesHolder.ofFloat("TranslationY", 0f, -(float)(distance*Math.sin(angle1))); //icon3 PropertyValuesHolder p31 = PropertyValuesHolder.ofFloat("TranslationX", 0f, (float)(distance*Math.cos(angle2))); PropertyValuesHolder p32 = PropertyValuesHolder.ofFloat("TranslationY", 0f, -(float)(distance*Math.sin(angle2))); //icon4 PropertyValuesHolder p41 = PropertyValuesHolder.ofFloat("TranslationX", 0f, (float)(distance*Math.cos(angle3))); PropertyValuesHolder p42 = PropertyValuesHolder.ofFloat("TranslationY", 0f, -(float)(distance*Math.sin(angle3))); //icon5 PropertyValuesHolder p5 = PropertyValuesHolder.ofFloat("TranslationY", 0f, -distance); ObjectAnimator animator1 = ObjectAnimator.ofPropertyValuesHolder(mIvIcon1, p5).setDuration(duration); ObjectAnimator animator2 = ObjectAnimator.ofPropertyValuesHolder(mIvIcon2, p21, p22).setDuration(duration); ObjectAnimator animator3 = ObjectAnimator.ofPropertyValuesHolder(mIvIcon3, p31,p32).setDuration(duration); ObjectAnimator animator4 = ObjectAnimator.ofPropertyValuesHolder(mIvIcon4, p41,p42).setDuration(duration); ObjectAnimator animator5 = ObjectAnimator.ofPropertyValuesHolder(mIvIcon5, p1).setDuration(duration); //添加自由落体效果插值器 animator1.setInterpolator(new BounceInterpolator()); animator2.setInterpolator(new BounceInterpolator()); animator3.setInterpolator(new BounceInterpolator()); animator4.setInterpolator(new BounceInterpolator()); animator5.setInterpolator(new BounceInterpolator()); //启动动画 animator1.start(); animator2.start(); animator3.start(); animator4.start(); animator5.start(); } /** * 竖屏时获取屏幕宽度,横屏时,获取高度 * @return */ public int getScreenWidth(){ DisplayMetrics outMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(outMetrics); int x = outMetrics.widthPixels; int y = outMetrics.heightPixels; return x>y?y:x; } }