Android Custom PopWindow Drive Down Pop up Effect

  • 2021-10-25 07:51:05
  • OfStack

In this paper, we share the specific code of PopWindow to drive down the pop-up effect for your reference, the specific contents are as follows

First, build an entity class of 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);
  //  Set externally clickable 
  this.setOutsideTouchable(true);
  /*  Set pop-up window characteristics  */
  //  Setting View 
  this.setContentView(this.view);
  //  Set the width and height of the pop-up form 
  this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);// Gao 
  this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);// Width 

  //  Set the pop-up form to click 
  this.setFocusable(true);

  //  Set the animation when the pop-up form is displayed, and pop it up from the bottom 
  this.setAnimationStyle(R.style.take_photo_anim);
//  mMenuView Add OnTouchListener Monitor and judge to obtain the touch screen position. If it is outside the selection box, destroy the pop-up box 
  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 Indicates the position where the finger clicks, and the top of the screen is 0 , down 1 Incremental times. height Yes popwin The height of. y > height It means that the finger point is popwin Outside, and then close it popwin
     if (y > height) {
      dismiss();
     }
    }
    return true;
   }

  });

 }

}

Then there is the layout of this class 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=" Moving static state "
   android:textColor="#f123" />

 </LinearLayout>
</LinearLayout>

Next is the animation involved in this class, popwin_anim, under 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>

Then there are entry animation pop_enter_anim and exit animation pop_exit_anim. Create a folder anim under res, and create the above two xml respectively

pop_enter_anim.xml


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:shareInterpolator="false">
 <!--  Translation animation  -->
 <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">
 <!--  Translation animation  -->
 <translate
  android:duration="1000"
  android:fromYDelta="0"
  android:toYDelta="-100%p" />

</set>

Finally, use the


// Darken the background 
 WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.alpha = 0.7f;
    getWindow().setAttributes(lp);
    // Pop-up form 
    PopWin popWin_ = new PopWin(this, null, 0);
    popWin_.showAsDropDown(findViewById(R.id.relativeLayout));
    // Eavesdropping popwin Whether to close or not, if closed, let the background recover 
    popWin_.setOnDismissListener(new PopupWindow.OnDismissListener() {
     @Override
     public void onDismiss() {
      WindowManager.LayoutParams lp = getWindow().getAttributes();
      lp.alpha = 1f;
      getWindow().setAttributes(lp);
  }
});

Related articles: