Android編程實現(xiàn)ViewPager多頁面滑動切換及動畫效果的方法
本文實例講述了Android編程實現(xiàn)ViewPager多頁面滑動切換及動畫效果的方法。分享給大家供大家參考,具體如下:
一、首先,我們來看一下效果圖,這是新浪微博的Tab滑動效果。我們可以手勢滑動,也可以點擊上面的頭標進行切換。與此同方式,
白色橫條會移動到相應(yīng)的頁卡頭標下。這是一個動畫效果,白條是緩慢滑動過去的。好了,接下來我們就來實現(xiàn)它。

二、在開始前,我們先要認識一個控件,ViewPager。它是google SDk中自帶的一個附加包的一個類,可以用來實現(xiàn)屏幕間的切換。
這個附加包是android-support-v4.jar,在最后的源碼中會提供給大家,在libs文件夾中。當然你也可以自己從網(wǎng)上搜索最新的版本。
找到它后,我們需要在項目中添加

三、我們先做界面
界面設(shè)計很簡單,第一行三個頭標,第二行動畫圖片,第三行頁卡內(nèi)容展示。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:umadsdk="http://schemas.android.com/apk/res/com.LoveBus" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="100.0dip" android:background="#FFFFFF" > <TextView android:id="@+id/text1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:gravity="center" android:text="頁卡1" android:textColor="#000000" android:textSize="22.0dip" /> <TextView android:id="@+id/text2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:gravity="center" android:text="頁卡2" android:textColor="#000000" android:textSize="22.0dip" /> <TextView android:id="@+id/text3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:gravity="center" android:text="頁卡3" android:textColor="#000000" android:textSize="22.0dip" /> </LinearLayout> <ImageView android:id="@+id/cursor" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scaleType="matrix" android:src="@drawable/a" /> <android.support.v4.view.ViewPager android:id="@+id/vPager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1.0" android:background="#000000" android:flipInterval="30" android:persistentDrawingCache="animation" /> </LinearLayout>
我們要展示三個頁卡,所以還需要三個頁卡內(nèi)容的界面設(shè)計,這里我們只設(shè)置了背景顏色,能起到區(qū)別作用即可。
<?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:orientation="vertical" android:background="#158684" > </LinearLayout>
四、代碼部分要進行初始化的工作
(1) 先來變量的定義
private ViewPager mPager;//頁卡內(nèi)容 private List<View> listViews; // Tab頁面列表 private ImageView cursor;// 動畫圖片 private TextView t1, t2, t3;// 頁卡頭標 private int offset = 0;// 動畫圖片偏移量 private int currIndex = 0;// 當前頁卡編號 private int bmpW;// 動畫圖片寬度
(2) 初始化頭標
/**
* 初始化頭標
*/
private void InitTextView() {
t1 = (TextView) findViewById(R.id.text1);
t2 = (TextView) findViewById(R.id.text2);
t3 = (TextView) findViewById(R.id.text3);
t1.setOnClickListener(new MyOnClickListener(0));
t2.setOnClickListener(new MyOnClickListener(1));
t3.setOnClickListener(new MyOnClickListener(2));
}
/**
* 頭標點擊監(jiān)聽
*/
public class MyOnClickListener implements View.OnClickListener {
private int index = 0;
public MyOnClickListener(int i) {
index = i;
}
@Override
public void onClick(View v) {
mPager.setCurrentItem(index);
}
};
相信大家看后都沒什么問題,點擊第幾個,就展示第幾個頁卡內(nèi)容。
(3) 初始化頁卡內(nèi)容區(qū)
/**
* 初始化ViewPager
*/
private void InitViewPager() {
mPager = (ViewPager) findViewById(R.id.vPager);
listViews = new ArrayList<View>();
LayoutInflater mInflater = getLayoutInflater();
listViews.add(mInflater.inflate(R.layout.lay1, null));
listViews.add(mInflater.inflate(R.layout.lay2, null));
listViews.add(mInflater.inflate(R.layout.lay3, null));
mPager.setAdapter(new MyPagerAdapter(listViews));
mPager.setCurrentItem(0);
mPager.setOnPageChangeListener(new MyOnPageChangeListener());
}
我們將三個頁卡界面裝入其中,默認顯示第一個頁卡。這里我們還需要實現(xiàn)一個適配器。
/**
* ViewPager適配器
*/
public class MyPagerAdapter extends PagerAdapter {
public List<View> mListViews;
public MyPagerAdapter(List<View> mListViews) {
this.mListViews = mListViews;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView(mListViews.get(arg1));
}
@Override
public void finishUpdate(View arg0) {
}
@Override
public int getCount() {
return mListViews.size();
}
@Override
public Object instantiateItem(View arg0, int arg1) {
((ViewPager) arg0).addView(mListViews.get(arg1), 0);
return mListViews.get(arg1);
}
@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) {
}
}
這里我們實現(xiàn)了各頁卡的裝入和卸載
(3) 初始化動畫
/**
* 初始化動畫
*/
private void InitImageView() {
cursor = (ImageView) findViewById(R.id.cursor);
bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a)
.getWidth();// 獲取圖片寬度
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenW = dm.widthPixels;// 獲取分辨率寬度
offset = (screenW / 3 - bmpW) / 2;// 計算偏移量
Matrix matrix = new Matrix();
matrix.postTranslate(offset, 0);
cursor.setImageMatrix(matrix);// 設(shè)置動畫初始位置
}
根據(jù)屏幕的分辨率和圖片的寬度計算動畫移動的偏移量

