編輯:關於 Android 程式設計
先看效果:

這個菜單由三層同心圓構成,最內圈是小房子圖標(level1),中間灰色的那一圈是 level2,最外圈則是 level3。點擊不同的圖標,會以旋轉動畫的方式把某一層旋入或旋出。
三層其實都是同一個套路,這裡用 RelativeLayout,方便後續把小圖標疊加上去。核心屬性就是 centerInParent 和 alignParentBottom。
唯一要留意的是外圈小圖標的排列方式:左半邊的圖標以最左邊那一個為基準,右半邊則以最右邊那一個為基準,分別是 iv_channel1 與 iv_channel7。這樣只要基準圖標的位置對了,其餘的相對偏移跟著就對。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 第一層:小房子 -->
<RelativeLayout android:id="@+id/level1"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level1" >
<ImageView android:id="@+id/iv_icon_home"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:src="@drawable/icon_home" />
</RelativeLayout>
<!-- 第二層 -->
<RelativeLayout android:id="@+id/level2"
android:layout_width="180dp"
android:layout_height="90dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level2" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:src="@drawable/icon_search" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:src="@drawable/icon_myyouku" />
<ImageView android:id="@+id/iv_icon_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:src="@drawable/icon_menu" />
</RelativeLayout>
<!-- 第三層:外圈頻道 -->
<RelativeLayout android:id="@+id/level3"
android:layout_width="260dp"
android:layout_height="130dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level3" >
<ImageView android:id="@+id/iv_channel1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:src="@drawable/channel1"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:id="@+id/iv_channel2"
android:layout_above="@id/iv_channel1"
android:layout_alignLeft="@id/iv_channel1"
android:layout_marginBottom="7dp"
android:layout_marginLeft="18dp"
android:src="@drawable/channel2"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:id="@+id/iv_channel3"
android:layout_above="@id/iv_channel2"
android:layout_alignLeft="@id/iv_channel2"
android:layout_marginBottom="2dp"
android:layout_marginLeft="35dp"
android:src="@drawable/channel3"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:id="@+id/iv_channel4"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:src="@drawable/channel4"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:id="@+id/iv_channel7"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="5dp"
android:src="@drawable/channel7"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:id="@+id/iv_channel6"
android:layout_above="@id/iv_channel7"
android:layout_alignRight="@id/iv_channel7"
android:layout_marginBottom="7dp"
android:layout_marginRight="18dp"
android:src="@drawable/channel6"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:id="@+id/iv_channel5"
android:layout_above="@id/iv_channel6"
android:layout_alignRight="@id/iv_channel6"
android:layout_marginBottom="2dp"
android:layout_marginRight="35dp"
android:src="@drawable/channel5"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
佈局完成後的靜態樣子:

動手寫動畫之前,先把行為規則定清楚:
1、點擊小房子圖標:若 level2 與 level3 目前都是顯示的,就兩層一起隱藏;若兩層都不在,則只把 level2 顯示出來;若只有 level2 顯示著,則把 level2 隱藏。
2、點擊 level2 上的「≡」圖標:切換 level3 的顯示與隱藏。
3、按下手機 Menu 鍵:三層都顯示時全部隱藏;只有 level1、level2 顯示時隱藏這兩層;只有 level1 顯示時隱藏 level1;完全沒有層顯示時,則把 level1 與 level2 顯示出來。
4、以上所有顯示與隱藏的過程,都使用動畫呈現。
邏輯理清之後,真正的難點變成了動畫本身。這裡需要用到 RotateAnimation,但因為牽涉到「旋出」與「旋入」兩種效果,就必須先搞清楚角度該怎麼給。
經過實測可以發現,RotateAnimation 的角度是依照順時針方向遞增的:

那麼,如果要讓組件「順時針旋出」,角度範圍是多少?「順時針旋入」又是什麼?

