广告的显示和关闭

app或游戏的主页显示广告页面,实现方式:

复制代码
public class MainActivity extends Activity implements View.OnClickListener{

    private Button btnShowAd;
    private RelativeLayout layoutAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView(){
        btnShowAd = (Button)findViewById(R.id.btnShowAd);
        btnShowAd.setOnClickListener(this);
    }

    private RelativeLayout createLayout(){
        final ImageView imgAd = new ImageView(this);
        imgAd.setImageResource(R.mipmap.pic22);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int width = (int)(metrics.widthPixels*0.7f);
        int height = (int)(metrics.heightPixels*0.7f);
        final RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(width, height);
        params1.addRule(RelativeLayout.CENTER_IN_PARENT);
        imgAd.setLayoutParams(params1);

        imgAd.requestLayout();

        final ImageView imgClose = new ImageView(this);
        imgClose.setImageResource(R.mipmap.close);
        int width2 = (int)(width*0.1f);
        int height2 = (int)(height*0.1f);
        RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(width2, height2);
        params2.leftMargin = metrics.widthPixels/2 + width/2 - width2 - 10;
        params2.topMargin = metrics.heightPixels/2 - height/2 + (2*height2)/3;
        imgClose.setLayoutParams(params2);
        imgClose.setClickable(true);
        imgClose.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                ViewParent parent = imgClose.getParent();
                if(parent != null){
                    layoutAd.setVisibility(View.GONE);
                    for(int i=0; i<layoutAd.getChildCount(); ++i){
                        View view = layoutAd.getChildAt(i);
                        view.setVisibility(View.GONE);
                    }
                }
                Toast.makeText(MainActivity.this, "close", Toast.LENGTH_SHORT).show();
            }
        });

        RelativeLayout layout = new RelativeLayout(this);
//        layout.setBackgroundColor(0xffff0000);
        layout.addView(imgAd);
        layout.addView(imgClose);
        addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        return layout;
    }

    private void showAd(){
        if(layoutAd == null){
            layoutAd = createLayout();
        }

        layoutAd.setVisibility(View.VISIBLE);
        for(int i=0; i<layoutAd.getChildCount(); ++i){
            View view = layoutAd.getChildAt(i);
            view.setVisibility(View.VISIBLE);
        }

        ScaleAnimation animation = new ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(600);
        animation.setFillAfter(true);
        layoutAd.startAnimation(animation);
    }

    @Override
    public void onClick(View v) {
        if(v == btnShowAd){
            showAd();
        }
    }
复制代码

 

posted @   丛林小阁楼  阅读(245)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示