編輯:關於 Android 程式設計
一、概述
最近一直在鑽研 Android 自定義 View,主要在看《Android 群英傳》,以及 CSDN 上鴻洋大神和 wing 大神的文章,寫得非常細緻。一時興起,便仿照衛星菜單的效果寫了個自定義 View,分享到博客上,還望各位多多指點。代碼寫得比較粗糙,敬請見諒。(由於是在 Linux 系統下開發的,效果圖直接用手機拍的,畫質一般,大家看個效果就好,切勿吐槽。)
先來看一張效果圖,確實有點不忍直視:

自定義 View 的準備工作:
(1)創建一個繼承自 View 的類;
(2)重寫構造函數;
(3)定義自定義屬性。
(4)重寫 onMeasure()、onLayout() 方法。
好,廢話不多說,正式上菜。
二、具體實現
首先是自定義的 View。這裡我直接繼承了 ViewGroup,構造函數重寫如下:
public MoonView(Context context) { this(context,null); } public MoonView(Context context, AttributeSet attrs) { this(context, attrs,0); } public MoonView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } 由於需要讀取自定義屬性,這裡調用的是含三個參數的構造函數。
自定義屬性方面,我定義了兩個:一個是菜單弧形的半徑,另一個是菜單在屏幕中的位置(可設在左上、左下、右上、右下角)。代碼如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MoonAttrs"> <attr name="mRadius" format="integer"></attr><!--菜單圓形半徑--> <attr name="mPosition"><!--衛星菜單屏幕所在位置--> <enum name="leftTop" value="-2"></enum><!--左上角--> <enum name="leftBottom" value="-1"></enum><!--左下角--> <enum name="rightTop" value="-3"></enum><!--右上角--> <enum name="rightBottom" value="-4"></enum><!--右下角--> </attr> </declare-styleable> </resources>
接著在布局文件中引用這個自定義 View 並配置屬性:
<?xml version="1.0" encoding="utf-8"?> <com.example.liujibin.testmyview3.myView.MoonView xmlns:android="https://schemas.android.com/apk/res/android" xmlns:custom="https://schemas.android.com/apk/res/com.example.liujibin.testmyview3" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" custom:mRadius="400" custom:mPosition="rightBottom" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/sapi_icon_add_account"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/sapi_icon_add_account"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/sapi_icon_add_account"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/sapi_icon_add_account"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/sapi_icon_add_account"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/sapi_icon_add_account"/> </com.example.liujibin.testmyview3.myView.MoonView>
最後,需要在自定義 View 的構造函數中獲取相關屬性值:
public MoonView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //獲取相關屬性 TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MoonAttrs, defStyleAttr,0); mRaius = ta.getInt(R.styleable.MoonAttrs_mRadius,500); position = ta.getInt(R.styleable.MoonAttrs_mPosition,-1); } 完成上述準備後,就可以對組件進行測量和布局了。
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); count = getChildCount()-1; angle = 90/(count-1); int count = getChildCount(); for(int i =0;i< count;i++){ measureChild(getChildAt(i),widthMeasureSpec,heightMeasureSpec); } } 這裡 count 用於獲取按鈕數量(其中一個是中心點,不參與角度計算),angle 則是每個按鈕相對基準線的角度,本例以 90 度為範圍均勻分配。
先把中心點的位置固定好:
@Override protected void onLayout(boolean b, int i, int i1, int i2, int i3) { if(isChanged){ layoutBottom(); } } private void layoutBottom(){ View view = getChildAt(0); switch (position){ case -1: btml = 0; btmt = getMeasuredHeight() - view.getMeasuredHeight(); btmr = view.getMeasuredWidth(); btmb = getMeasuredHeight(); break; case -2: btml = 0; btmt = 0; btmr = view.getMeasuredWidth(); btmb = view.getMeasuredHeight(); break; case -3: btml = getMeasuredWidth() - view.getMeasuredWidth(); btmt = 0; btmr = getMeasuredWidth(); btmb = view.getMeasuredHeight(); break; case -4: btml = getMeasuredWidth() - view.getMeasuredWidth(); btmt = getMeasuredHeight() - view.getMeasuredHeight(); btmr = getMeasuredWidth(); btmb = getMeasuredHeight(); break; } btmWidth = view.getMeasuredWidth(); btmHeight = view.getMeasuredHeight(); view.setOnClickListener(this); view.layout(btml,btmt,btmr,btmb); } position 的取值對照屬性定義即可理解,這段代碼負責固定中心點位置並註冊點擊事件。
接下來給剩下的按鈕布局,並先將它們隱藏:
@Override protected void onLayout(boolean b, int i, int i1, int i2, int i3) { if(isChanged){ layoutBottom(); int count = getChildCount(); for(int k = 0;k < count - 1;k++){ View view = getChildAt(k+1); int childWidth = view.getMeasuredWidth(); int childHeight = view.getMeasuredHeight(); int childX = (int)(mRaius*(Math.sin(angle*(k)*Math.PI/180))); int childY = (int)(mRaius*(Math.cos(angle*(k)*Math.PI/180))); int left = 0; int top = 0; int right = 0; int bottom = 0; switch(position){ case -1: left = childX+btmWidth/2-childWidth/2; top =getMeasuredHeight() - (childY+childHeight/2+btmHeight/2); right = childX+btmWidth/2+childWidth/2; bottom =getMeasuredHeight() - (childY + btmHeight/2) + childHeight/2; break; case -2: left = childX+btmWidth/2-childWidth/2; top =childY-childHeight/2+btmHeight/2; right = childX+btmWidth/2+childWidth/2; bottom = childY + btmHeight/2 + childHeight/2; break; case -3: left = getMeasuredWidth() - (childX+btmWidth/2+childWidth/2); top =childY-childHeight/2+btmHeight/2; right = getMeasuredWidth() - (childX+btmWidth/2+childWidth/2)+childWidth; bottom = childY + btmHeight/2 + childHeight/2; break; case -4: left = getMeasuredWidth() - (childX+btmWidth/2+childWidth/2); top =getMeasuredHeight() - (childY+childHeight/2+btmHeight/2); right = getMeasuredWidth() - (childX+btmWidth/2+childWidth/2)+childWidth; bottom =getMeasuredHeight() - (childY + btmHeight/2) + childHeight/2; break; } view.layout(left,top,right,bottom); view.setVisibility(View.GONE); } } } 現在來實現點擊事件:
@Override public void onClick(View view) { if(isChanged){ int count = getChildCount(); for(int i = 0;i < count - 1;i++){ View childView = getChildAt(i+1); int childX = (int)(mRaius*(Math.sin(angle*(i)*Math.PI/180))); int childY = (int)(mRaius*(Math.cos(angle*(i)*Math.PI/180))); int childWidth = view.getMeasuredWidth(); int childHeight = view.getMeasuredHeight(); int left = 0; int top = 0; TranslateAnimation ta = null; switch(position){ case -1: left = childX+btmWidth/2-childWidth/2; top =getMeasuredHeight() - (childY+childHeight/2+btmHeight/2); ta = new TranslateAnimation(-(left+childView.getMeasuredWidth()),0,getMeasuredHeight()-top,0); break; case -2: left = childX+btmWidth/2-childWidth/2; top =childY-childHeight/2+btmHeight/2; ta = new TranslateAnimation(-(left+childView.getMeasuredWidth()),0,-top,0); break; case -3: left = getMeasuredWidth() - (childX+btmWidth/2+childWidth/2); top =childY-childHeight/2+btmHeight/2; ta = new TranslateAnimation(getMeasuredWidth()-(left+childView.getMeasuredWidth()),0,-top,0); break; case -4: left = getMeasuredWidth() - (childX+btmWidth/2+childWidth/2); top =getMeasuredHeight() - (childY+childHeight/2+btmHeight/2); ta = new TranslateAnimation(getMeasuredWidth()-(left+childView.getMeasuredWidth()),0,getMeasuredHeight()-top,0); break; } ta.setDuration(500); childView.setAnimation(ta); childView.setVisibility(View.VISIBLE); } isChanged = false; }else{ int count = getChildCount(); for(int i = 0;i < count - 1;i++){ View childView = getChildAt(i+1); int childX = (int)(mRaius*(Math.sin(angle*(i)*Math.PI/180))); int childY = (int)(mRaius*(Math.cos(angle*(i)*Math.PI/180))); int childWidth = view.getMeasuredWidth(); int childHeight = view.getMeasuredHeight(); int left = 0; int top = 0; TranslateAnimation ta = null; switch(position){ case -1: left = childX+btmWidth/2-childWidth/2; top =getMeasuredHeight() - (childY+childHeight/2+btmHeight/2); ta = new TranslateAnimation(0,-(left+childView.getMeasuredWidth()),0,getMeasuredHeight()-top); break; case -2: left = childX+btmWidth/2-childWidth/2; top =childY-childHeight/2+btmHeight/2; ta = new TranslateAnimation(0,-(left+childView.getMeasuredWidth()),0,-top); break; case -3: left = getMeasuredWidth() - (childX+btmWidth/2+childWidth/2); top =childY-childHeight/2+btmHeight/2; ta = new TranslateAnimation(0,getMeasuredWidth()-(left+childView.getMeasuredWidth()),0,-top); break; case -4: left = getMeasuredWidth() - (childX+btmWidth/2+childWidth/2); top =getMeasuredHeight() - (childY+childHeight/2+btmHeight/2); ta = new TranslateAnimation(0,getMeasuredWidth()-(left+childView.getMeasuredWidth()),0,getMeasuredHeight()-top); break; } ta.setDuration(500); childView.setAnimation(ta); childView.setVisibility(View.GONE); } isChanged = true; } } 通過點擊控制菜單的顯示與隱藏,並搭配飄動的平移動畫過渡。
四個角落的效果如下:




以上就是本文關於自定義 View 實現衛星菜單效果的淺析,希望對你有所幫助。如有疑問,歡迎留言交流。
Chromium Extension 頁面加載過程分析
Extension Page 的本質 Chromium 的 Extension Page 本質上也是網頁,因此加載流程與一般網頁相似。常見的頁面包括 Background
Android 自訂進度條:只改樣子,不重造輪子
為什麼要繼承 ProgressBar 需要進度條時,先上 GitHub 翻了一圈,發現多數好看的實作都是直接繼承 View,從零打造一個進度條。但盯著那些炫麗的效果想了一
在 Android 手機上用 Terminal IDE 遠端登入你的 Mac 或 Ubuntu
用 Android 手機搭配 Terminal IDE 遠端控制自己的電腦,這件事的爽度出乎意料——尤其是當你躺在床上,還能操作另一個房間裡開著的機器。同样的思路也能套用
Android Studio 快速生成 Setter 與 Getter
在 Android 或 Java 開發中,類別通常需要為欄位產生 Setter 與 Getter。若每個欄位都手動撰寫,不僅費時,也容易出錯。Android Studio