Android開(kāi)發(fā)實(shí)現(xiàn)高仿優(yōu)酷的客戶端圖片左右滑動(dòng)切換功能實(shí)例【附源碼下載】
本文實(shí)例講述了Android開(kāi)發(fā)實(shí)現(xiàn)高仿優(yōu)酷的客戶端圖片左右滑動(dòng)切換功能。分享給大家供大家參考,具體如下:
本例是用ViewPager去做的實(shí)現(xiàn),支持自動(dòng)滑動(dòng)和手動(dòng)滑動(dòng),不僅優(yōu)酷網(wǎng),實(shí)際上有很多商城和門戶網(wǎng)站都有類似的實(shí)現(xiàn):
具體思路:
1. 工程中需要添加android-support-v4.jar,才能使用ViewPager控件.
2. 圖片的自動(dòng)切換: 可使用Timer或者ScheduledExecutorService,這個(gè)有多重方式可以實(shí)現(xiàn).
同時(shí)要切換底部的dots(園點(diǎn))
3.Handler+Message機(jī)制更新UI,這個(gè)相信大家都很熟練,不再描述
4. 實(shí)現(xiàn)的一些細(xì)節(jié):注意本例中的優(yōu)化:圖片的自動(dòng)切換啟動(dòng)了其他的線程,要在Activity在可見(jiàn)到不可見(jiàn)的狀態(tài),也就是在onStop()方法中將線程停止,在onStart()方法中開(kāi)啟線程。否則,Timer沒(méi)有停止,或者反復(fù)開(kāi)啟,會(huì)引起較大的內(nèi)存消耗,時(shí)間一長(zhǎng)就程序就會(huì)崩掉。 還有,就是在跳轉(zhuǎn)到其他Activity的過(guò)程中會(huì)出現(xiàn)畫(huà)面的卡頓
下面看一下效果圖和具體代碼:
工程結(jié)構(gòu)如下圖所示:
main.xml:
然后是具體的布局文件及代碼實(shí)現(xiàn):
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="優(yōu)酷客戶端" 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="中國(guó)家庭院校園區(qū)域名字體現(xiàn)" 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; /** * 仿優(yōu)酷Android客戶端圖片左右滑動(dòng) * */ public class MyViewPagerActivity extends Activity { private ViewPager viewPager; // android-support-v4中的滑動(dòng)組件 private List<ImageView> imageViews; // 滑動(dòng)的圖片集合 private String[] titles; // 圖片標(biāo)題 private int[] imageResId; // 圖片ID private List<View> dots; // 圖片標(biāo)題正文的那些點(diǎn) private TextView tv_title; private int currentItem = 0; // 當(dāng)前圖片的索引號(hào) // An ExecutorService that can schedule commands to run after a given delay, // or to execute periodically. private ScheduledExecutorService scheduledExecutorService; // 切換當(dāng)前顯示的圖片 private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { viewPager.setCurrentItem(currentItem);// 切換當(dāng)前顯示的圖片 }; }; @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] = "撲樹(shù)又回來(lái)啦!再唱經(jīng)典老歌引萬(wàn)人大合唱"; titles[2] = "揭秘北京電影如何升級(jí)"; titles[3] = "樂(lè)視網(wǎng)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());// 設(shè)置填充ViewPager頁(yè)面的適配器 // 設(shè)置一個(gè)監(jiān)聽(tīng)器,當(dāng)ViewPager中的頁(yè)面改變時(shí)調(diào)用 viewPager.setOnPageChangeListener(new MyPageChangeListener()); } @Override protected void onStart() { scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); // 當(dāng)Activity顯示出來(lái)后,每?jī)擅腌娗袚Q一次圖片顯示 scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 1, 2, TimeUnit.SECONDS); super.onStart(); } @Override protected void onStop() { // 當(dāng)Activity不可見(jiàn)的時(shí)候停止切換 scheduledExecutorService.shutdown(); super.onStop(); } /** * 換行切換任務(wù) * * @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(); // 通過(guò)Handler切換圖片 } } } /** * 當(dāng)ViewPager中頁(yè)面的狀態(tài)發(fā)生改變時(shí)調(diào)用 * * @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頁(yè)面的適配器 * * @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>
完整源碼點(diǎn)擊此處本站下載。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android開(kāi)發(fā)之使用ViewPager實(shí)現(xiàn)圖片左右滑動(dòng)切換效果
- Android編程單擊圖片實(shí)現(xiàn)切換效果的方法
- Android自定義ImageView實(shí)現(xiàn)點(diǎn)擊兩張圖片切換效果
- Android實(shí)現(xiàn)圖片輪播切換實(shí)例代碼
- Android基于ImageSwitcher實(shí)現(xiàn)圖片切換功能
- Android實(shí)現(xiàn)滑動(dòng)屏幕切換圖片
- Android游戲開(kāi)發(fā):實(shí)現(xiàn)手勢(shì)操作切換圖片的實(shí)例
- android控件實(shí)現(xiàn)多張圖片漸變切換
- Android使用ViewFlipper實(shí)現(xiàn)圖片切換功能
- Android開(kāi)發(fā)實(shí)現(xiàn)的圖片點(diǎn)擊切換功能示例
相關(guān)文章
Android ViewPager畫(huà)廊效果詳解及實(shí)例
這篇文章主要介紹了Android ViewPager畫(huà)廊效果詳解及實(shí)例的相關(guān)資料,這里提供實(shí)例代碼及實(shí)現(xiàn)效果圖,具有參考價(jià)值,需要的朋友可以參考下2016-12-12詳解Android Activity中的幾種監(jiān)聽(tīng)器和實(shí)現(xiàn)方式
這篇文章主要介紹了Activity中的幾種監(jiān)聽(tīng)器和實(shí)現(xiàn)方式的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04Android開(kāi)發(fā)之sqlite3命令行簡(jiǎn)單使用方法
這篇文章主要介紹了Android開(kāi)發(fā)之sqlite3命令行簡(jiǎn)單使用方法,分析了Android增刪改查等常用sqlite3的數(shù)據(jù)庫(kù)操作命令使用方法,需要的朋友可以參考下2016-02-02Android 頂部標(biāo)題欄隨滑動(dòng)時(shí)的漸變隱藏和漸變顯示效果
這篇文章主要介紹了Android 頂部標(biāo)題欄隨滑動(dòng)時(shí)的漸變隱藏和漸變顯示效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06Android開(kāi)發(fā)中畫(huà)廊視圖Gallery的兩種使用方法分析
這篇文章主要介紹了Android開(kāi)發(fā)中畫(huà)廊視圖Gallery的兩種使用方法,結(jié)合實(shí)例形式分析了Android畫(huà)廊視圖Gallery的簡(jiǎn)單布局與功能實(shí)現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下2018-01-01android ListActivity顯示圖標(biāo)實(shí)例
在ListActivity中顯示圖標(biāo),好像并不復(fù)雜,實(shí)現(xiàn)起來(lái)卻不輕松,我們下面一步步來(lái)實(shí)現(xiàn)ListActivity中顯示圖標(biāo)2013-11-11Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Android開(kāi)發(fā)之瀏覽器用法實(shí)例詳解(調(diào)用uc,opera,qq瀏覽器訪問(wèn)網(wǎng)頁(yè))
這篇文章主要介紹了Android開(kāi)發(fā)之瀏覽器用法,結(jié)合實(shí)例形式詳細(xì)分析了Android調(diào)用瀏覽器的具體步驟與相關(guān)使用技巧,需要的朋友可以參考下2016-01-01