編輯:Android 開發實例
一、先看效果。這是新浪微博的 Tab 滑動效果:既能用手勢左右滑動,也能點擊上方的頁卡標題切換。與此同時,那條白色橫條會平滑地移動到對應的頁卡下方。注意這是一個動畫過程,白條是緩慢滑動過去的,並非瞬間跳躍。接下來我們就一步步實現它。
二、動手前,先認識一個關鍵控件——ViewPager。它是 Google SDK 中附加包裡的一個類,專門用來實現屏幕間的切換。這個附加包就是 android-support-v4.jar,最終源碼裡會提供,放在 libs 文件夾中;你也可以自己上網搜最新版本。拿到後,把它添加進項目即可。
三、先來設計界面。結構很簡單:第一行是三個頁卡標題,第二行是滑動的白條動畫圖片,第三行是頁卡內容展示區。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 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>
我們要展示三個頁卡,所以還需要三個內容界面。這裡為了區分,只給每個頁卡設了不同的背景色,起到區分作用即可。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://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; // 頁卡內容 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));
}
/** 頭標點擊監聽 */
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);
}
}(3)初始化頁卡內容區,這裡實現了各頁卡的裝入與卸載:
/** 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) {}
}(4)初始化動畫,根據屏幕分辨率和圖片寬度計算出白條的移動偏移量:
/** 初始化動畫 */
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); // 設置動畫初始位置
}最後是頁卡切換監聽,通過 TranslateAnimation 讓白條在不同頁卡之間平滑移動:
/** 頁卡切換監聽 */
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:圖片停在動畫結束位置
animation.setDuration(300);
cursor.startAnimation(animation);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}
@Override
public void onPageScrollStateChanged(int arg0) {}
}五、打完收工,快來看看自己的勞動成果吧!
Android NestedScrolling 完全實戰:打造自訂的嵌套滑動效果
從 Android 5.0 Lollipop 開始,官方提供了一整套 API 來支援嵌套滑動(Nested Scrolling);最新的 Support V4 包也做了向
Android JSON 解析實戰:用 JSONObject 取出你要的欄位
JSON 的全名是 JavaScript Object Notation(JavaScript 物件符號法),它是一種與程式語言無關的輕量級資料交換格式。語法乾淨、可讀性
Android 介面設計趨勢:左側隱藏菜單、右側顯示內容
相關文章:android popwindow 實現左側彈出菜單層https://www.jb51.net/article/33533.htm移動 App 設計的 13 大
動手打造Android電子書書架:自定義GridView實現原理全解析
做電子書的開發者,大概都對「書架」這個效果又愛又恨。早期我曾用 ListView 硬湊過一版,效果差強人意;後來才發現,絕大多數優秀的書架其實都是靠 GridView 實