編輯:關於 Android 程式設計
直接上代碼。效果是這樣的:中間一個主按鈕,點一下周圍的菜單項以圓心為軸旋轉展開;再點一下旋轉收回。外層還有第二圈菜單,可以分層展開。
package com.example.testroundmenu;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class Main extends Activity {
// 下標分別是從下到上、從左到右
private RelativeLayout rl_1, rl_2, rl_3;
private ImageView iv_rl1_home, iv_rl2_1, iv_rl2_2, iv_rl2_3,
iv_rl3_1, iv_rl3_2, iv_rl3_3, iv_rl3_4, iv_rl3_5, iv_rl3_6, iv_rl3_7;
private boolean isMidleMenuShow = true;
private boolean isOutMenuShow = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
}
private void initView() {
MyListener listener = new MyListener();
rl_1 = (RelativeLayout) findViewById(R.id.rl_1);
rl_2 = (RelativeLayout) findViewById(R.id.rl_2);
rl_3 = (RelativeLayout) findViewById(R.id.rl_3);
iv_rl1_home = (ImageView) findViewById(R.id.iv_home);
iv_rl2_1 = (ImageView) findViewById(R.id.iv_search);
iv_rl2_2 = (ImageView) findViewById(R.id.iv_menu);
iv_rl2_3 = (ImageView) findViewById(R.id.iv_myyouku);
iv_rl3_1 = (ImageView) findViewById(R.id.iv_out_1);
iv_rl3_2 = (ImageView) findViewById(R.id.iv_out_2);
iv_rl3_3 = (ImageView) findViewById(R.id.iv_out_3);
iv_rl3_4 = (ImageView) findViewById(R.id.iv_out_4);
iv_rl3_5 = (ImageView) findViewById(R.id.iv_out_5);
iv_rl3_6 = (ImageView) findViewById(R.id.iv_out_6);
iv_rl3_7 = (ImageView) findViewById(R.id.iv_out_7);
iv_rl1_home.setOnClickListener(listener);
iv_rl2_1.setOnClickListener(listener);
iv_rl2_2.setOnClickListener(listener);
iv_rl2_3.setOnClickListener(listener);
iv_rl3_1.setOnClickListener(listener);
iv_rl3_2.setOnClickListener(listener);
iv_rl3_3.setOnClickListener(listener);
iv_rl3_4.setOnClickListener(listener);
iv_rl3_5.setOnClickListener(listener);
iv_rl3_6.setOnClickListener(listener);
iv_rl3_7.setOnClickListener(listener);
}
/**
* ImageView 的統一點擊監聽
*/
public class MyListener implements OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_home:
if (isMidleMenuShow) {
isMidleMenuShow = false;
animatrHiden(rl_2, 500);
// 如果最外層菜單正在顯示,一起收起
if (isOutMenuShow) {
animatrHiden(rl_3, 1000);
isOutMenuShow = false;
}
} else {
isMidleMenuShow = true;
animatrShow(rl_2, 500);
}
showToast("HOME");
break;
case R.id.iv_menu:
showToast("我的菜單");
if (isOutMenuShow) {
animatrHiden(rl_3, 1000);
isOutMenuShow = false;
} else {
animatrShow(rl_3, 1000);
isOutMenuShow = true;
}
break;
case R.id.iv_search:
showToast("我的搜索");
break;
case R.id.iv_myyouku:
showToast("我的優酷");
break;
case R.id.iv_out_1:
showToast("第一個");
break;
case R.id.iv_out_2:
showToast("第二個");
break;
case R.id.iv_out_3:
showToast("第三個");
break;
case R.id.iv_out_4:
showToast("第四個");
break;
case R.id.iv_out_5:
showToast("第五個");
break;
case R.id.iv_out_6:
showToast("第六個");
break;
case R.id.iv_out_7:
showToast("第七個");
break;
default:
break;
}
}
}
public void showToast(String text) {
Toast.makeText(Main.this, text, Toast.LENGTH_SHORT).show();
}
/**
* 讓視圖動畫顯示出來:180° → 360°
*/
public void animatrShow(View view, long durction) {
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "rotation", 180f, 360f);
// 設置旋轉中心點
view.setPivotX(view.getWidth() / 2);
view.setPivotY(view.getHeight());
objectAnimator.setDuration(durction).start();
}
/**
* 讓視圖動畫隱藏起來:0° → 180°
*/
public void animatrHiden(View view, long durction) {
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "rotation", 0f, 180f);
view.setPivotX(view.getWidth() / 2);
view.setPivotY(view.getHeight());
objectAnimator.setDuration(durction).start();
}
// 下面這兩種是傳統補間動畫,沒有交互效果,僅作對比參考
/**
* 0° → 180°,讓視圖隱藏
*/
public static void startAnimout(RelativeLayout view) {
RotateAnimation rotateAnimation = new RotateAnimation(0, 180,
view.getWidth() / 2, view.getHeight());
rotateAnimation.setDuration(500);
rotateAnimation.setFillAfter(true);
view.startAnimation(rotateAnimation);
}
/**
* 180° → 360°,讓視圖出現
*/
public static void startAnimin(RelativeLayout view) {
RotateAnimation rotateAnimation = new RotateAnimation(180, 360,
view.getWidth() / 2, view.getHeight());
rotateAnimation.setDuration(500);
rotateAnimation.setFillAfter(true);
view.startAnimation(rotateAnimation);
}
}
// 設置旋轉中心點 view.setPivotX(view.getWidth() / 2); view.setPivotY(view.getHeight());
這三行是整個效果的靈魂。setPivotX / setPivotY 決定了旋轉、縮放圍繞哪個點進行。把 Y 軸支點設在 view 的底部(getHeight()),X 軸設在水平中線,菜-menu 項就會以「母按鈕所在位置」為圓心做圓周運動,而不是原地打轉。如果改成 getHeight() / 2,效果立刻就變成繞自身中心旋轉,完全不是想要的樣子。
文件末尾保留的 startAnimout / startAnimin 用的是老式 RotateAnimation。兩者最大的差別在於:補間動畫只是改變了繪製時的變換矩陣,View 自身的真實屬性並沒有變化,所以動畫結束後視圖會「彈回」原位(除非設了 setFillAfter),而且點擊事件的命中區域仍停留在最初的位置,交互上很容易出 bug。屬性動畫則是真正修改了 View 的 rotation 屬性,位置、事件、狀態全部同步更新,這也是它更值得推薦的原因。
想讓效果更細膩,還可以在 ObjectAnimator 上追加 setInterpolator(new OvershootInterpolator()) 做出回彈手感,或用 ViewPropertyAnimator 串聯多個屬性形成更豐富的運動軌跡。
百度地圖 SDK v3.5.0 定位功能詳解:從 LatLng 到定位圖層配置
坐標——LatLng 定位開發中最高頻的概念就是坐標(經緯度),先把基本數據結構弄清楚。 LatLng 是地理坐標的基本結構,構造方法為 LatLng(double la
Android Studio 2.2 的 AIDL 跨進程通信完整配置指南
前言 跨進程通信是 Android 開發中常見需求。本文以「取得外部服務提供的座標資料」為例,說明如何在 Android Studio 2.2 中完成 AIDL 設定,並
MVP 實踐(二):如何把 MVP 用得既優雅又安全
上一篇簡單介紹了 MVP 的概念與基本寫法,我也把它用進了實際專案。其實 MVP 並沒有唯一正解,只要真正理解架構意圖,每個人都能寫出屬於自己的風格。GitHub 上也有
Android 7.0 PowerManagerService 解析(三):updatePowerStateLocked 的五個階段
前面我們已經理清了兩條主線:進程要用電時向 PMS 申請 WakeLock,用完之後釋放。而無論是申請還是釋放,PMS 的最終動作都是同一個——呼叫 updatePowe