編輯:關於 Android 程式設計
本節要做的是 App 的「第一印象」:一個帶淡入動畫的歡迎頁,接一段可左右滑動的引導頁,最後由引導頁末屏的 Go 按鈕跳轉到登錄界面。
目標效果:

運行後先出現圖一的 walker 歡迎界面,畫面由模糊漸漸變清晰;停留數秒後自動跳轉到圖二的引導界面,底部有四個小圓點指示當前頁,第四屏右下角有一個 Go 按鈕,點擊進入登錄頁。
新建兩個 Activity:WelcomeActivity 與 GiudeActivity(源碼裡拼寫如此,保持一致以免引用出錯),各自配一份 xml 布局。
activity_welcome.xml
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:id="@+id/layoutwelcome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/welcome_bg"
tools:context=".WelcomeActivity">
</RelativeLayout>
新建 view1_of_pager.xml ~ view4_of_pager.xml。前三屏結構相同,只是背景圖不同;第四屏右下角多一個文字為 Go 的 TextView。
view1_of_pager.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/guide_1"
android:orientation="vertical">
</LinearLayout>
view4_of_pager.xml
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/guide_4">
<TextView
android:id="@+id/tvGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="16dp"
android:layout_marginRight="19dp"
android:background="@drawable/start_select"
android:gravity="center"
android:text="Go"
android:textColor="#ffffff"
android:textSize="30sp"/>
</RelativeLayout>
用 selector 加兩個 shape 就能做出按下反饋。
start_select.xml
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@drawable/start_before"/>
<item android:state_pressed="true" android:drawable="@drawable/start_after"/>
</selector>
start_before.xml(未按下)
<shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="#2362a7"/>
<size android:width="80dp" android:height="80dp"/>
</shape>
start_after.xml(按下)
<shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="#333333"/>
<size android:width="80dp" android:height="80dp"/>
</shape>
同樣用兩個 oval shape 區分選中與未選中狀態。
bg_point_select.xml(選中)
<shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="#427ab7"/>
<size android:width="8dp" android:height="8dp"/>
</shape>
bg_point.xml(未選中)
<shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="@android:color/black"/>
<size android:width="8dp" android:height="8dp"/>
</shape>
activity_giude.xml 放置四個圓點:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GiudeActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:orientation="horizontal">
<ImageView android:id="@+id/ivGuide_dot1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_point_selected"/>
<ImageView android:id="@+id/ivGuide_dot2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_point"/>
<ImageView android:id="@+id/ivGuide_dot3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_point"/>
<ImageView android:id="@+id/ivGuide_dot4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_point"/>
</LinearLayout>
</RelativeLayout>
GuideAdapter.java
public class GuideAdapter extends PagerAdapter {
private List<View> views;
public GuideAdapter(List<View> views) {
this.views = views;
}
@Override public int getCount() { return views.size(); }
@Override public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override public Object instantiateItem(ViewGroup container, int position) {
container.addView(views.get(position));
return views.get(position);
}
@Override public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(views.get(position));
}
}
WelcomeActivity.java
public class WelcomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
RelativeLayout layoutWelcome = (RelativeLayout) findViewById(R.id.layoutwelcome);
// 透明度從 0.1 渐变到 1.0
AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
alphaAnimation.setDuration(3000);
layoutWelcome.startAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override public void onAnimationStart(Animation animation) { }
@Override public void onAnimationRepeat(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(WelcomeActivity.this, GiudeActivity.class));
finish(); // 銷毀本頁,避免按返回鍵又回到歡迎頁
}
});
}
}
這裡的關鍵是監聽 onAnimationEnd 並在其中 finish 掉自己,否則用戶從登錄頁一路返回,會又看到歡迎動畫。
GiudeActivity.java(核心片段)
public class GiudeActivity extends Activity {
private List<View> guideViews;
private ViewPager vpGuide;
private int[] guide_dots = {R.id.ivGuide_dot1, R.id.ivGuide_dot2, R.id.ivGuide_dot3, R.id.ivGuide_dot4};
private ImageView[] dots;
private TextView tvGo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_giude);
initGuideViews();
initDots();
setListeners();
}
private void initGuideViews() {
vpGuide = (ViewPager) findViewById(R.id.vpGuide);
guideViews = new ArrayList<>();
LayoutInflater inflater = LayoutInflater.from(this);
guideViews.add(inflater.inflate(R.layout.view1_of_pager, null));
guideViews.add(inflater.inflate(R.layout.view2_of_pager, null));
guideViews.add(inflater.inflate(R.layout.view3_of_pager, null));
// 第四屏需要拿到 tvGo,所以单独 inflate 出來
View view4 = inflater.inflate(R.layout.view4_of_pager, null);
guideViews.add(view4);
tvGo = (TextView) view4.findViewById(R.id.tvGo);
vpGuide.setAdapter(new GuideAdapter(guideViews));
}
private void initDots() {
dots = new ImageView[4];
for (int i = 0; i < dots.length; i++) {
dots[i] = (ImageView) findViewById(guide_dots[i]);
}
}
private void setListeners() {
vpGuide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float offset, int offsetPixels) { }
@Override
public void onPageSelected(int position) {
// 切換圓點選中態
for (int i = 0; i < dots.length; i++) {
dots[i].setBackgroundResource(
i == position ? R.drawable.bg_point_selected : R.drawable.bg_point);
}
}
@Override public void onPageScrollStateChanged(int state) { }
});
tvGo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(GiudeActivity.this, LoginActivity.class));
finish();
}
});
}
}
記得在清單里把 WelcomeActivity 配為 LAUNCHER 入口,並給兩個 Activity 都加上合適的 theme。
這一節涉及的技術點都不難,但組合起來剛好覆蓋了幾個高頻場景:AlphaAnimation 做啟動過渡、ViewPager + PagerAdapter 拼引導流、shape 與 selector 做無圖形狀與點擊反饋、以及通過 OnPageChangeListener 同步指示器狀態。把這套模板沉澱下來,之後任何 App 的引導頁都能快速套用。
Android 高仿課程表布局實作詳解
這個範例會實作一個類似課程表的畫面,包含年份、週次選擇、星期標題、節次標示,以及以 GridView 呈現課程內容。整體結構適合拿來練習自訂 View、Adapter 與
Android 相機拍照、相冊選圖與圖片裁剪的完整實作
「點一下頭像,拍照或從相冊挑一張,裁一裁換上去」——這個看似簡單的流程,實際上牽涉自定義圓形 View、兩種 Intent、裁剪參數與權限申請。本文把它拆成六個步驟走一遍
ListView 多類型項目的實作方式
在實際開發中,ListView 常需要顯示多種不同樣式的項目,例如字母標題與一般內容列。要實作這類效果,除了基本 Adapter 外,還需要覆寫項目類型相關方法。 基本
深入解析 Android 的 MVC 設計模式實戰
轉眼間,我與 Android 開發已相伴走過了兩年時光。這段日子裡,我基本把 Android 的基礎知識摸了个透。可越往深處鑽研,越感到吃力——一方面自以為 Androi