Android动画-收缩菜单

Android 收缩菜单按钮

废话不多说,直接上效果图

你好! 这是你第一次使用 **Markdown编辑器** 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。

代码

xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#DCDCDC">


    <RelativeLayout
        android:id="@+id/rl_Relative"
        android:layout_width="45dp"
        android:layout_height="250dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal"
        android:layout_marginBottom="100dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:layout_marginRight="30dp"
        android:orientation="vertical">
        <!--<View-->
            <!--android:id="@+id/v_View"-->
            <!--android:layout_width="45dp"-->
            <!--android:layout_height="35dp"-->
            <!--android:orientation="vertical"-->
            <!--android:layout_centerInParent="true"-->
            <!--android:visibility="invisible"-->
            <!--android:layout_alignParentBottom="true"/>-->
        <ImageView
            android:src="@mipmap/message"
            android:id="@+id/iv_menu3"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"/>
        <ImageView
            android:src="@mipmap/collect"
            android:id="@+id/iv_menu2"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"/>
        <ImageView
            android:src="@mipmap/shopping"
            android:id="@+id/iv_menu1"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"/>
        <ImageView
            android:src="@mipmap/menu"
            android:id="@+id/iv_menu"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"/>
    </RelativeLayout>
</RelativeLayout>


java

package com.lrq.animation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import static android.view.animation.Animation.RELATIVE_TO_SELF;

public class TranlationAnim extends Activity implements View.OnClickListener {

    private ImageView iv_menu3;
    private ImageView iv_menu2;
    private ImageView iv_menu1;
    private ImageView iv_menu;//菜单按钮

//    private View v_View;//

    private int intHeight;//菜单按钮高度
    private int intViewHeight;//菜单子按钮高度

    private AnimationSet animationSet;//动画组
    private Animation roate;//旋转
    private Animation tranlate;//一定
    private Animation scale;//缩放

