您好,登錄后才能下訂單哦!
這篇文章主要介紹了Android怎么實(shí)現(xiàn)仿網(wǎng)易嚴(yán)選底部彈出菜單效果的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Android怎么實(shí)現(xiàn)仿網(wǎng)易嚴(yán)選底部彈出菜單效果文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
首先展示效果圖如下:
是不是還可以呢,由于代碼量不多卻注釋詳細(xì),所以先貼出代碼再一一詳說(shuō):
BottomPopupWindowView類:
public class BottomPopupWindowView extends LinearLayout{ private AnimatorListener animatorListener; //底部?jī)?nèi)容的View private FrameLayout base_view; //內(nèi)容的View private FrameLayout content_view; //背景的View private RelativeLayout popup_bg; //xml加載的View private View bottomPopouView; //外部加載的內(nèi)容View private View contentView; //外部加載的底部?jī)?nèi)容View private View baseView; //手勢(shì)的最小值 private float minVelocity=0; //加載一次的判斷值 private boolean mDrawable=true; public void setAnimatorListener(AnimatorListener animatorListener) { this.animatorListener = animatorListener; } public void setBaseView(View baseView){ this.baseView=baseView; } public void setContextView(View view){ this.contentView=view; } public void setContentView(int id){ this.contentView=LayoutInflater.from(getContext()).inflate(id,null); } public BottomPopupWindowView(Context context) { this(context,null); } public BottomPopupWindowView(Context context, @Nullable AttributeSet attrs) { this(context,attrs,0); } public BottomPopupWindowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //初始化各種數(shù)值 minVelocity=ViewConfiguration.get(getContext()).getScaledTouchSlop(); bottomPopouView= LayoutInflater.from(getContext()).inflate(R.layout.layout_bottom_popup,null); base_view=(FrameLayout)bottomPopouView.findViewById(R.id.bottom_view); content_view=(FrameLayout)bottomPopouView.findViewById(R.id.content_view); popup_bg=(RelativeLayout)bottomPopouView.findViewById(R.id.popup_bg); //把整個(gè)View都加載在LinearLayout里以顯示出來(lái) addView(bottomPopouView); //背景顏色監(jiān)聽(tīng) popup_bg.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { disMissPopupView(); } }); //屏蔽內(nèi)容區(qū)域點(diǎn)擊事件 content_view.setOnClickListener(new OnClickListener() { @Override public void onClick(View view){} }); //屏蔽底部?jī)?nèi)容區(qū)域點(diǎn)擊事件 base_view.setOnClickListener(new OnClickListener() { @Override public void onClick(View view){} }); //內(nèi)容區(qū)域判斷是否向下,手勢(shì)向下就關(guān)閉彈框 content_view.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { float y1=0,y2=0; if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) { y1 = motionEvent.getY(); } if(motionEvent.getAction() == MotionEvent.ACTION_UP){ y2 = motionEvent.getY(); if((y2-y1)>minVelocity){ disMissPopupView(); } } return false; } }); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if(mDrawable&&baseView!=null){ //剛開(kāi)始加載底部?jī)?nèi)容區(qū)域,只需一次就行,多次報(bào)錯(cuò) base_view.addView(baseView); mDrawable=false; } } public void showPopouView(){ if(contentView!=null){ //開(kāi)始動(dòng)畫(huà)數(shù)據(jù) startAnimation(); //開(kāi)啟背景顏色的漸變動(dòng)畫(huà) popup_bg.setVisibility(View.VISIBLE); popup_bg.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.bp_bottom_bg_in)); //把這個(gè)區(qū)域全部顯示出來(lái) ((BottomPopupWindowView)this).setLayoutParams(new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT)); //假如內(nèi)容區(qū)域 content_view.addView(contentView,0); content_view.setVisibility(View.VISIBLE); //開(kāi)啟內(nèi)容區(qū)域動(dòng)畫(huà) content_view.setAnimation(AnimationUtils.loadAnimation(getContext(),R.anim.bp_bottom_view_in)); } } public void disMissPopupView(){ //開(kāi)始關(guān)閉動(dòng)畫(huà)數(shù)據(jù) endAnimation(); //開(kāi)啟內(nèi)容區(qū)域動(dòng)畫(huà) content_view.setVisibility(View.GONE); Animation animation=AnimationUtils.loadAnimation(getContext(),R.anim.bp_bottom_view_out); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) {} @Override public void onAnimationRepeat(Animation animation) {} @Override public void onAnimationEnd(Animation animation) { //等內(nèi)容區(qū)域動(dòng)畫(huà)結(jié)束后,清楚所有View content_view.removeAllViews(); //開(kāi)啟背景顏色的漸變動(dòng)畫(huà) popup_bg.setVisibility(View.GONE); popup_bg.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.bp_bottom_bg_out)); //把整個(gè)控件的大小恢復(fù)到底部View區(qū)域的大小 RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT,getViewHeight((BottomPopupWindowView)BottomPopupWindowView.this)); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,-1); ((BottomPopupWindowView)BottomPopupWindowView.this).setLayoutParams(layoutParams); } }); //開(kāi)始動(dòng)畫(huà) content_view.setAnimation(animation); } //獲取View的高度 public int getViewHeight(View view){ int width =View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); int height =View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); view.measure(width,height); return view.getMeasuredHeight(); } //開(kāi)始動(dòng)畫(huà)數(shù)據(jù)變化 public void startAnimation(){ ValueAnimator valueAnimator = ValueAnimator.ofInt(0,40); valueAnimator.setDuration(250); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { if(animatorListener!=null){ animatorListener.startValue((int) valueAnimator.getAnimatedValue()); } } }); valueAnimator.start(); } //結(jié)束動(dòng)畫(huà)數(shù)值變化 public void endAnimation() { ValueAnimator valueAnimator = ValueAnimator.ofInt(40,0); valueAnimator.setDuration(250); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { if(animatorListener!=null){ animatorListener.endValue((int) valueAnimator.getAnimatedValue()); } } }); valueAnimator.start(); } }
對(duì)應(yīng)的加載的xml布局是:
layout_bottom_popou.xml如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#707A7A7A"> <RelativeLayout android:id="@+id/popup_bg" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#707A7A7A" android:layout_above="@+id/bottom_view"></RelativeLayout> <FrameLayout android:id="@+id/content_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/bottom_view" android:orientation="horizontal"> </FrameLayout> <FrameLayout android:id="@+id/bottom_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"></FrameLayout> </RelativeLayout>
1.在BottomPopupWindowView是繼承LinearLayout,而layout_bottom_popou.xml是這整個(gè)BottomPopupWindowView里的骨架,然后在BottomPopupWindowView初始化的時(shí)候通過(guò)addView()來(lái)加載整個(gè)骨架布局。在onDraw()里只需加載一次baseView就可以了,不然后重復(fù)加載導(dǎo)致報(bào)錯(cuò)。這樣就初始化成功了,剛開(kāi)始只會(huì)加載baseView的界面,就相當(dāng)于嚴(yán)選最下面的購(gòu)物車立即購(gòu)買(mǎi)等界面。
2.當(dāng)調(diào)用showPopouView()時(shí)顯示菜單的。startAnimation()方法只是為了產(chǎn)生動(dòng)畫(huà)的數(shù)據(jù)。
popup_bg.setVisibility(View.VISIBLE); popup_bg.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.bp_bottom_bg_in));
只是為了開(kāi)啟背景漸變的動(dòng)畫(huà)沒(méi)什么說(shuō)的。最重要的是顯示菜單實(shí)現(xiàn)是把BottomPopupWindowView的大小擴(kuò)展到全屏,所以設(shè)置((BottomPopupWindowView)this).setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT));,然后把彈出菜單的View即contentView裝進(jìn)content_view即可,然后開(kāi)啟彈出動(dòng)畫(huà)就實(shí)現(xiàn)了。
3.最后是disMissPopupView()方法關(guān)閉彈窗。endAnimation()方法只是為了產(chǎn)生動(dòng)畫(huà)的數(shù)據(jù)。再啟動(dòng)內(nèi)容域View即content_View的退出動(dòng)畫(huà),在動(dòng)畫(huà)結(jié)束后用content_view.removeAllViews();
起初菜單內(nèi)容,再像上面一樣開(kāi)啟背景顏色漸變動(dòng)畫(huà),最后只需使BottomPopupWindowView恢復(fù)原來(lái)的baseView的大小及可以了,具體如下:
RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT,getViewHeight((BottomPopupWindowView)BottomPopupWindowView.this)); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,-1); ((BottomPopupWindowView)BottomPopupWindowView.this).setLayoutParams(layoutParams);
這就是核心的代碼功能了,代碼量不多具體細(xì)節(jié)看上面的源碼。
有人或許會(huì)問(wèn)返回動(dòng)畫(huà)的數(shù)據(jù)有什么用,很簡(jiǎn)單就是為了實(shí)現(xiàn)嚴(yán)選菜單框出來(lái)時(shí)整個(gè)上面詳情的縮放。具體看如下demo,首先給出界面xml,如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/main_view" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorAccent" android:orientation="vertical"> <ImageView android:id="@+id/banner_img" android:layout_width="match_parent" android:layout_height="300dp" android:scaleType="fitXY" android:src="@mipmap/banner"/> <View android:layout_width="match_parent" android:layout_height="0.1dp" android:background="@color/colorPrimary"></View> <RelativeLayout android:id="@+id/guige" android:layout_width="match_parent" android:layout_height="50dp" android:background="#ffffff"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="15dp" android:textSize="15dp" android:text="規(guī)格數(shù)量選擇"/> <ImageView android:layout_width="20dp" android:layout_height="20dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:src="@mipmap/ic_jiantou"/> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="0.1dp" android:background="@color/colorPrimary"></View> </LinearLayout> <com.jack.bottompopupwindowview.BottomPopupWindowView android:id="@+id/bottom_popup" android:layout_width="match_parent" android:layout_height="50dp" android:background="@android:color/transparent" android:layout_alignParentBottom="true"> </com.jack.bottompopupwindowview.BottomPopupWindowView> </RelativeLayout>
這就是上面效果圖的界面布局,沒(méi)什么可以說(shuō)的,再看事例代碼如下:
public class MainActivity extends AppCompatActivity implements View.OnClickListener, AnimatorListener { private BottomPopupWindowView bottomPopupWindowView; private View contentView; private View bottomView; private LinearLayout mainView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mainView=(LinearLayout)findViewById(R.id.main_view); bottomView=LayoutInflater.from(this).inflate(R.layout.layout_bottom_view,null); (bottomView.findViewById(R.id.promptly_buy)).setOnClickListener(this); (findViewById(R.id.guige)).setOnClickListener(this); bottomPopupWindowView=(BottomPopupWindowView)findViewById(R.id.bottom_popup); bottomPopupWindowView.setOnClickListener(this); bottomPopupWindowView.setBaseView(bottomView); contentView=LayoutInflater.from(this).inflate(R.layout.layout_content_view,null); bottomPopupWindowView.setContextView(contentView); (contentView.findViewById(R.id.ic_cancel)).setOnClickListener(this); bottomPopupWindowView.setAnimatorListener(this); } @Override public void onClick(View view) { switch(view.getId()){ case R.id.promptly_buy: case R.id.ic_cancel: bottomPopupWindowView.disMissPopupView(); break; case R.id.guige: bottomPopupWindowView.showPopouView(); break; } } @Override public void startValue(int value) { setMargins (mainView,value-10,value,value-10,value); } @Override public void endValue(int value) { setMargins (mainView,value,value,value,value); } public static void setMargins (View v, int l, int t, int r, int b) { if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) { ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams(); p.setMargins(l, t, r, b); v.requestLayout(); } } }
其中設(shè)置內(nèi)容菜單的View
BottomPopupWindowView.setContextView(bottomView);
設(shè)置沒(méi)有顯示菜單時(shí)候顯示的View(注:bottomView的高度要和BottomPopupWindowView的高度一樣,具體看demo)
BottomPopupWindowView.setBaseView(bottomView);
而回調(diào)的public void startValue(int value)和public void endValue(int value)設(shè)置動(dòng)畫(huà)監(jiān)聽(tīng)放回的數(shù)據(jù),以便根據(jù)數(shù)據(jù)實(shí)現(xiàn)動(dòng)畫(huà),嚴(yán)選的彈出和顯示商品詳情動(dòng)畫(huà)很簡(jiǎn)單就是不斷設(shè)設(shè)置View的間距就可以了。
關(guān)于“Android怎么實(shí)現(xiàn)仿網(wǎng)易嚴(yán)選底部彈出菜單效果”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Android怎么實(shí)現(xiàn)仿網(wǎng)易嚴(yán)選底部彈出菜單效果”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。