仿支付宝广告-顶部往下掉的popwindow,消失在左上角
最近玩支付宝,有时候打开时,会从顶部掉下一个营销的广告窗口,效果图如下
分析:
1.从顶部往下掉,一个位移动画
背景半透明
首页构建广告界面
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#78666666"> <FrameLayout android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/adArea" android:orientation="vertical" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/closeIcon" android:src="@drawable/close" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/adContent" android:src="@drawable/adcontent" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </FrameLayout> </FrameLayout>
用到的动画,顶部掉下来fade_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="1000" android:fromYDelta="-100%" android:toYDelta="0" /> <alpha android:startOffset="500" android:fromAlpha="0" android:toAlpha="1" android:duration="1000"/> </set>
退出时动画fade_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" android:fillBefore="false"> <translate android:duration="2000" android:fromYDelta="0" android:toYDelta="-100%" /> <scale android:startOffset="800" android:pivotX="0%" android:pivotY="0%" android:fromXScale="1" android:toXScale="0" android:fromYScale="1" android:duration="1000" android:toYScale="0"> </scale> </set>
启动Activity
package com.example.exportgradledemo; import android.app.Activity; import android.content.Context; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.view.WindowManager; import android.widget.Button; import android.widget.PopupWindow; public class Main3Activity extends Activity { private android.widget.Button button; private PopupWindow popupWindow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); this.button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showPopupWindow(); } }); } /** * 微信分享弹框 */ private void showPopupWindow() { if (popupWindow == null) { LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = layoutInflater.inflate(R.layout.popad_layout, null); popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); } popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); popupWindow.setAnimationStyle(R.style.AnimationPreview); // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景 popupWindow.setBackgroundDrawable(new BitmapDrawable()); WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); popupWindow.showAtLocation(findViewById(R.id.screen_content_layout), Gravity.BOTTOM, 0, 0); } }