Android自定义PopWindow带动画向下弹出效果

本文实例为大家分享了PopWindow实现带动画向下弹出效果的具体代码,供大家参考,具体内容如下

首先建一个popwin的实体类

package dmpte.mytest;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

public class PopWin extends PopupWindow {
 private Context mContext;
 private View view;

 public PopWin(final Context mContext, View.OnClickListener itemsOnClick, int flag) {
  this.mContext = mContext;
  this.view = LayoutInflater.from(mContext).inflate(R.layout.view_popwin, null);
  // 设置外部可点击
  this.setOutsideTouchable(true);
  /* 设置弹出窗口特征 */
  // 设置视图
  this.setContentView(this.view);
  // 设置弹出窗体的宽和高
  this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);//高
  this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);//宽

  // 设置弹出窗体可点击
  this.setFocusable(true);

  // 设置弹出窗体显示时的动画,从底部向上弹出
  this.setAnimationStyle(R.style.take_photo_anim);
//  mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
  this.view.setOnTouchListener(new View.OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    int height = view.findViewById(R.id.pop_layout).getHeight();
    int y = (int) event.getY();
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
    //Y表示手指点击的位置,屏幕顶端为0,往下一次递增。height是popwin的高度。y > height就表示手指点在popwin的外面,然后关闭popwin
     if (y > height) {
      dismiss();
     }
    }
    return true;
   }

  });

 }

}

然后是这个类的布局 view_popwin.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/pop_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@null"
 android:orientation="vertical">

 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="170dp"
  android:background="#ffff"
  android:orientation="vertical">

  <TextView
   android:id="@+id/tv_jingtai"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_gravity="center"
   android:layout_marginTop="2dp"
   android:gravity="center"
   android:text="移动静态"
   android:textColor="#f123" />

 </LinearLayout>
</LinearLayout>

接下来是这个类里涉及的动画 popwin_anim,在res/values/styles下

<style name="popwin_anim" parent="android:Animation">
    <item name="android:windowEnterAnimation">@anim/pop_enter_anim</item>
    <item name="android:windowExitAnimation">@anim/pop_exit_anim</item>
</style>

然后是进场动画 pop_enter_anim和出场动画 pop_exit_anim,在res下建一个文件夹anim,分别新建上面两个xml

pop_enter_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:shareInterpolator="false">
 <!-- 平移动画 -->
 <translate
  android:duration="500"
  android:fromYDelta="-100%p"
  android:toYDelta="0" />
</set>

pop_exit_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:shareInterpolator="false">
 <!-- 平移动画 -->
 <translate
  android:duration="1000"
  android:fromYDelta="0"
  android:toYDelta="-100%p" />

</set>

最后是使用

//让背景变暗
 WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.alpha = 0.7f;
    getWindow().setAttributes(lp);
    //弹出窗体
    PopWin popWin_ = new PopWin(this, null, 0);
    popWin_.showAsDropDown(findViewById(R.id.relativeLayout));
    //监听popwin是否关闭,关闭的话让背景恢复
    popWin_.setOnDismissListener(new PopupWindow.OnDismissListener() {
     @Override
     public void onDismiss() {
      WindowManager.LayoutParams lp = getWindow().getAttributes();
      lp.alpha = 1f;
      getWindow().setAttributes(lp);
  }
});

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

(0)