實現(xiàn)頁卡切換監(jiān)聽
/**
* 頁卡切換監(jiān)聽
*/
public class MyOnPageChangeListener implements OnPageChangeListener {
int one = offset * 2 + bmpW;// 頁卡1 -> 頁卡2 偏移量
int two = one * 2;// 頁卡1 -> 頁卡3 偏移量
@Override
public void onPageSelected(int arg0) {
Animation animation = null;
switch (arg0) {
case 0:
if (currIndex == 1) {
animation = new TranslateAnimation(one, 0, 0, 0);
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, 0, 0, 0);
}
break;
case 1:
if (currIndex == 0) {
animation = new TranslateAnimation(offset, one, 0, 0);
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, one, 0, 0);
}
break;
case 2:
if (currIndex == 0) {
animation = new TranslateAnimation(offset, two, 0, 0);
} else if (currIndex == 1) {
animation = new TranslateAnimation(one, two, 0, 0);
}
break;
}
currIndex = arg0;
animation.setFillAfter(true);// True:圖片停在動畫結(jié)束位置
animation.setDuration(300);
cursor.startAnimation(animation);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
}
五、打完收工,快來看看自己的勞動成果吧

希望本文所述對大家Android程序設(shè)計有所幫助。
- Android如何使用ViewPager2實現(xiàn)頁面滑動切換效果
- Android HorizontalScrollView滑動與ViewPager切換案例詳解
- Android使用TabLayou+fragment+viewpager實現(xiàn)滑動切換頁面效果
- Android ViewPager撤消左右滑動切換功能實現(xiàn)代碼
- Android開發(fā)之使用ViewPager實現(xiàn)圖片左右滑動切換效果
- Android中的ViewPager視圖滑動切換類的入門實例教程
- Android App中使用ViewPager+Fragment實現(xiàn)滑動切換效果
- Android應(yīng)用中利用ViewPager實現(xiàn)多頁面滑動切換效果示例
- Android實現(xiàn)界面左右滑動切換功能
- Android開發(fā)之ViewPager實現(xiàn)滑動切換頁面
相關(guān)文章
基于Android10渲染Surface的創(chuàng)建過程
這篇文章主要介紹了基于Android10渲染Surface的創(chuàng)建過程,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08
MVVMLight項目之綁定在表單驗證上的應(yīng)用示例分析
這篇文章主要為大家介紹了MVVMLight項目中綁定在表單驗證上的應(yīng)用示例及源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步除夕快樂,新年快樂2022-01-01
Android ListView自動顯示隱藏布局的實現(xiàn)方法
這篇文章主要介紹了Android ListView自動顯示隱藏布局的實現(xiàn)方法的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
Android高級組件AutoCompleteTextView自動完成文本框使用詳解
這篇文章主要介紹了Android高級組件AutoCompleteTextView自動完成文本框的使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android編程實現(xiàn)的微信支付功能詳解【附Demo源碼下載】
這篇文章主要介紹了Android編程實現(xiàn)的微信支付功能,結(jié)合實例形式詳細分析了Android微信支付功能的實現(xiàn)步驟與具體操作技巧,并附帶了Demo源碼供讀者下載參考,需要的朋友可以參考下2017-07-07
Android ListView的item背景色設(shè)置和item點擊無響應(yīng)的解決方法
在Android開發(fā)中,listview控件是非常常用的控件,在大多數(shù)情況下,大家都會改掉listview的item默認的外觀。2013-11-11

