Android中实现首页图片/视频广告

Android中实现首页图片/视频广告

如何实现

在首页把广告下载到本地,然后启动界面显示,提前下载的目的是,显示本地更快。

参考文档:

Android中封装OkHttp,处理网络请求-优快云博客

我的工具类专栏

下载依赖

//给任意控件添加闪光效果
//这里主要用到广告界面,打开广告按钮
//https://github.com/facebook/shimmer-android
implementation 'com.facebook.shimmer:shimmer:0.5.0'

//腾讯官方播放器,可以播放其他地址视频,不是说一定要播放腾讯点播服务的视频
//com.tencent.liteav:LiteAVSDK_Player:播放器核心
//super-player-tencent:更上层的接口和功能,例如:弹幕,滑动进度,滑动调整音量
//https://cloud.tencent.com/document/product/881/20213
implementation project(path: ':super-player-tencent')

实现下载广告数据到本地

private void loadSplashAd() {
   
    DefaultRepository.getInstance().splashAd()
            .to(autoDisposable(AndroidLifecycleScopeProvider.from(this)))
            .subscribe(new HttpObserver<ListResponse<Ad>>() {
   
                @Override
                public boolean onFailed(ListResponse<Ad> data, Throwable e) {
   
                    endRefresh();
                    return super.onFailed(data, e);
                }

                @Override
                public void onSucceeded(ListResponse<Ad> data) {
   
                    List<Ad> results = data.getData().getData();
                    if (CollectionUtils.isNotEmpty(results)) {
   
                        downloadAd(results.get(0));
                    } else {
   
                        //删除本地广告数据
                        deleteSplashAd();
                    }
                }
            });
}

private void downloadAd(Ad data) {
   
    if (SuperNetworkUtil.isWifiConnected(getHostActivity())) {
   
        //wifi才下载
        sp.setSplashAd(data);

        //判断文件是否存在,如果存在就不下载
        File targetFile = FileUtil.adFile(getHostActivity(), data.getIcon());
        if (targetFile.exists()) {
   
            return;
        }

        new Thread(
                new Runnable() {
   
                    @Override
                    public void run() {
   

                        try {
   
                            //FutureTarget会阻塞
                            //所以需要在子线程调用
                            FutureTarget<File> target = Glide.with(getHostActivity().getApplicationContext())
                                    .asFile()
                                    .load(ResourceUtil.resourceUri(data.getIcon()))
                                    .submit();

                            //获取下载的文件
                            File file = target.get();

                            //将文件拷贝到我们需要的位置
                            FileUtils.moveFile(file, targetFile);

                        } catch (Exception e) {
   
                            e.printStackTrace();
                        }
                    }
                }
        ).start();
    }
}

/**
 * 删除启动界面广告
 */
private void deleteSplashAd() {
   
    //获取广告信息
    Ad ad = sp.getSplashAd();
    if (ad != null) {
   
        //删除配置文件
        sp.setSplashAd(null);

        //删除文件
        FileUtils.deleteQuietly(FileUtil.adFile(getHostActivity(), ad.getIcon()));
    }
}

图片广告

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:attr/colorBackground"
    tools:context=".component.ad.activity.AdActivity">

    <!--图片广告-->
    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

    <!--广告控制层-->
    <RelativeLayout
        android:id="@+id/ad_control"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">

        <TextView
            android:id="@+id/preload"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/padding_meddle"
            android:layout_marginTop="@dimen/d50"
            android:layout_marginBottom="@dimen/d50"
            android:background="@drawable/shape_button_transparent_radius_small"
            android:gravity="center"
            android:padding="@dimen/d5"
            android:text="@string/wifi_preload"
            android:textColor="?attr/colorLightWhite"
            android:textSize="@dimen/text_small"
            android:visibility="gone" />

        <!--跳过广告按钮-->
        <TextView
            android:id="@+id/skip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginTop="@dimen/d50"
            android:layout_marginRight="@dimen/padding_large"
            android:layout_marginBottom="@dimen/d50"
            android:background="@drawable/shape_button_transparent_radius_small"
            android:gravity="center"
            android:padding="@dimen/padding_meddle"
            android:textColor="?attr/colorLightWhite"
            android:textSize="@dimen/text_meddle"
            app:cornerRadius="@dimen/d30"
            tools:text="@string/skip_ad_count&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值