android 實現自訂衛星菜單

來源:互聯網
上載者:User

標籤:

看了hyman老師的視頻,聽起來有點迷糊,所以就想把實現衛星菜單的實現總結一下。長話短說,下面總結一下:

一、自訂ViewGroup
1)、自訂屬性檔案

屬性的定義:

<attr name="position">
<enum name="left_top" value="0"/>
<enum name="left_bottom" value="1"/>
<enum name="right_top" value="2"/>
<enum name="right_bottom" value="3"/>
<enum name="center_bottom" value="4"/>
</attr>
<attr name="radius" format="dimension"/>

這裡需要注意的是:如何將屬性檔案和自訂的ViewGroup聯絡起來

通過ArcMenu將其聯絡起來,而ArcMenu就是我們自訂的ViewGroup

<declare-styleable name="ArcMenu">
<attr name="position"/>
<attr name="radius"/>
</declare-styleable>


2)、在布局檔案中使用
這裡需要注意的是:注意命名空間的使用
xmlns:hyman="http://schemas.android.com/apk/res/com.example.arcmenu"
這裡分為兩個部分:
第一個部分是:http://schemas.android.com/apk/res/

 


第二個部分是:com.example.arcmenu(這個可以在AndroidManifest.xml中有自訂的ViewGroup的包名)


3)、在自訂控制項中讀取

建立自訂ViewGroup

public class ArcMenu extends ViewGroup

{

}

然後可以按照下邊的步驟就行相關的操作,實現相應的方法

自訂屬性的讀取方法:

首先需要擷取的自訂的屬性

TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ArcMenu, defStyle, 0);

然後擷取自訂屬性的值

int position = typedArray.getInt(R.styleable.ArcMenu_position, POSITION_RIGHT_BOTTOM);

4)關於自訂控制項的布局

這裡我主要著重講一下主菜單在下面中間位置的布局:

首先獲得主菜單的長度寬度:

int width = mCButton.getMeasuredWidth();
int height = mCButton.getMeasuredHeight();

要想主菜單放到螢幕的中間位置:

則他的x座標為l = getMeasuredWidth() / 2 - width / 2;

y座標為:t = getMeasuredHeight() - height;

最後布局

mCButton.layout(l, t, l + width, t + height);

 

子功能表的布局位置

t1 = (int) (mRadius * Math.sin(Math.PI / count * (i + 1)));
l1= (int) (mRadius * Math.cos(Math.PI / count * (i + 1)));

最終子功能表的座標

l = getMeasuredWidth() / 2 - width / 2 - l1;
t = getMeasuredHeight() - height - t2;

 

子功能表布局

child.layout(l, t, l + width, t + height);


4)、設定主按鈕的旋轉動畫

這裡邊的0.5f 表示的是相對與self的座標位置

RotateAnimation anim = new RotateAnimation(start, end, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
anim.setDuration(duration);
anim.setFillAfter(true);
view.startAnimation(anim);


5)、為menuItem添加平移動畫(這裡需要注意的是:在設定主菜單關閉與開啟狀態的時候,注意其修改的位置)

if (mCurrentStatus == Status.IS_CLOSED)
{
tranAnim = new TranslateAnimation(xflag * cl, 0, yflag * ct, 0);
childView.setClickable(true);
childView.setFocusable(true);
}
else
{
tranAnim = new TranslateAnimation(0, xflag * cl, 0, yflag * ct);
childView.setClickable(false);
childView.setFocusable(false);
}

tranAnim.setFillAfter(true);
tranAnim.setDuration(duration);
tranAnim.setStartOffset((i * 100) / count);
tranAnim.setAnimationListener(new AnimationListener()
{

@Override
public void onAnimationStart(Animation animation)
{

}

@Override
public void onAnimationRepeat(Animation animation)
{

}

@Override
public void onAnimationEnd(Animation animation)
{
if (mCurrentStatus == Status.IS_CLOSED)
{
childView.setVisibility(View.GONE);
}

}
});
RotateAnimation rotateAnim = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnim.setDuration(duration);
rotateAnim.setFillAfter(true);

animset.addAnimation(rotateAnim);
animset.addAnimation(tranAnim);
childView.startAnimation(animset);

這裡需要注意的是:監聽子功能表的動畫監聽事件,當動畫結束的時候,需要修改其主菜單的狀態。


6)、實現menuItem的點擊動畫

這裡就是關於疊加動畫的添加

首先建立動畫集

AnimationSet animationSet = new AnimationSet(true);

其次建立縮小,以及漸層動畫

ScaleAnimation scaleAnim = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
AlphaAnimation alphaAnim = new AlphaAnimation(1f, 0.0f);

最後將動畫添加到動畫集
animationSet.addAnimation(scaleAnim);
animationSet.addAnimation(alphaAnim);

設定時間長度和狀態
animationSet.setDuration(duration);
animationSet.setFillAfter(true);

 

最終如下:

android 實現自訂衛星菜單

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.