pop菜单

本文介绍如何在Android应用中使用PopupWindow并为其添加动画效果,包括定义动画、设置动画样式及响应触摸事件等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.mainActivity

 

package com.example.topmenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

public class MainActivity extends Activity {

 private Button btn;
 private PopupWindow pop;
 private View line;
 
 //这是pop种的空间
 private LinearLayout vi;
 private Animation animation;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  btn = (Button) findViewById(R.id.button1);

  //这是头部的分割线
  line = (View) findViewById(R.id.line);

  animation = new TranslateAnimation(0, 0, -60f, 0);

  animation.setDuration(1000);

  btn.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

    getPopupWindow();

    pop.showAsDropDown(line);

   }
  });
 }

 // 获取popupwindow实例
 private void getPopupWindow() {

  if (null != pop) {
   pop.dismiss();
   return;

  } else {
   initPopWindow();
  }
 }

 public void initPopWindow() {

  View popupWindow_view = getLayoutInflater().inflate(R.layout.pop, null);

  vi = (LinearLayout) popupWindow_view.findViewById(R.id.head);

  int width = getWindowManager().getDefaultDisplay().getWidth();

  int height = getWindowManager().getDefaultDisplay().getHeight();
  // 创建popupwindow实例
  pop = new PopupWindow(popupWindow_view, width, height, true);
  // 设置动画效果
  pop.setAnimationStyle(R.style.AnimationAlpha);
  //这是在pop中的某个控件添加效果
  vi.startAnimation(animation);
  // 不设置当前pop不能点击,一定的记住
  pop.setFocusable(true);
  // 点击其他地方消失
  popupWindow_view.setOnTouchListener(new OnTouchListener() {

   @Override
   public boolean onTouch(View v, MotionEvent event) {
    if (pop != null && pop.isShowing()) {
     pop.dismiss();
     pop = null;
    }
    return false;
   }
  });

 }
}

 

对于上面的动画,这个可以在anim中定义相关的文件,然后再style中设置相关的效果

<style name="AnimationFade">

        <!-- PopupWindow左右弹出的效果 -->
        <item name="android:windowEnterAnimation">@anim/in_lefttoright</item>
        <item name="android:windowExitAnimation">@anim/in_righttoleft</item>
    </style>

上面的两个">@anim/in_lefttoright,">@anim/in_lefttoright

1.   <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 定义从左向右进入的动画 -->
    <translate
        android:duration="500"
        android:fromXDelta="-100%"
        android:toXDelta="0" />

</set>

 

2.    <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 定义从右向左动画退出动画 -->
    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="-100%" />

</set>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值