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:
然后是具体的布局文件及代码实现:
main.xml:
<?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:gravity="center" > <view android:id="@+id/v_dot0" style="@style/dot_style" android:background="@drawable/dot_focused" /> <view android:id="@+id/v_dot1" style="@style/dot_style" /> <view android:id="@+id/v_dot2" style="@style/dot_style" /> <view android:id="@+id/v_dot3" style="@style/dot_style" /> <view android:id="@+id/v_dot4" style="@style/dot_style" /> </linearlayout> </linearlayout> </framelayout> </linearlayout>
myviewpageractivity:
package com.tony.viewpager; import java.util.arraylist; import java.util.list; import java.util.concurrent.executors; import java.util.concurrent.scheduledexecutorservice; import java.util.concurrent.timeunit; import android.app.activity; import android.os.bundle; import android.os.handler; import android.os.parcelable; import android.support.v4.view.pageradapter; import android.support.v4.view.viewpager; import android.support.v4.view.viewpager.onpagechangelistener; import android.view.view; import android.widget.imageview; import android.widget.imageview.scaletype; import android.widget.textview; /** * 仿优酷android客户端图片左右滑动 * */ public class myviewpageractivity extends activity { private viewpager viewpager; // android-support-v4中的滑动组件 private list<imageview> imageviews; // 滑动的图片集合 private string[] titles; // 图片标题 private int[] imageresid; // 图片id private list<view> dots; // 图片标题正文的那些点 private textview tv_title; private int currentitem = 0; // 当前图片的索引号 // an executorservice that can schedule commands to run after a given delay, // or to execute periodically. private scheduledexecutorservice scheduledexecutorservice; // 切换当前显示的图片 private handler handler = new handler() { public void handlemessage(android.os.message msg) { viewpager.setcurrentitem(currentitem);// 切换当前显示的图片 }; }; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); imageresid = new int[] { r.drawable.a, r.drawable.b, r.drawable.c, r.drawable.d, r.drawable.e }; titles = new string[imageresid.length]; titles[0] = "巩俐不低俗,我就不能低俗"; titles[1] = "扑树又回来啦!再唱经典老歌引万人大合唱"; titles[2] = "揭秘北京电影如何升级"; titles[3] = "乐视网tv版大派送"; titles[4] = "热血屌丝的反杀"; imageviews = new arraylist<imageview>(); // 初始化图片资源 for (int i = 0; i < imageresid.length; i++) { imageview imageview = new imageview(this); imageview.setimageresource(imageresid[i]); imageview.setscaletype(scaletype.center_crop); imageviews.add(imageview); } dots = new arraylist<view>(); dots.add(findviewbyid(r.id.v_dot0)); dots.add(findviewbyid(r.id.v_dot1)); dots.add(findviewbyid(r.id.v_dot2)); dots.add(findviewbyid(r.id.v_dot3)); dots.add(findviewbyid(r.id.v_dot4)); tv_title = (textview) findviewbyid(r.id.tv_title); tv_title.settext(titles[0]);// viewpager = (viewpager) findviewbyid(r.id.vp); viewpager.setadapter(new myadapter());// 设置填充viewpager页面的适配器 // 设置一个监听器,当viewpager中的页面改变时调用 viewpager.setonpagechangelistener(new mypagechangelistener()); } @override protected void onstart() { scheduledexecutorservice = executors.newsinglethreadscheduledexecutor(); // 当activity显示出来后,每两秒钟切换一次图片显示 scheduledexecutorservice.scheduleatfixedrate(new scrolltask(), 1, 2, timeunit.seconds); super.onstart(); } @override protected void onstop() { // 当activity不可见的时候停止切换 scheduledexecutorservice.shutdown(); super.onstop(); } /** * 换行切换任务 * * @author administrator * */ private class scrolltask implements runnable { public void run() { synchronized (viewpager) { system.out.println("currentitem: " + currentitem); currentitem = (currentitem + 1) % imageviews.size(); handler.obtainmessage().sendtotarget(); // 通过handler切换图片 } } } /** * 当viewpager中页面的状态发生改变时调用 * * @author administrator * */ private class mypagechangelistener implements onpagechangelistener { private int oldposition = 0; /** * this method will be invoked when a new page becomes selected. * position: position index of the new selected page. */ public void onpageselected(int position) { currentitem = position; tv_title.settext(titles[position]); dots.get(oldposition).setbackgroundresource(r.drawable.dot_normal); dots.get(position).setbackgroundresource(r.drawable.dot_focused); oldposition = position; } public void onpagescrollstatechanged(int arg0) { } public void onpagescrolled(int arg0, float arg1, int arg2) { } } /** * 填充viewpager页面的适配器 * * @author administrator * */ private class myadapter extends pageradapter { @override public int getcount() { return imageresid.length; } @override public object instantiateitem(view arg0, int arg1) { ((viewpager) arg0).addview(imageviews.get(arg1)); return imageviews.get(arg1); } @override public void destroyitem(view arg0, int arg1, object arg2) { ((viewpager) arg0).removeview((view) arg2); } @override public boolean isviewfromobject(view arg0, object arg1) { return arg0 == arg1; } @override public void restorestate(parcelable arg0, classloader arg1) { } @override public parcelable savestate() { return null; } @override public void startupdate(view arg0) { } @override public void finishupdate(view arg0) { } } }
drawable目录下
btn_back_selector.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_top_pressed" android:state_focused="true"></item> <item android:drawable="@drawable/btn_top_pressed" android:state_pressed="true"></item> <item android:drawable="@drawable/btn_top_pressed" android:state_selected="true"></item> <item android:drawable="@drawable/title_bk"></item> </selector>
btn_top_pressed.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:angle="270" android:endcolor="#009ad6" android:startcolor="#11264f" /> </shape>
dot_focused.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <solid android:color="#aaffffff" /> <corners android:radius="5dip" /> </shape>
dot_normal.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <solid android:color="#33000000" /> <corners android:radius="5dip" /> </shape>
title_bk.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:angle="270" android:endcolor="#11264f" android:startcolor="#009ad6" /> </shape>
完整源码点击此处本站下载。
更多关于android相关内容感兴趣的读者可查看本站专题:《android图形与图像处理技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。
上一篇: C#多线程编程中的锁系统基本用法
下一篇: 运营微信公众平台服务号的三点心得