Android自定义控件3--优酷菜单执行动画

时间:2022-09-29 20:34:35

上篇文章中实现了优酷菜单的布局,本文接着实现动画功能

本文地址:http://www.cnblogs.com/wuyudong/p/5914901.html,转载请注明源地址。

新建动画工具类AnimationUtils.java,代码如下:

package com.wuyudong.youkumenu.utils;

import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.RelativeLayout;

public class AnimationUtils {

    // 旋转出去的动画
    public static void rotateOutAnim(RelativeLayout layout, long delay) {
        RotateAnimation ra = new RotateAnimation(
                0f, -180f, //开始,结束的角度, 逆时针
                Animation.RELATIVE_TO_SELF, 0.5f, //相对的x坐标点(指定旋转中心x值)
                Animation.RELATIVE_TO_SELF, 1.0f); //相对的y坐标点(指定旋转中心y值)
        ra.setDuration(500);
        ra.setFillAfter(true);
        ra.setStartOffset(delay);
        layout.startAnimation(ra);
        
    }
    
    // 旋转进来的动画
    public static void rotateInAnim(RelativeLayout layout) {
        RotateAnimation ra = new RotateAnimation(
                -180f, 0f,  //开始,结束的角度, 顺时针
                Animation.RELATIVE_TO_SELF, 0.5f, //相对的x坐标点(指定旋转中心x值)
                Animation.RELATIVE_TO_SELF, 1.0f); //相对的y坐标点(指定旋转中心y值)
        ra.setDuration(500);
        ra.setFillAfter(true);
        layout.startAnimation(ra);
        
    }

}

接着编写逻辑部分代码:

package com.wuyudong.youkumenu;

import com.wuyudong.youkumenu.utils.AnimationUtils;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;

public class MainActivity extends Activity implements OnClickListener {

    private RelativeLayout rl_level1;
    private RelativeLayout rl_level2;
    private RelativeLayout rl_level3;
    private boolean islevel1Display = true;
    private boolean islevel2Display = true;
    private boolean islevel3Display = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化控件
        initViews();
    }

    private void initViews() {
        // 添加点击事件
        findViewById(R.id.ib_home).setOnClickListener(this);
        findViewById(R.id.ib_menu).setOnClickListener(this);
        rl_level1 = (RelativeLayout) findViewById(R.id.rl_level1);
        rl_level2 = (RelativeLayout) findViewById(R.id.rl_level2);
        rl_level3 = (RelativeLayout) findViewById(R.id.rl_level3);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.ib_home:
            if (islevel2Display) {
                long delay = 200;
                // 如果当前三级菜单已经显示,先转出去
                if (islevel3Display) {
                    AnimationUtils.rotateOutAnim(rl_level3, 0);
                    islevel3Display = false;
                    delay += 200;

                } 
                //如果当前二级菜单已经显示,转出去
                AnimationUtils.rotateOutAnim(rl_level2, delay);
                
            } else {
                //如果当前二级菜单没有显示,转出来
                AnimationUtils.rotateInAnim(rl_level2);
            }
            islevel2Display = !islevel2Display;

            break;
        case R.id.ib_menu:
            if (islevel3Display) {
                // 如果当前三级菜单已经显示,转出去
                AnimationUtils.rotateOutAnim(rl_level3, 0);
            } else {
                AnimationUtils.rotateInAnim(rl_level3);
            }
            islevel3Display = !islevel3Display;
            break;

        default:
            break;
        }

    }

}

基本实现菜单的旋转功能