Android之本地图片轮播(广告轮播)

本文介绍了如何在Android应用中实现本地图片的轮播功能,包括关键的布局文件、自定义组件和适配器的代码实现,并提供了参考链接及源码下载。通过MainActivity.java、SlideShowView.java、ChildViewPager.java和SlideShowPagerAdapter.java等组件,实现了广告轮播效果。
摘要由CSDN通过智能技术生成
最近软件开发要做一个本地图片轮播的功能,主要实现一下功能:
(1)每隔特定时间(本例是4秒)自动展示一张新的图片;
(2)可以进行手势滑动(滑动过程中暂停自动播放计时);
(3)可以轮转显示,播放到最后一张或者滑动到最后一张,跳转到第一张。
效果图如下:

效果图
该项目demo源码的下载地址:
Android之本地图片轮播.zip

下面开始代码实现:

首先是主界面布局activity_main.xml(注:其中slideshowView我用了addView的方法添加进去,这里就不需要它的布局了,
而之后的slideshowView中的ChildViewPager我在布局文件里添加了。其实两种方法都一样的效果,看个人喜好。):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
    android:id="@+id/tv"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:background="#05de38"

    android:gravity="center"
    android:text="本地图片轮播"
    android:textColor="#ffffff"
    android:textSize="23sp" />

    <RelativeLayout
        android:paddingTop="40dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:id="@+id/relativeLayout"
        android:layout_below="@id/tv"
        android:layout_width="fill_parent"
        android:layout_height="300dp">

        <!-- <SlideShowView
             android:id="@+id/slideshowView"
             android:layout_width="fill_parent"
             android:layout_height="200dp"
             android:layout_centerHorizontal="true"
             />-->


    </RelativeLayout>

</RelativeLayout>

slideshowView的布局layout_slideshow.xml :

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

    <com.android.imagecarousel.ChildViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout android:id="@+id/dotLayout"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentBottom="true"
                  android:padding="8dp"
                  android:gravity="right"
                  android:orientation="horizontal">

        <View
            android:id="@+id/v_dot1"
            android:layout_width="5dp"
            android:layout_height="5dp"
            android:background="@drawable/dotshape" />

        <View
            android:id="@+id/v_dot2"
            android:layout_width="5dp"
            android:layout_height="5dp"
            android:layout_marginLeft=
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值