Android实现底部缓慢弹出菜单

项目要求要做一个从底部缓慢弹出一个Button Menu

开始编码:
MyselfFragment

package io.dcloud.H5B79C397.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.vanda.vandalibnetwork.fragment.BaseFragment;

import java.util.Map;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import io.dcloud.H5B79C397.R;
import io.dcloud.H5B79C397.Urls;
import io.dcloud.H5B79C397.activity.BottomMenu;
import io.dcloud.H5B79C397.activity.LoginActivity;
import io.dcloud.H5B79C397.activity.Myself_About_fae;
import io.dcloud.H5B79C397.activity.Myself_Invitation;
import io.dcloud.H5B79C397.activity.Myself_help;
import io.dcloud.H5B79C397.activity.Myself_setting;
import io.dcloud.H5B79C397.activity.RegisterActivity;
import io.dcloud.H5B79C397.activity.user_management;
import io.dcloud.H5B79C397.pojo.InformmationData;
import io.dcloud.H5B79C397.util.ActionBarUtil;
import io.dcloud.H5B79C397.util.ExtUtils;

/**
 * 用户个人信息界面
 */
public class MyselfFragment extends BaseFragment<InformmationData> implements View.OnClickListener {
  private Context mContext;
  private View view;
  private BottomMenu menuWindow;
  private RelativeLayout layout_advise, layout_collect, layout_logo, layout_share, layout_qr_code, layout_help, layout_set, layout_url;
  private Button btnLogin, btnRegister;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
               Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_information, container, false);

    initView();
    return view;

  }

  @Override
  public void onStart() {
    super.onStart();
  }

  private void initView() {
    new ActionBarUtil(view, R.id.actionBar, R.id.Back, R.id.Title, R.id.Activity, "我的管理中心", getActivity(), 2);

    layout_advise = (RelativeLayout) view.findViewById(R.id.myself_advise);
    layout_collect = (RelativeLayout) view.findViewById(R.id.myself_collect);
    layout_logo = (RelativeLayout) view.findViewById(R.id.myself_logo);
    layout_share = (RelativeLayout) view.findViewById(R.id.myself_share);
    layout_qr_code = (RelativeLayout) view.findViewById(R.id.myself_qr_code);
    layout_help = (RelativeLayout) view.findViewById(R.id.myself_help);
    layout_set = (RelativeLayout) view.findViewById(R.id.myself_set);
    layout_url = (RelativeLayout) view.findViewById(R.id.myself_url);

    btnLogin = (Button) view.findViewById(R.id.myself_login);
    btnRegister = (Button) view.findViewById(R.id.myself_register);

    layout_advise.setOnClickListener(this);
    layout_collect.setOnClickListener(this);
    layout_logo.setOnClickListener(this);
    layout_share.setOnClickListener(this);
    layout_qr_code.setOnClickListener(this);
    layout_help.setOnClickListener(this);
    layout_set.setOnClickListener(this);
    layout_url.setOnClickListener(this);
    btnLogin.setOnClickListener(this);
    btnRegister.setOnClickListener(this);
  }


  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.myself_advise:
        startActivity(new Intent(getActivity(), user_management.class));
        break;
      case R.id.myself_collect:
        break;
      case R.id.myself_logo:
        Intent intent=new Intent(getActivity(),Myself_About_fae.class);
        startActivity(intent);
        break;
      case R.id.myself_share:
        menuWindow = new BottomMenu(mContext, clickListener);
        menuWindow.show();

        break;
      case R.id.myself_qr_code:
        startActivity(new Intent(getActivity(), Myself_Invitation.class));
        break;
      case R.id.myself_help:
        startActivity(new Intent(getActivity(), Myself_help.class));
        break;
      case R.id.myself_set:
          startActivity(new Intent(getActivity(), Myself_setting.class));
        break;
      case R.id.myself_url:
        Uri uri = Uri.parse("http://www.fae.cn/");
        Intent intent1 = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent1);
        break;
      case R.id.myself_login:
        startActivity(new Intent(getActivity(), LoginActivity.class));
        break;
      case R.id.myself_register:
        startActivity(new Intent(getActivity(), RegisterActivity.class));
        break;
      default:
        break;
    }
  }
  private View.OnClickListener clickListener = new View.OnClickListener(){
    public void onClick(View v) {
      switch (v.getId()) {
        case R.id.btn1:
          showZiDingYi(Wechat.NAME);
          break;
        case R.id.btn2:
          showZiDingYi(WechatMoments.NAME);
          Toast.makeText(mContext,"这是微信朋友圈",Toast.LENGTH_SHORT).show();
          break;
        case R.id.sina:
          showZiDingYi(SinaWeibo.NAME);
          break;
        case R.id.QQ:
          showZiDingYi(QQ.NAME);
          break;
        case R.id.more:
          Toast.makeText(mContext,"这是更多",Toast.LENGTH_SHORT).show();

          break;
        default:
          break;
      }
    }
  };
  private void showZiDingYi(String ss){
    ShareSDK.initSDK(getActivity());
    OnekeyShare oks = new OnekeyShare();
    oks.setSilent(false);
    oks.setDialogMode();
    oks.setText("xxxxxxxxx");
    //oks.setImageUrl("http://sharesdk.cn");
    oks.setPlatform(ss);
    oks.show(getActivity());
  }

  public static MyselfFragment newInstance(Context context) {
    MyselfFragment fragment = new MyselfFragment();
    fragment.mContext = context;
    return fragment;
  }

  public MyselfFragment() {

    // Required empty public constructor

  }


  @Override
  protected void processData(InformmationData response) {
    super.processData(response);
    if (response != null) {


    }

  }

  @Override
  protected void errorData(VolleyError volleyError) {
    super.errorData(volleyError);
    ExtUtils.errorLog("-----MyselfFragment----volleyError--------->", volleyError + "");
  }

  @Override
  protected String getRequestUrl() {
    return Urls.URL_PREFIX + "";
  }

  @Override
  protected Class<InformmationData> getResponseDataClass() {
    return InformmationData.class;
  }

  @Override
  protected Map<String, String> getParamMap() {
    return null;
  }

}

