Android使用属性动画,实现分散式弹出菜单

                          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;
    }
}










  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值