最近软件开发要做一个本地图片轮播的功能,主要实现一下功能:
(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=