android 向下动画,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

android:id="@+id/pop_layout"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@null"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="170dp"

android:background="#ffff"

android:orientation="vertical">

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" />

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

@anim/pop_enter_anim

@anim/pop_exit_anim

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

pop_enter_anim.xml

android:shareInterpolator="false">

android:duration="500"

android:fromYDelta="-100%p"

android:toYDelta="0" />

pop_exit_anim.xml

android:shareInterpolator="false">

android:duration="1000"

android:fromYDelta="0"

android:toYDelta="-100%p" />

最后是使用

//让背景变暗

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值