当前位置:操作系统 > 安卓/Android >>

高仿优酷Android客户端图片左右滑动(自动切换)

本例是用ViewPager去做的实现,支持自动滑动和手动滑动,不仅优酷网,实际上有很多商城和门户网站都有类似的实现:

具体思路:

1. 工程中需要添加android-support-v4.jar,才能使用ViewPager控件.

2. 图片的自动切换: 可使用Timer或者ScheduledExecutorService,这个有多重方式可以实现.

    同时要切换底部的dots(园点)


3.Handler+Message机制更新UI,这个相信大家都很熟练,不再描述

4. 实现的一些细节:注意本例中的优化:图片的自动切换启动了其他的线程,要在Activity在可见到不可见的状态,也就是在onStop()方法中将线程停止,在onStart()方法中开启线程。否则,Timer没有停止,或者反复开启,会引起较大的内存消耗,时间一长就程序就会崩掉。 还有,就是在跳转到其他Activity的过程中会出现画面的卡顿

 

 

下面看一下效果图和具体代码:

           

 \            \
 

 

 


工程结构如下图所示:

main.xml:

[java]
<P> 
</P><P><STRONG>然后是具体的布局文件及代码实现:</STRONG></P><P>main.xml:</P> 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFF" 
    android:orientation="vertical" > 
 
    <RelativeLayout 
        android:layout_width="fill_parent" 
        android:layout_height="40dip" 
        android:background="@drawable/title_bk" > 
 
        <ImageButton 
            android:id="@+id/btn_back" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:background="@drawable/btn_back_selector" 
            android:src="@drawable/btn_back" /> 
 
        <View 
            android:id="@+id/line0" 
            android:layout_width="1px" 
            android:layout_height="fill_parent" 
            android:layout_toRightOf="@id/btn_back" 
            android:background="#aa11264f" /> 
 
        <View 
            android:layout_width="1px" 
            android:layout_height="fill_parent" 
            android:layout_toRightOf="@id/line0" 
            android:background="#009ad6" /> 
 
        <TextView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_centerInParent="true" 
            android:text="优酷客户端" 
            android:textColor="#FFFFFF" 
            android:textSize="20sp" /> 
    </RelativeLayout> 
 
    <FrameLayout 
        android:layout_width="fill_parent" 
        android:layout_height="140dip" > 
 
        <android.support.v4.view.ViewPager 
            android:id="@+id/vp" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent" /> 
 
        <LinearLayout 
            android:layout_width="fill_parent" 
            android:layout_height="35dip" 
            android:layout_gravity="bottom" 
            android:background="#33000000" 
            android:gravity="center" 
            android:orientation="vertical" > 
 
            <TextView 
                android:id="@+id/tv_title" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:text="中国家庭院校园区域名字体现" 
                android:textColor="#ffffff" /> 
 
            <LinearLayout 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:layout_marginTop="3dip" 
    

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,