Android?PopUpWindow實(shí)現(xiàn)卡片式彈窗

首先自定義一個(gè)ViewGroup,繼承自LinerLayout,為了實(shí)現(xiàn)上下滑動(dòng)
//用于實(shí)現(xiàn)頂部彈窗動(dòng)畫(huà) 以及向上滑動(dòng)動(dòng)畫(huà)
public class TopTipsLinearLayout extends LinearLayout {
private static final String TAG = "Hyh";
private int mHeight;
private boolean mIsFirstLayout=true;
private boolean mIsPalyingAnimation=false;
private int mLastY=0;
private int mLastX=0;
private final int mTouchSlop = 4;
private final long SINGLE_CLICK_TIME = 300;
private long beginTiem=0;
private boolean mIsMoving=false;
public TopTipsLinearLayout(Context context) {
super(context);
}
public TopTipsLinearLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public TopTipsLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public TopTipsLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if(mIsFirstLayout) {
mHeight = getHeight();
mIsFirstLayout = false;
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mIsMoving = false;
beginTiem = System.currentTimeMillis();
break;
case MotionEvent.ACTION_MOVE:
int deltay = (int)event.getRawY() - mLastY;
if(!mIsPalyingAnimation) {
if(deltay < 0 || getTranslationY() + deltay <= 0) {
setTranslationY(getTranslationY() + deltay);
}
}
if(isMove(event.getRawX(), event.getRawY())) {
mIsMoving = true;
}
break;
case MotionEvent.ACTION_UP:
if(System.currentTimeMillis() - beginTiem <= SINGLE_CLICK_TIME && !mIsMoving) {
performClick();
}
mIsMoving = false;
if(Math.abs(getTranslationY()) <= (float) mHeight/3) {
setTranslationY(0);
} else {
fadeOutAnimator(200);
}
break;
}
mLastY = (int)event.getRawY();
mLastX = (int)event.getRawX();
return true;
}
private boolean isMove(float curX,float curY) {
return Math.abs(curX - mLastX) >= mTouchSlop || Math.abs(curY - mLastY) >= mTouchSlop;
}
public void showEnterAnimator(long time) {
setVisibility(VISIBLE);
//向下移動(dòng)動(dòng)畫(huà)
TranslateAnimation downTranslateAnimation=new TranslateAnimation(0,0,-mHeight, 0);
downTranslateAnimation.setDuration(time);
downTranslateAnimation.setFillAfter(true);
mIsPalyingAnimation = true;
startAnimation(downTranslateAnimation);
downTranslateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
mIsPalyingAnimation = false;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
public void fadeOutAnimator(long time) {
if(mIsMoving) {
postDelayed(new Runnable() {
@Override
public void run() {
fadeOutAnimator(300);
}
}, 1500);
return ;
}
//向上移動(dòng)動(dòng)畫(huà)
TranslateAnimation downTranslateAnimation=new TranslateAnimation(0,0,0, -mHeight - getTranslationY());
downTranslateAnimation.setDuration(time);
downTranslateAnimation.setFillAfter(true);
mIsPalyingAnimation = true;
startAnimation(downTranslateAnimation);
//動(dòng)畫(huà)監(jiān)聽(tīng)
downTranslateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation){
setVisibility(GONE); //動(dòng)畫(huà)結(jié)束 消除視圖
mIsPalyingAnimation = false;
}
@Override
public void onAnimationRepeat(Animation animation) {}
});
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
}
}
定義一個(gè)layout文件
<?xml version="1.0" encoding="utf-8"?>
<com.example.randfood.customview.TopTipsLinearLayout
android:id="@+id/top_tips_root"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:visibility="invisible">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="這是一個(gè)消息彈窗"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:padding="20dp"
android:background="@drawable/messageview_top_pop_bg"/>
</com.example.randfood.customview.TopTipsLinearLayout>
調(diào)用下面的方法即可
private static void initPopUpWindow(Context context, View parentView) {
View view = View.inflate(context, R.layout.messageview_top_pop, null);
PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, false);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupWindow.setOutsideTouchable(false);
//讓popupwindow可以顯示在狀態(tài)欄中
popupWindow.setClippingEnabled(false);
// popupWindow.setTouchable(true);
popupWindow.showAtLocation(parentView, Gravity.TOP, 0, 0);
TopTipsLinearLayout layout = view.findViewById(R.id.top_tips_root);
//讓viewgroup中的內(nèi)容顯示在狀態(tài)欄下面
layout.setPadding(0, DisplayUtil.getStatusBarHeight(context), 0, 0);
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "你點(diǎn)擊了頂部消息", Toast.LENGTH_SHORT).show();
}
});
layout.post(new Runnable() {
@Override
public void run() {
layout.showEnterAnimator(300);
}
});
layout.postDelayed(new Runnable() {
@Override
public void run() {
layout.fadeOutAnimator(300);
}
}, 5000);
}
到此這篇關(guān)于Android PopUpWindow實(shí)現(xiàn)卡片式彈窗的文章就介紹到這了,更多相關(guān)Android PopUpWindow彈窗內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android彈窗ListPopupWindow的簡(jiǎn)單應(yīng)用詳解
- Android使用 PopupWindow 實(shí)現(xiàn)底部彈窗功能
- Android開(kāi)發(fā)之PopupWindow創(chuàng)建彈窗、對(duì)話框的方法詳解
- Android PopupWindow實(shí)現(xiàn)左側(cè)彈窗效果
- Android編程實(shí)現(xiàn)的自定義彈窗(PopupWindow)功能示例
- Android控件PopupWindow模仿ios底部彈窗
- Android開(kāi)發(fā)之PopupWindow實(shí)現(xiàn)彈窗效果
相關(guān)文章
Android開(kāi)發(fā)自定義控件之折線圖實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Android開(kāi)發(fā)自定義控件之折線圖實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Android自定義控件中折線圖原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05
基于Android的英文詞典的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了基于Android的英文詞典的實(shí)現(xiàn)方法2016-05-05
Android編程之TextView的字符過(guò)濾功能分析
這篇文章主要介紹了Android編程之TextView的字符過(guò)濾功能,結(jié)合實(shí)例形式分析了TextView控件實(shí)現(xiàn)字符過(guò)濾的相關(guān)技巧與使用方法,需要的朋友可以參考下2016-01-01
Android應(yīng)用開(kāi)發(fā)的版本更新檢測(cè)升級(jí)功能實(shí)現(xiàn)示例
本文對(duì)Android版本更新的知識(shí)做全面的總結(jié),主要包括開(kāi)發(fā)中版本的設(shè)置,如何檢測(cè)本程序的版本,版本的更新判斷和顯示,新版本程序的安裝2022-04-04
android教程之使用popupwindow創(chuàng)建菜單示例
這篇文章主要介紹了android使用popupwindow創(chuàng)建菜單的示例,需要的朋友可以參考下2014-02-02
android組件SwipeRefreshLayout下拉小球式刷新效果
這篇文章主要為大家詳細(xì)介紹了android組件SwipeRefreshLayout下拉小球式刷新效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android Studio 4.0 正式發(fā)布在Ubuntu 20.04中安裝的方法
這篇文章主要介紹了Android Studio 4.0 正式發(fā)布如何在Ubuntu 20.04中安裝,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06

