android 开发卫星菜单,android之类似卫星菜单,来自定义ViewGroup。。。。。
這是一個繼承ViewGroup的自定義控件,對于高手來說比較簡單,但對于我們這樣的小白,確實是一個不錯的demo
定義繼承ViewGroup的自定義View ?,需要實現它的兩個基本方法, onMeasure和onLayout,我們在onMeasure中測量控件的大小,在onLayout中設置控件所在的位置。>我們知道在onMeasure中有3中模式,在本例子中由于設置的是match_parent,所以就根據系統測量的size來設定,本例子的onMeasure比較簡單,就是測量子控件
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
for (int i = 0; i < getChildCount(); i++) {
View childView = getChildAt(i);
measureChild(childView, widthMeasureSpec, heightMeasureSpec);
}
}
主要就是布局,不過本例子,布局也比較簡單,先將主menubtn的位置放好,
menuBtn = getChildAt(0);
menuBtn.setOnClickListener(this);
int width = menuBtn.getMeasuredWidth();
int height = menuBtn.getMeasuredHeight();
ml = getMeasuredWidth() - width - OFFSET;
mt = getMeasuredHeight() - height - OFFSET * 3;
menuBtn.layout(ml, mt, getMeasuredWidth() - OFFSET, getMeasuredHeight() - OFFSET * 3);
在循環遍歷子view放置子控件, 這個比較隨意,可以根據自己的喜好,隨意的放置子控件,在這里我用的是一條直線,
for (int i = 0; i < getChildCount() - 1; i++) {
View childView = getChildAt(i + 1);
childView.setVisibility(View.GONE);
int cWidth = childView.getMeasuredWidth();
int cHeight = childView.getMeasuredHeight();
int average = getMeasuredWidth() / getChildCount();
int ct = mt - average * (i + 1);
childView.layout(ml + OFFSET / 2, ct, ml + cWidth + OFFSET / 2, ct + cHeight);
}
最后就是設置 主btn的點擊事件,以及動畫的設置,這也是一個難點,不過,看你像要實現什么樣的動畫了,簡單的動畫,還是比較簡單的。
private void toggleMenu(int duration) {
for (int i = 0; i < getChildCount() - 1; i++) {
final View childView = getChildAt(i + 1);
childView.setVisibility(View.VISIBLE);
int average = getMeasuredWidth() / getChildCount();
int ct = average * (i + 1);
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation = null;
if (mStatus == Status.CLOSE) {// to open
translateAnimation = new TranslateAnimation(0,
0, ct, 0);
childView.setClickable(true);
childView.setFocusable(true);
} else {
translateAnimation = new TranslateAnimation(0,
0, 0, ct);
childView.setClickable(false);
childView.setFocusable(false);
}
translateAnimation.setDuration(duration);
translateAnimation.setFillAfter(true);
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if (mStatus == Status.CLOSE) {
childView.setVisibility(View.GONE);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
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);
final int pos=i+1;
childView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (itemClickListener != null)
itemClickListener.onClick(childView, pos);
menuItemAnimation(pos - 1);
changeStatus();
}
});
}
//切換菜單狀態
changeStatus();
}
最后不要忘了,切換按鈕的狀態。
本例子還有個就是監聽事件的回調,我想這個大家都會把。還是比較簡答的。
由于不會制作gif動畫,就用下面的兩張靜態的湊合著吧,有會制作gif的簡便方法嗎?
求告訴
具體源碼下載地址 ?:? ??http://download.csdn.net/detail/qq_21840193/8977311 ??點擊打開鏈接
Github下載地址 ?: ?https://github.com/sweethearting/ArcMenu
總結
以上是生活随笔為你收集整理的android 开发卫星菜单,android之类似卫星菜单,来自定义ViewGroup。。。。。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android mat下载地址,MatL
- 下一篇: android 百度地图 在线建议查询,