相关推荐

  • Android Popwindow弹出框的模板使用示例

    Pop弹出框 public class ProductSortPop { private Context mContext; private View.OnClickListener mOnClickListener; //创建一个点击事件接口回调数据 private TextView mAllSort; private TextView mNewSort; private TextView mCommentSort; private View mLeftV; private PopupWind

  • android popwindow实现左侧弹出菜单层及PopupWindow主要方法介绍

    PopupWindow可以实现浮层效果,主要方法有:可以自定义view,通过LayoutInflator方法:可以出现和退出时显示动画:可以指定显示位置等. 为了将PopupWindow的多个功能展现并力求用简单的代码实现,编写了一个点击按钮左侧弹出菜单的功能,实现出现和退出时显示动画效果并点击其他区域时弹出层自动消失,效果图如下: 源码: 1.PopwindowOnLeftActivity.java 复制代码 代码如下: package com.pop.main; import android

  • Android 中Popwindow弹出菜单的两种方法实例

    Android 中Popwindow弹出菜单的两种方法实例 1.popWindow就是对话框的一种方式! 此文讲解的android中对话框的一种使用方式,它叫popWindow. 2.popWindow的特性 Android的对话框有两种:PopupWindow和AlertDialog.它们的不同点在于: AlertDialog的位置固定,而PopupWindow的位置可以随意. AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的. PopupWindow的位置按照有无偏

  • Android自定义PopWindow实现QQ、微信弹出菜单效果

    前段时间在个人开发的项目中需要用到弹出菜单,类似QQ右上角的弹出菜单,自己使用popwin的次数也不是很多,其中也遇到过一点问题,今天正好有时间就把一些经验分享给大家. 先来看看最终实现过后的效果怎么样,下面放上图 自定义的弹出菜单是继承的popwin,并不是view 因为没有必要重复造车轮,如果想要实现某种特殊的效果另说.首先创建类MyPopWindow继承Popwindow. public class MyPopWindow extends PopupWindow implements Vi

  • Android自定义PopWindow带动画向下弹出效果

    本文实例为大家分享了PopWindow实现带动画向下弹出效果的具体代码,供大家参考,具体内容如下 首先建一个popwin的实体类 package dmpte.mytest; import android.content.Context; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.widget.PopupWindow; i

  • Bootstrap实现带动画过渡的弹出框

    先看看效果图: 代码: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>带动画过的渡弹出框</title> <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css&quo

  • Android使用popUpWindow带遮罩层的弹出框

    上次项目中实现了新功能,就一直想添加到博客里来着,惰性发作起来简直太可怕,不说了,跟着一起写吧,三步即可实现简单的弹出框功能,首先看效果-- 首先:主页面布局,触发控件一定要有,再有就是给根标签设置id <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:

  • Android开发Popwindow仿微信右上角下拉菜单实例代码

    先给大家看下效果图: MenuPopwindow: package com.cloudeye.android.cloudeye.view; import android.app.Activity; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.view.LayoutInflater; import android.view.View; import an

  • Android自定义水波纹动画Layout实例代码

    话不多说,我们先来看看效果: Hi前辈搜索预览 这一张是<Hi前辈>的搜索预览图,你可以在这里下载这个APP查看更多效果: http://www.wandoujia.com/apps/com.superlity.hiqianbei LSearchView 这是一个MD风格的搜索框,集成了ripple动画以及search时的loading,使用很简单,如果你也需要这样的搜索控件不妨来试试:https://github.com/onlynight/LSearchView RippleEverywh

  • Android 自定义加载动画Dialog弹窗效果的示例代码

    效果图 首先是创建弹窗的背景 这是上面用到的 以shape_bg_5_blue.xml为例,其他的三个无非就是里面的颜色不一样而已 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp"

  • Android 自定义圆形带刻度渐变色的进度条样式实例代码

    效果图 一.绘制圆环 圆环故名思意,第一个首先绘制是圆环 1:圆环绘制函数 圆环API public void drawArc (RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint) 参数说明 oval:圆弧所在的椭圆对象. startAngle:圆弧的起始角度. sweepAngle:圆弧的角度. useCenter:是否显示半径连线,true表示显示圆弧与圆心的半径连线,false表示不

  • android自定义环形统计图动画

    本文实例为大家分享了android自定义环形统计图动画的具体代码,供大家参考,具体内容如下 一.测试截图 二.实现原理  package com.freedomanlib; import java.util.Timer; import java.util.TimerTask; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import

  • Android开发仿QQ空间根据位置弹出PopupWindow显示更多操作效果

    我们打开QQ空间的时候有个箭头按钮点击之后弹出PopupWindow会根据位置的变化显示在箭头的上方还是下方,比普通的PopupWindow弹在屏幕中间显示好看的多. 先看QQ空间效果图: 这个要实现这个效果可以分几步进行 1.第一步自定义PopupWindow,实现如图的样式,这个继承PopupWindow自定义布局很容易实现 2.得到点击按钮的位置,根据位置是否在屏幕的中间的上方还是下方,将PopupWindow显示在控件的上方或者下方 3.适配问题,因为PopupWindow上面的操作列表

随机推荐