实现android广告栏效果

本文介绍了如何在Android中实现广告栏效果,包括无限轮播、自动和手动滑动、自定义指示器样式和位置、支持本地和网络图片、流畅滑动等功能。详细步骤包括添加Glide库、开启网络权限、自定义BannerLayout类、创建布局XML以及在MainActivity中调用相关方法。提供的示例代码可实现点击监听和灵活配置,适用于各种广告需求。
摘要由CSDN通过智能技术生成

android对于广告栏的要求:

无限轮播
自动加手动滑动
简单的自定义指示器样式及位置
支持本地图片及网络图片
滑动流畅,无卡顿,无闪烁
广告页面不限制个数
页面点击监听事件
简单易用,高配置,无明显bug

动画效果默认的就好

实现效果图:


前期准备工作:

1.添加类库,在build.gradle中添加对Glide (图片加载框架)的引用:

compile 'com.ydevelop:bannerlayout:1.0.4'


 2.AndroidManifest.xml中开网络权限,因为需要获取网络的图片

 
<uses-permission android:name="android.permission.INTERNET"/>


(1)在values目录下新建一个attr.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="visible_tab_count" format="integer"/>
    <declare-styleable name="BannerLayoutStyle">
        <attr name="selectedIndicatorColor" format="color|reference"/>
        <attr name="unSelectedIndicatorColor" format="color|reference"/>
        <attr name="titleBGColor" format="color|reference"/>
        <attr name="titleColor" format="color|reference"/>


        <attr name="indicatorShape" format="enum">
            <enum name="rect" value="0"/>
            <enum name="oval" value="1"/>
        </attr>

        <attr name="selectedIndicatorHeight" format="dimension|reference"/>
        <attr name="selectedIndicatorWidth" format="dimension|reference"/>
        <attr name="unSelectedIndicatorHeight" format="dimension|reference"/>
        <attr name="unSelectedIndicatorWidth" format="dimension|reference"/>

        <attr name="indicatorSpace" format="dimension|reference"/>
        <attr name="indicatorMargin" format="dimension|reference"/>
        <attr name="autoPlayDuration" format="integer|reference"/>
        <attr name="scrollDuration" format="integer|reference"/>
        <attr name="isAutoPlay" format="boolean"/>
        <attr name="defaultImage" format="integer|reference"/>

    </declare-styleable>
</resources>

(2)自定义一个类BannerLayout,实现轮播图效果:注意类的包名,见后面

(3)新建一个xml,引用自定义的类

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical" >
 
    <com.example.administrator.testb.BannerLayout
        android:id="@+id/banner2"
        android:layout_width="match_parent"
        android:layout_height="210dp"
        app:defaultImage="@mipmap/ic_launcher"
        app:scrollDuration="1000"
        app:autoPlayDuration="3000"
        app:unSelectedIndicatorHeight="10dp"
        app:unSelectedIndicatorWidth="10dp"
        app:selectedIndicatorHeight="10dp"
        
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值