首页 > 代码库 > 广告的显示和关闭
广告的显示和关闭
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(); } }
广告的显示和关闭
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。