仿京东淘宝等首页广告弹窗广告 dialog

记录贴 防止以后忘记.


上图



1.dialog布局 图片随便找的

<?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">

    <TextView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/d"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"/>

</RelativeLayout>

2.style 

    <!-- dialog动画 -->
    <style name="style_dialog">
        <item name="android:windowEnterAnimation">@anim/anim_popup_zoom_enter</item>
        <item name="android:windowExitAnimation">@anim/anim_popup_zoom_exit</item>
    </style>


3.俩个动画 一进一出

(1) anim_popup_zoom_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale
        android:duration="500"
        android:fromXScale="0.8"
        android:fromYScale="0.8"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />

    <alpha
        android:duration="500"
        android:fromAlpha="0"
        android:toAlpha="1.0" />
</set>
(2) anim_popup_zoom_exit.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="300"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.8"
        android:toYScale="0.8" />
    <alpha
        android:duration="300"
        android:fromAlpha="1.0"
        android:toAlpha="0" />
</set>


4.主体代码

/**
 * Created by lpq on 2017/7/18.
 * 仿京东淘宝首页广告
 */

public class AdsDialog  extends Dialog implements View.OnClickListener {

    private TextView tv_img;

    public AdsDialog(@NonNull Context context) {
        super(context);
        setContentView(R.layout.dialog_ads);
        setCanceledOnTouchOutside(true);
        tv_img = (TextView) findViewById(R.id.img);
        tv_img.setOnClickListener(this);
    }

    public void showDialog() {
        Window window = getWindow();
        window.setWindowAnimations(R.style.style_dialog);
        window.setBackgroundDrawableResource(R.color.colorTransparent);
        WindowManager.LayoutParams wl = window.getAttributes();
        //设置弹窗位置
        wl.gravity = Gravity.CENTER;
        window.setAttributes(wl);
        show();
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.img) {
//            closeAnimations();
            dismiss();
        }
    }

}

5.调用

(1) kotlin

class TestActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)
        val dialog = AdsDialog(this)
        test_tv.setOnClickListener(object : NoDoubleListener() {
            override fun onNoDoubleClick(v: View) {
                dialog.showDialog()
            }
        })
    }
}
(2) 一般调用

public class DemoActivity extends AppCompatActivity {
    private AdsDialog dialog;
    private  TextView tv;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        tv = (TextView) findViewById(R.id.tv_btn);
        dialog = new AdsDialog(this);
        tv.setOnClickListener(new NoDoubleListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                dialog.showDialog();
            }
        });
    }
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值