您好,登錄后才能下訂單哦!
小編給大家分享一下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的值即可。
以上是“Android如何實現自適應屏幕的彈窗廣告”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。