自定义Banner轮播广告(真*无限循环无卡顿&设置切换速度)

本文介绍如何自定义Banner控件,实现无限循环的轮播广告,并详细讲解了如何设置切换速度,确保无卡顿效果,提供源码供参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

    Github上有很多轮播广告的源码,比如带着很酷炫动画的flashView框架。
    不过就学习而已,我建议每个人都应该自己多尝试着写一些控件。
    以下,是我为小白们分享的简单经验。

自定义控件

    先展示下效果图。!由于不会做gif图,只能粗略的展示效果,看官见谅
    ![这里写图片描述](https://img-blog.youkuaiyun.com/20170225165425535?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGFiZW5kYW4wNzE0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)

    **简单说下思路,就是由一个展示图片的viewPager和右下角的三个radioButton组成的自定义组合控件。**

送上第一道菜——布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>
    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="15dp"
        >
        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="7dp"
            android:layout_height="8dp"
            android:button="@null"
            android:background="@drawable/circle_advertisement"
            android:clickable="false"
            />
        <RadioButton
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:id="@+id/rb2"
            android:layout_width="7dp"
            android:layout_height="8dp"
            android:button="@null"
            android:background="@drawable/circle_advertisement"
            android:clickable="false"
            />
        <RadioButton
            android:id="@+id/rb3"
            android:layout_width="7dp"
            android:layout_height="8dp"
            android:button="@null"
            android:background="@drawable/circle_advertisement"
            android:clickable="false"
            />
    </RadioGroup>
</RelativeLayout>

简单说一说自定义组合控件的使用,其实很简单。
1.让你的控件继承一个布局(LinerLayout or RelativeLayout)
public class YAdvertisementLayout extends LinearLayout
2.重写构造方法,添加你要进行的操作(构造方法有4个,分别对应不同构造方式),
尽量在每个构造方法下都要写你初始化的操作。
 public YAdvertisementLayout(Context context) {
        super(context);
        initView(context);
    }

    public YAdvertisementLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public YAdvertisementLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }
3.初始化布局,组合控件就加上如下代码,然后就可以对你的控件为所欲为了
View v = inflate(context, R.layout.widget_advertisement, this);
<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值