    private boolean isClose = true;//是否已打开菜单

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.anim_tranlation);
        initView();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_menu:
                //点击菜单按钮
                if (isClose){

                    initStartAnim(360,-intHeight,-intViewHeight,-20);//展开菜单

                    /*60表示旋转角度, -20表示子项之间的间距,-intHeight,-intViewHeight决定伸缩的方向和高度,
                    由于数字为正的展开方向是向下,所以要向上展开需要为负数
                     */
                    isClose = false;
                }else{
                    initStopAnim(-360,-intHeight,-intViewHeight,-20);//关闭菜单
                    isClose = true;
                }
                break;
        }
    }
    //初始化动画
    private void initStartAnim(int toDegrees,int height,int viewHeight,int margin) {
        //menu
        animationSet = new AnimationSet(true);
        //旋转动画,菜单按钮至需要旋转即可
        roate = new RotateAnimation(0,toDegrees,RELATIVE_TO_SELF,0.5f,RELATIVE_TO_SELF,0.5f);
        roate.setDuration(1000);
        roate.setRepeatCount(0);
        animationSet.addAnimation(roate);
        animationSet.setFillAfter(true);
        iv_menu.startAnimation(animationSet);
        //menu1  第一个子菜单
        animationSet = new AnimationSet(true);
        //旋转
        roate = new RotateAnimation(0,toDegrees,RELATIVE_TO_SELF,0.5f,RELATIVE_TO_SELF,0.5f);
        roate.setDuration(1000);
        roate.setRepeatCount(0);
        //移动
        tranlate = new TranslateAnimation(0,0,0,height+margin);
        tranlate.setFillAfter(true);
        tranlate.setDuration(1000);

        animationSet.addAnimation(roate);
        animationSet.addAnimation(tranlate);
        animationSet.setFillAfter(true);
        iv_menu1.startAnimation(animationSet);
        //menu2 第二个子菜单
        animationSet = new AnimationSet(true);
        //旋转
        roate = new RotateAnimation(0,toDegrees,RELATIVE_TO_SELF,0.5f,RELATIVE_TO_SELF,0.5f);
        roate.setDuration(1000);
        roate.setRepeatCount(0);
        //移动
        tranlate = new TranslateAnimation(0,0,0,height+viewHeight+margin*2);
        tranlate.setFillAfter(true);
        tranlate.setDuration(1000);

        animationSet.addAnimation(roate);
        animationSet.addAnimation(tranlate);
        animationSet.setFillAfter(true);
        iv_menu2.startAnimation(animationSet);
        //menu3 第三个子菜单
        animationSet = new AnimationSet(true);
        //旋转
        roate = new RotateAnimation(0,toDegrees,RELATIVE_TO_SELF,0.5f,RELATIVE_TO_SELF,0.5f);
        roate.setDuration(1000);
        roate.setRepeatCount(0);
        //移动
        tranlate = new TranslateAnimation(0,0,0,height+viewHeight*2+margin*3);
        tranlate.setFillAfter(true);
        tranlate.setDuration(1000);

        animationSet.addAnimation(roate);
        animationSet.addAnimation(tranlate);
        animationSet.setFillAfter(true);
        iv_menu3.startAnimation(animationSet);
        //背景
//        scale = new ScaleAnimation(1,1,1,(float) 4.9,RELATIVE_TO_SELF,0f,RELATIVE_TO_SELF,1f);
//        scale.setFillAfter(true);
//        scale.setDuration(1000);
//        v_View.startAnimation(scale);
    }
    private void initStopAnim(int toDegrees,int height,int viewHeight,int margin) {
        //menu
        animationSet = new AnimationSet(true);
        roate = new RotateAnimation(0,toDegrees,RELATIVE_TO_SELF,0.5f,RELATIVE_TO_SELF,0.5f);
        roate.setDuration(1000);
        roate.setRepeatCount(0);
        animationSet.addAnimation(roate);
        animationSet.setFillAfter(true);
        iv_menu.startAnimation(animationSet);

        //menu1
        animationSet = new AnimationSet(true);
        //旋转
        roate = new RotateAnimation(0,toDegrees,RELATIVE_TO_SELF,0.5f,RELATIVE_TO_SELF,0.5f);
        roate.setDuration(1000);
        roate.setRepeatCount(0);
        //移动
        tranlate = new TranslateAnimation(0,0,height+margin,0);
        tranlate.setFillAfter(true);
        tranlate.setDuration(1000);

        animationSet.addAnimation(roate);
        animationSet.addAnimation(tranlate);
        animationSet.setFillAfter(true);
        iv_menu1.startAnimation(animationSet);
        //menu2
        animationSet = new AnimationSet(true);
        //旋转
        roate = new RotateAnimation(0,toDegrees,RELATIVE_TO_SELF,0.5f,RELATIVE_TO_SELF,0.5f);
        roate.setDuration(1000);
        roate.setRepeatCount(0);
        //移动
        tranlate = new TranslateAnimation(0,0,height+viewHeight+margin*2,0);
        tranlate.setFillAfter(true);
        tranlate.setDuration(1000);

        animationSet.addAnimation(roate);
        animationSet.addAnimation(tranlate);
        animationSet.setFillAfter(true);
        iv_menu2.startAnimation(animationSet);
        //menu3
        animationSet = new AnimationSet(true);
        //旋转
        roate = new RotateAnimation(0,toDegrees,RELATIVE_TO_SELF,0.5f,RELATIVE_TO_SELF,0.5f);
        roate.setDuration(1000);
        roate.setRepeatCount(0);
        //移动
        tranlate = new TranslateAnimation(0,0,height+viewHeight*2+margin*3,0);
        tranlate.setFillAfter(true);
        tranlate.setDuration(1000);

        animationSet.addAnimation(roate);
        animationSet.addAnimation(tranlate);
        animationSet.setFillAfter(true);
        iv_menu3.startAnimation(animationSet);
        //背景
//        scale = new ScaleAnimation(1,1,(float) 4.9,1,RELATIVE_TO_SELF,0f,RELATIVE_TO_SELF,1f);
//        scale.setFillAfter(true);
//        scale.setDuration(1000);
//        v_View.startAnimation(scale);
    }

    //在此方法中获得控件宽高,否则为空
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        intHeight = iv_menu.getHeight();
        intViewHeight = iv_menu1.getHeight();
    }
    //获取控件
    private void initView() {
        iv_menu3 = findViewById(R.id.iv_menu3);
        iv_menu2 = findViewById(R.id.iv_menu2);
        iv_menu1 = findViewById(R.id.iv_menu1);
        iv_menu =  findViewById(R.id.iv_menu);
//        v_View = findViewById(R.id.v_View);

        iv_menu.setOnClickListener(this);
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值