Day12 Android - banner(横幅广告)应用(1)

今天主要要来提banner(横幅广告)的应用,那麽就先来做本地取图来放映广告并且轮播,明天则做线上取图的应用(因为有些微不一样所以分开来讲)。


首先先添加依赖在build.gradle/dependencies中。

依赖

    <--添加banner依赖及glide图片载入器-->
    implementation 'com.youth.banner:banner:1.4.9'
    implementation "com.github.bumptech.glide:glide:3.7.0"

依赖加进去以後按右上角的Sync now就可以了,接着就在布局中添加banner元件。


布局

<--布局ConstraintLayout,将广告放置於底部-->
    <com.youth.banner.Banner
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:background="@color/purple_200"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />

就看是使用什麽布局,就使用那个布局的属性来进行排版,我这边是使用ConstraintLayout来做,且因为我选的图都没有颜色,所以则加入background来观察下面圆点的部分,虽然不会覆盖原图的颜色,但未被覆盖到的地方则会有颜色,实际操作是可以拿掉的,而接下来,因为是本地图片,所以可以先将想要显示当广告的图片加入至drawable中。

加入图片

可透过Image Asset将想显示的图片加入。

https://ithelp.ithome.com.tw/upload/images/20210823/20139259SywJ2cg9tt.png

https://ithelp.ithome.com.tw/upload/images/20210823/20139259g6SOQWsK6y.png

若按了next之後发现是红色的,代表这个图档的名字在你专案底下的app/src/main已经存在了,则需要再将那个名字的档案删除再建立。

banner

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Banner banner = findViewById(R.id.banner);
        int[] resourceID = new int[]{R.mipmap.image1_foreground, R.mipmap.image2_foreground, R.mipmap.image3_foreground};
        //线上图片索引则用String[]
        List<Integer> img_list = new ArrayList<>();
        for (int i = 0; i < resourceID.length; i++) {
            img_list.add(resourceID[i]);              //图片加入List中
            banner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    Glide.with(MainActivity.this).load(path).into(imageView);//Glide加载图片
                }
            });
            banner.setBannerAnimation(Transformer.Accordion);//轮播动画效果
            banner.setImages(img_list);//设定图片位址(已加入至List中)
            banner.setDelayTime(5000);//播5秒自动切换
            //banner.isAutoPlay(true);自动切换,设定好setDelayTime也会自动切换
            banner.start();
        }
    }
}

我使用Glide加载图片并将图片位址使用List的图片,其他则是banner常用的功能属性,大约这样就完成了一个简易的banner!明天就来看有关线上取图的部分。

成果

https://ithelp.ithome.com.tw/upload/images/20210824/20139259YPfKKJgoZv.jpg


https://ithelp.ithome.com.tw/upload/images/20210824/20139259XqHXgBnyR3.jpg


https://ithelp.ithome.com.tw/upload/images/20210824/20139259k9sKiS0kKu.jpg


<<:  Pascal 语言和你 SAY HELLO!!

>>:  [前端暴龙机,Vue2.x 进化 Vue3 ] Day18.组件练习-分页(一)

Day 7 - PD架构

身为TIDB的PM,PD主要接收两类讯息,一种是TiKV store回传的心跳信息,store就是T...

Day-27 使用StatefulSet

前言 前面我们先介绍了Pod, 控管Pod的ReplicaSet与管理ReplicaSet的Depl...

【day7】关於每日便当菜

其实这个系列参赛 主要是希望自己可以平日做便当、假日吃好料 但工作太忙了 还需要多一点时间调整 所以...

[DAY 21]纠团通知功能(1/3)

先前做的公会文字云 其中任务、副本、主线的出现次数很多代表频道里蛮常有人想纠团的 但我翻了一下纪录成...

Day24 NiFi 延伸应用 - Slack & Email

有时候 Data Pipeline 可能会因为一些原因出现一些问题,像是网路问题、OOM、资料格式不...