從圖上可以比對出來:順時針旋出是 0 到 180 度;而旋入並不是直覺以為的 180 到 0 度,正確的寫法是 180 到 360 度。這個細節如果搞錯,動畫會變成逆向轉回去,看起來就很怪。
三層的動畫行為完全一致,因此抽成一個工具類、提供靜態方法來複用:
package com.alexchen.youkumenuexercise.utils;
import android.view.View;
import android.view.animation.RotateAnimation;
public class AnimationUtils {
// 旋出動畫,無延遲
public static void startAnimationOut(View view) {
startAnimationOut(view, 0);
}
// 旋入動畫,無延遲
public static void startAnimationIn(View view) {
startAnimationIn(view, 0);
}
/**
* 旋出動畫
* @param delay 動畫延遲的時間,單位毫秒
*/
public static void startAnimationOut(View view, long delay) {
RotateAnimation animation = new RotateAnimation(
0, 180, view.getWidth() / 2, view.getHeight());
animation.setDuration(500);
animation.setStartOffset(delay);
animation.setFillAfter(true);
view.startAnimation(animation);
}
/**
* 旋入動畫
* @param delay 動畫延遲的時間,單位毫秒
*/
public static void startAnimationIn(View view, long delay) {
RotateAnimation animation = new RotateAnimation(
180, 360, view.getWidth() / 2, view.getHeight());
animation.setDuration(500);
animation.setStartOffset(delay);
animation.setFillAfter(true);
view.startAnimation(animation);
}
}
這裡刻意提供了帶延遲參數的版本,目的是讓三層在進出時拉出時間差,呈現出層層展開的層次感,操作起來的質感會好上不少。另外 setFillAfter(true) 是必須的,否則動畫播完會瞬間跳回起始角度。
package com.alexchen.youkumenuexercise;
import com.alexchen.youkumenuexercise.utils.AnimationUtils;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity implements OnClickListener {
private RelativeLayout level1;
private RelativeLayout level2;
private RelativeLayout level3;
private ImageView iv_icon_menu;
private ImageView iv_icon_home;
/** level3 是否處於顯示狀態,預設 true */
private boolean isLevel3In;
private boolean isLevel2In;
private boolean isLevel1In;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
isLevel3In = true;
isLevel2In = true;
isLevel1In = true;
initViews();
setOnclickListenersForViews();
}
/** 初始化 view 組件 */
private void initViews() {
level1 = (RelativeLayout) findViewById(R.id.level1);
level2 = (RelativeLayout) findViewById(R.id.level2);
level3 = (RelativeLayout) findViewById(R.id.level3);
iv_icon_menu = (ImageView) findViewById(R.id.iv_icon_menu);
iv_icon_home = (ImageView) findViewById(R.id.iv_icon_home);
}
/** 設置監聽事件 */
private void setOnclickListenersForViews() {
iv_icon_home.setOnClickListener(this);
iv_icon_menu.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_icon_menu:
if (isLevel3In) {
AnimationUtils.startAnimationOut(level3);
isLevel3In = false;
} else {
AnimationUtils.startAnimationIn(level3);
isLevel3In = true;
}
break;
case R.id.iv_icon_home:
if (isLevel2In) {
AnimationUtils.startAnimationOut(level2, 200);
isLevel2In = false;
} else {
AnimationUtils.startAnimationIn(level2);
isLevel2In = true;
}
if (isLevel3In) {
AnimationUtils.startAnimationOut(level3);
isLevel3In = false;
}
break;
default:
break;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
changeLevelState();
}
return super.onKeyDown(keyCode, event);
}
/** 改變三個圓環的狀態 */
private void changeLevelState() {
if (isLevel1In) {
AnimationUtils.startAnimationOut(level1, 400);
isLevel1In = false;
} else {
AnimationUtils.startAnimationIn(level1);
isLevel1In = true;
}
if (isLevel2In) {
AnimationUtils.startAnimationOut(level2, 200);
isLevel2In = false;
} else {
AnimationUtils.startAnimationIn(level2, 200);
isLevel2In = true;
}
if (isLevel3In) {
AnimationUtils.startAnimationOut(level3);
isLevel3In = false;
}
}
}
這裡沒有做任何生命週期相關的處理,只是把功能先跑起來。實際專案中還需要注意兩件事:一是旋轉動畫不會真正改變 View 的可用性與點擊區域,隱藏之後若仍要防止誤觸,記得搭配 setVisibility 處理;二是每層的延遲時間(0、200、400 毫秒)是 manually 調出來的節奏,換成不同尺寸的圖標素材時可能需要重新微調。
Android 頭像更換實戰:相冊選擇、拍照與裁剪上傳
##需求:個人介面的頭像圖片切換 方式一:點擊彈窗切換頭像 — 相冊選擇 二、這裡有兩種方式:從相冊選擇與直接拍照。假設現在是從相冊選擇頭像 * 我在彈窗確
Android 實作:Include 佈局 + Fragment + ViewPager 滑動切換頁面
前面的文章已經講完「隨手拍」專案的圖像處理部分,本篇聚焦主介面的佈局與頁面滑動切換。參考鴻洋的影片教學與郭霖的《第一行代碼》(兩位部落格都強烈推薦),完成了以下內容: (
Android 使用 Matrix 處理 Bitmap:縮放、旋轉、平移與斜切
Matrix 是 Android 中對圖形做變換的核心工具,縮放、旋轉、平移、斜切都靠它。以下整理它的數學結構與常用 API,並附完整示範。 Matrix 的結構 Ma
疑難:部分機型(三星 S3)SQLite 資料庫莫名被鎖住
今天在三星 S3 上開發時遇到一個怪問題:資料庫老是鎖住,換到其他機型卻完全正常。查資料庫所在目錄時發現,除了 db 檔本身,還多出一個同名的、以 -journal 結尾