亲宝软件园·资讯

展开

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

狱火苍穹 人气:0

首先建一个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);
  }
});

加载全部内容

相关教程
猜你喜欢
用户评论