Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android实现弹窗广告

Android实现自适应屏幕的弹窗广告

作者:Mr_Leixiansheng

这篇文章主要为大家详细介绍了Android实现自适应屏幕的弹窗广告,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android实现弹窗广告的具体代码,供大家参考,具体内容如下

内容:弹窗广告

特点:根据屏幕尺寸和原图片比例自适应

代码如下:

package com.example.leixiansheng.addialog;
 
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
 
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
 
/**
 * Created by Leixiansheng on 2018/6/28.
 */
 
public class AdDialog extends Dialog implements View.OnClickListener {
 
 private Context context;
 private float finalWidth; //最终宽度
 private float finalHeight; //最终高度
 private int padding = 50; //屏幕边距间隔
 private int picWidth; //图片真实宽度
 private int picHeight; //图片真实高度
 private String mUrl;
 
 
 public AdDialog(Context context,String url) {
 // 更改样式,把背景设置为透明的
 super(context, R.style.LocationDialogStyle);
 this.context = context;
 mUrl = url;
 }
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 requestWindowFeature(Window.FEATURE_NO_TITLE);
 setContentView(R.layout.dialog_ad);
 
 calculateHeightAndWidth();
 
 //初始化布局的位置
 initLayoutParams();
 setCancelable(false);
 setCanceledOnTouchOutside(false);
 }
 
 /**
 * 按实际图片比例对其的宽高进行缩放
 */
 private void calculateHeightAndWidth() {
 DisplayMetrics dm = context.getResources().getDisplayMetrics();
 final int screenWidth = dm.widthPixels;
 final int screenHeight = dm.heightPixels;
 
 //获取图片真正的宽高
 Glide.with(context)
  .load(mUrl)
  .asBitmap()//强制Glide返回一个Bitmap对象
  .into(new SimpleTarget<Bitmap>() {
   @Override
   public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
   picWidth = bitmap.getWidth();
   picHeight = bitmap.getHeight();
 
   if (picHeight > picWidth) {
    //真实图片高度大于宽度时
    finalHeight = screenHeight - padding * 2;
    finalWidth = ((picWidth * 100) / picHeight) * finalHeight/100;
   } else {
    //真实图片宽度大于高度时
    finalWidth = screenWidth - padding * 2;
    finalHeight = ((picHeight * 100) / picWidth) * finalWidth/100;
   }
   if ((int) finalWidth > screenWidth || (int) finalHeight > screenHeight) {
    //放大后的比例超出屏幕时
    finalWidth = picWidth;
    finalHeight = picHeight;
   }
   initAdView(finalWidth, finalHeight);
   }
  });
 }
 
 @SuppressLint("ResourceType")
 private void initAdView(float width, float height) {
 RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rl_ad);
 ImageView ivAd = new ImageView(context);
 ivAd.setScaleType(ImageView.ScaleType.FIT_XY);
 ivAd.setId(222);
 ivAd.setOnClickListener(this);
// RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(UIUtils.px2Dp((int) width), UIUtils.px2Dp((int) height));
 RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams((int) width, (int) height);
 params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
 params1.setMargins(0, dp2Px(50), 0, 0);
 relativeLayout.addView(ivAd, params1);
 
 Glide
  .with(context)
  .load(mUrl)
  .crossFade()
  .into(ivAd);
 
 ImageView ivCancel = new ImageView(context);
 ivCancel.setId(333);
 ivCancel.setOnClickListener(this);
 ivCancel.setImageResource(R.drawable.icon_ad_close);
 RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(dp2Px(30), dp2Px(30));
 params2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
 params2.addRule(RelativeLayout.ALIGN_END,222);
 relativeLayout.addView(ivCancel,params2);
 }
 
 // 初始化布局的参数
 private void initLayoutParams() {
 // 布局的参数
 WindowManager.LayoutParams params = getWindow().getAttributes();
 params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER;
 getWindow().setAttributes(params);
 }
 
 @Override
 public void onClick(View v) {
 switch (v.getId()) {
  case 222:
  Toast.makeText(context, "你点击了广告,即将进入···",Toast.LENGTH_SHORT).show();
  dismiss();
  break;
  case 333:
  dismiss();
  break;
 }
 }
 
 public int dp2Px(int dp){
    /*
    1、px = dp * (dpi / 160)
    2、px/dp = density;//像素密度比
    */
 DisplayMetrics metrics = context.getResources().getDisplayMetrics();
//    int dpi = metrics.densityDpi;//获得当前设备的dpi ,像素密度
 float density = metrics.density;//密度比
 return (int) (density * dp + .5f);
 }
}

动态布局可参考我的文章:安卓动态布局

Main

package com.example.leixiansheng.addialog;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
 
import java.util.Random;
 
public class MainActivity extends AppCompatActivity {
 
 private String[] adUrls = {"http://pic17.photophoto.cn/20101028/0017029521232265_b.jpg",
  "http://pic.58pic.com/58pic/13/56/51/95K58PICPFk_1024.jpg",
  "http://img5.imgtn.bdimg.com/it/u=1615585387,2628665620&fm=27&gp=0.jpg",
  "http://img1.imgtn.bdimg.com/it/u=1306190881,4135880992&fm=27&gp=0.jpg"};
 
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 randomAd();
 new AdDialog(this, randomAd()).show();
 }
 
 private String randomAd() {
 int i = (int) (Math.random() * (adUrls.length)); // 0 到 3 随机生成
 return adUrls[i];
 }
}

PS:当真实图片长度大于宽度时,图片可能距屏幕上方过近,自己修改padding值,或者增加setMargins的值即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文