BottomMenu

package io.dcloud.H5B79C397.activity;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import io.dcloud.H5B79C397.R;

import static io.dcloud.H5B79C397.R.id.btn1;
import static io.dcloud.H5B79C397.R.id.btn2;

/**
 * Created by Administrator on 2017/7/21 0021.
 */

public class BottomMenu implements View.OnClickListener,View.OnTouchListener {
  private PopupWindow popupWindow;
  private Button wechat, friend,qq,sina,more ,btnCancel;
  private View mMenuView;
  private Activity mContext;
  private View.OnClickListener clickListener;

  public BottomMenu(Context context, View.OnClickListener clickListener) {
    LayoutInflater inflater = LayoutInflater.from(context);
    this.clickListener=clickListener;
    mContext= (Activity) context;
    mMenuView = inflater.inflate(R.layout.layout_popwindow, null);
    wechat= (Button) mMenuView.findViewById(btn1);
    friend= (Button) mMenuView.findViewById(btn2);
    qq=(Button)mMenuView.findViewById(R.id.QQ) ;
    sina=(Button)mMenuView.findViewById(R.id.sina);
    more=(Button)mMenuView.findViewById(R.id.more);
    btnCancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
    btnCancel.setOnClickListener(this);
    wechat.setOnClickListener(this);
    friend.setOnClickListener(this);
    qq.setOnClickListener(this);
    sina.setOnClickListener(this);
    more.setOnClickListener(this);
    popupWindow=new PopupWindow(mMenuView, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT,true);
    popupWindow.setAnimationStyle(R.style.popwin_anim_style);
    ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.apha));
    popupWindow.setBackgroundDrawable(dw);
    mMenuView.setOnTouchListener(this);
  }
  /**
   * 显示菜单
   */
  public void show(){
    //得到当前activity的rootView
    View rootView=((ViewGroup)mContext.findViewById(android.R.id.content)).getChildAt(0);
    popupWindow.showAtLocation(rootView, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
  }
  @Override
  public void onClick(View view) {
    popupWindow.dismiss();
    switch (view.getId()) {
      case R.id.btn_cancel:
        break;
      default:
        clickListener.onClick(view);
        break;
    }
  }
  @Override
  public boolean onTouch(View arg0, MotionEvent event) {
    int height = mMenuView.findViewById(R.id.pop_layout).getTop();
    int y=(int) event.getY();
    if(event.getAction()==MotionEvent.ACTION_UP){
      if(y<height){
        popupWindow. dismiss();
      }
    }
    return true;
  }

}

layout_popwindow.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:gravity="center_horizontal"
  android:orientation="vertical" >

  <LinearLayout
    android:id="@+id/pop_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center_horizontal"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:orientation="vertical" >

    <Button
      android:background="#F2F2F2"
      android:id="@+id/btn1"
      android:layout_width="match_parent"
      android:layout_height="40dp"
      android:text="分享到微信好友"
      android:textColor="@color/black"
       />

    <Button
      android:id="@+id/btn2"
      android:layout_width="match_parent"
      android:layout_height="40dp"
      android:text="分享到微信朋友圈"
      android:layout_marginTop="5dp"
      android:textColor="@color/black"
      android:background="#F2F2F2"
      />
    <Button
      android:id="@+id/QQ"
      android:layout_width="match_parent"
      android:layout_height="40dp"
      android:text="分享到QQ"
      android:layout_marginTop="5dp"
      android:textColor="@color/black"
      android:background="#F2F2F2"
      />
    <Button
      android:id="@+id/sina"
      android:layout_width="match_parent"
      android:layout_height="40dp"
      android:text="分享到新浪微博"
      android:layout_marginTop="5dp"
      android:textColor="@color/black"
      android:background="#F2F2F2"
      />
    <Button
      android:id="@+id/more"
      android:layout_width="match_parent"
      android:layout_height="40dp"
      android:text="分享到更多..."
      android:layout_marginTop="5dp"
      android:textColor="@color/black"
      android:background="#F2F2F2"
      />


    <Button
      android:id="@+id/btn_cancel"
      android:layout_width="match_parent"
      android:layout_height="50dp"
      android:text="取消"
      android:layout_marginBottom="10dp"
      android:layout_marginTop="15dp"
      android:background="#F2F2F2"
      android:alpha="100"
      android:textStyle="bold" />
  </LinearLayout>
</RelativeLayout>

包anim//放在res目录下其中包括ppwindow_hide_anim.xml和ppwindow_show_anim.xml
其中ppwindow_hide_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate
    android:duration="300"
    android:fromXDelta="0"
    android:toXDelta="0"
    android:fromYDelta="0"
    android:toYDelta="1920"
    />
  <alpha
    android:duration="300"
    android:fromAlpha="1"
    android:toAlpha="0"
    />

</set>

其中ppwindow_show_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate
    android:duration="300"
    android:fromXDelta="0"
    android:toXDelta="0"
    android:fromYDelta="1920"
    android:toYDelta="0"
    />
  <alpha
    android:duration="300"
    android:fromAlpha="0"
    android:toAlpha="1"
    />

</set>

最后上图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。