編輯:關於 Android 程式設計
ViewPager 是 Google SDK 中自帶附加包(android-support-v4.jar)裡的一個類別,可用來實現螢幕間的切換。本文示範如何使用 ViewPager 實現多頁面滑動切換,並加上底部指示圖片的滑動動畫。
介面設計很簡單:第一行三個頁卡頭標,第二行是動畫圖片(指示器),第三行是頁卡內容展示。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_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:text="頁卡1" ... />
<TextView android:id="@+id/text2" android:text="頁卡2" ... />
<TextView android:id="@+id/text3" android:text="頁卡3" ... />
</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>
我們要展示三個頁卡,因此需要三個頁卡內容的版面配置(lay1、lay2、lay3),這裡只設置不同背景顏色以作區別:
<?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="#1586FF" >
</LinearLayout>
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);
}
};
此功能就是點擊第幾個頭標,就展示第幾個頁卡內容。
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());
}
將三個頁卡介面裝入其中,預設顯示第一個,並需要一個自定義的適配器。
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 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) {}
@Override
public void finishUpdate(View arg0) {}
}
這裡實現了各頁卡的裝入與卸載。
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); // 圖片停在動畫結束位置
animation.setDuration(300);
cursor.startAnimation(animation);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}
@Override
public void onPageScrollStateChanged(int arg0) {}
}
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); // 設定動畫初始位置
}
此處依螢幕解析度與圖片寬度計算動畫移動的偏移量。
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; // 動畫圖片寬度
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager);
InitImageView();
InitTextView();
InitViewPager();
}
TranslateAnimation 是移動的動畫效果,最常用的構造函式為:
public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
若 View 在 A(x, y) 點,動畫即從 B(x+fromXDelta, y+fromYDelta) 移動到 C(x+toXDelta, y+toYDelta)。
另一個構造函式 TranslateAnimation(int fromXType, float fromXValue, int toXType, float toYValue, ...) 則多了「參照類型」參數,可選 Animation.ABSOLUTE(絕對座標值)、RELATIVE_TO_SELF(相對於自身)、RELATIVE_TO_PARENT(相對於父容器)。以 x 軸為例:若參照為 ABSOLUTE,對應值為具體座標值;若為 RELATIVE_TO_SELF 或 RELATIVE_TO_PARENT,則對應值代表相對於自身或父容器的幾倍或百分比。
Android 允許我們在對話框中顯示指定的 XML 版面配置,從而實現自定義對話框的效果。以下是一個自定義的登入對話框:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/loginForm"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableRow>
<TextView android:text="用戶名:" android:textSize="10pt" ... />
<EditText android:hint="請填寫登錄帳號" android:selectAllOnFocus="true" ... />
</TableRow>
<TableRow>
<TextView android:text="密碼:" android:textSize="10pt" ... />
<EditText android:password="true" ... />
</TableRow>
<TableRow>
<TextView android:text="電話號碼:" android:textSize="10pt" ... />
<EditText android:hint="請填寫您的電話號碼" android:phoneNumber="true" ... />
</TableRow>
<Button android:text="注冊" ... />
</TableLayout>
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View source) {
builder.setIcon(R.drawable.tools);
builder.setTitle("自定義普通對話框");
// 裝載 /res/layout/login.xml 版面配置
TableLayout loginForm = (TableLayout) getLayoutInflater().inflate(R.layout.login, null);
builder.setView(loginForm);
builder.setPositiveButton("登錄", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
});
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
});
builder.create().show();
}
});
Android 屬性動畫入門:ValueAnimator、ObjectAnimator 與組合動畫
在 Android 開發中,適當使用動畫可以提升應用整體質感。早期的動畫方案主要包含幀動畫與補間動畫,而 Android 3.0 之後推出的屬性動畫,則讓開發者能更靈活地
子線程為何能更新 UI?從 TextView 到 ViewRootImpl 的線程檢查
從一個看似反常的現象說起有時在子線程中直接呼叫 TextView.setText,畫面竟然會更新。這並不代表 Android 允許任意線程操作 UI,而是因為在某些早期階
Android 三種菜單機制深度解析:OptionMenu、ActionMode 與 PopupMenu
以下說明皆以 Android 3.0(API 11)為基準。本文將依序介紹三種基本菜單:PartA 操作欄(選項菜單 OptionMenu)、PartB 上下文操作模式(
Android 中獲取控件寬高與屏幕寬高的多種方法
一、獲取屏幕寬高 1. Android 界面結構簡介 要獲取屏幕寬高,不妨先了解 Android 界面的構成。它由三部分組成:狀態欄、標題欄、內容區域。 (1)狀態欄