• 用属性动画模仿展开菜单


    页面布局文件

    <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 以自身中心为轴,怎么赋值参数呢?

  • 相关阅读:
    web图片100%宽度自适应,高度不塌陷
    基于Kafka+ELK搭建海量日志平台
    一句话撸完重量级锁、自旋锁、轻量级锁、偏向锁、悲观、乐观锁等
    MySQL数据库设计规范
    夺命连环问:一个 TCP 连接可以发多少个 HTTP 请求?
    [Kafka]
    ZooKeeper学习总结 第一篇:ZooKeeper快速入门
    Vue图片浏览组件v-viewer,支持旋转、缩放、翻转等操作
    opencv处理验证码python代码
    mac使用pytesseract
  • 原文地址:https://www.cnblogs.com/Karson001/p/4229346.html
Copyright © 2020-2023  润新知