這一篇文在上一篇的基礎(chǔ)上做的功能修改(上一篇地址:http://www.reibang.com/p/ecea65f4015d)
效果圖如下:
主要的修改有下面幾個(gè)方面:
1.主菜單有五個(gè)赞警,點(diǎn)擊第三個(gè)會(huì)打開星級(jí)菜單
2.在星級(jí)菜單打開的情況科展,點(diǎn)擊其余的主菜單會(huì)關(guān)閉星級(jí)菜單
3.主菜單和星級(jí)菜單的位置不一樣了峭跳,星級(jí)菜單這次是180度的區(qū)域
4.星級(jí)菜單的移動(dòng)動(dòng)畫的路徑不一樣
1.前面的步驟還是一樣,自定義屬性,自定義view,繪制xml布局,在自定義的view里面獲取半徑霎匈,重寫onMeasure方法
2.重寫onLayout方法
測(cè)量主button的位置
private void layoutCenter2() {
LinearLayout mainView = (LinearLayout) getChildAt(0);
int width = mainView.getMeasuredWidth();
int height = mainView.getMeasuredHeight();
int t = getMeasuredHeight() - height;
mainView.layout(0, t, width, getMeasuredHeight());
mCButton = findViewById(R.id.btnCenter);
findViewById(R.id.btnMain1).setOnClickListener(this);
findViewById(R.id.btnMain2).setOnClickListener(this);
findViewById(R.id.btnMain3).setOnClickListener(this);
findViewById(R.id.btnMain4).setOnClickListener(this);
mCButton.setOnClickListener(this);
}
測(cè)量星級(jí)菜單的位置
/**
* 設(shè)置itemMenu的位置
*/
private void layoutIButtom() {
int cout = getChildCount();
for (int i = 0; i < cout - 1; i++) {
View childView = getChildAt(i + 1);
int width = childView.getMeasuredWidth();
int height = childView.getMeasuredHeight();
//相對(duì)坐標(biāo)
int rlx = (int) (mRadius * Math.cos(Math.PI / cout * (i + 1)));
int rly = (int) (mRadius * Math.sin(Math.PI / cout * (i + 1)));
int l = getMeasuredWidth() / 2 + rlx - width / 2;
int t = getMeasuredHeight() - rly - height;
childView.layout(l, t, l + width, t + height);
childView.setVisibility(GONE);
}
}
3.點(diǎn)擊中間的星級(jí)菜單的動(dòng)畫效果,主菜單還是一樣的送爸,星級(jí)菜單會(huì)有點(diǎn)區(qū)別
/**
* mune的動(dòng)畫效果
*
* @param duration
*/
private void toggleItemMenu(int duration) {
int count = getChildCount();
for (int i = 0; i < count - 1; i++) {
final View childView = getChildAt(i + 1);
childView.setVisibility(VISIBLE);
int rlx = (int) (mRadius * Math.cos(Math.PI / count * (i + 1)));
int rly = (int) (mRadius * Math.sin(Math.PI / count * (i + 1)));
AnimationSet animationSet = new AnimationSet(true);
//位移動(dòng)畫
TranslateAnimation translateAnimation = null;
//打開
if (mCurrentStatus == State.CLOSE) {
translateAnimation = new TranslateAnimation(-rlx, 0f, rly, 0f);
childView.setEnabled(true);
childView.setFocusable(true);
} else {//關(guān)閉
translateAnimation = new TranslateAnimation(0f, -rlx, 0f, rly);
}
translateAnimation.setDuration(duration);
translateAnimation.setFillAfter(true);
//旋轉(zhuǎn)動(dòng)畫
RotateAnimation rotateAnimation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(duration);
rotateAnimation.setFillAfter(true);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(translateAnimation);
childView.startAnimation(animationSet);
animationSet.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if (mCurrentStatus == State.CLOSE) {
childView.setVisibility(GONE);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
final int pos = i + 1;
childView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onMenuItemClick != null) {
onMenuItemClick.onItemMenuItemClick(pos);
}
onMenuItemAnmiate(pos - 1);
changeMenuStatus();
}
});
}
changeMenuStatus();
}
其余的效果都差不多铛嘱,項(xiàng)目地址和上篇的項(xiàng)目地址是一樣的