Android自定義PopWindow帶動畫向下彈出效果
更新時間:2021年09月09日 14:19:21 作者:獄火蒼穹
這篇文章主要為大家詳細介紹了Android自定義PopWindow帶動畫向下彈出效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了PopWindow實現(xiàn)帶動畫向下彈出效果的具體代碼,供大家參考,具體內(nèi)容如下
首先建一個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監(jiān)聽判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框
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="移動靜態(tài)" 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));
//監(jiān)聽popwin是否關閉,關閉的話讓背景恢復
popWin_.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1f;
getWindow().setAttributes(lp);
}
});
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android自定義控件之可拖動控制的圓環(huán)控制條實例代碼
這篇文章主要介紹了Android自定義控件之可拖動控制的圓環(huán)控制條實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04

