首页 > 代码库 > 广告的显示和关闭

广告的显示和关闭

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();
        }
    }

 

广告的显示和关闭