真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Android衛(wèi)星菜單效果的實(shí)現(xiàn)方法

Android小白第一次寫博客,心情無比激動(dòng)。下面給大家展示一下衛(wèi)星菜單的實(shí)現(xiàn)。

創(chuàng)新互聯(lián)公司主營南安網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,手機(jī)APP定制開發(fā),南安h5小程序設(shè)計(jì)搭建,南安網(wǎng)站營銷推廣歡迎南安等地區(qū)企業(yè)咨詢

1.簡單介紹衛(wèi)星菜單

在應(yīng)用程序中,有很多展示菜單的方式,但其功能都是大同小異,這樣一來,菜單的美觀以及展示方式就顯的尤為重要,衛(wèi)星菜單就是很不錯(cuò)的一種。下面是本案例的gif圖:

Android衛(wèi)星菜單效果的實(shí)現(xiàn)方法 

2.學(xué)習(xí)本案例需要的知識點(diǎn)

(1)動(dòng)畫

(2)自定義ViewGroup

(3)自定義屬性

a、attr.xml

b、在布局中使用自定義屬性

c、在代碼中獲取自定義屬性值

3.首先分析我們的衛(wèi)星菜單需要那些自定義屬性并書寫代碼

首先,菜單可以顯示在屏幕的四個(gè)角,所以我們需要一個(gè)屬性來確定它的位置,菜單在屏幕的四個(gè)角比較美觀,在這里用到枚舉。

其次,我們還需要一個(gè)展開半徑,因此還需要自定義半徑。

下面是attr.xml

<?xml version="1.0" encoding="utf-8"?>

 
  
  
  
  
 
 
 
  
  
 

4.自定義ViewGroup

–繼承ViewGroup 以相關(guān)屬性

public class SateMenu extends ViewGroup implements View.OnClickListener {
 private int animationTime; //動(dòng)畫時(shí)間
 private int radius; //展開半徑
 private int pos; //從自定義屬性中獲取的菜單位置
 private State state; //菜單狀態(tài)
 private int l = 0, t = 0; //左上值
 private View centerBtn = null; //展開按鈕
 private MenuItemListener menuItemListener; //菜單項(xiàng)點(diǎn)擊監(jiān)聽
 private Position position; //枚舉型菜單位置
 private enum Position { //位置枚舉
  LEFT_TOP, LEFT_BOTTOM, RIGHT_TOP, RIGHT_BOTTOM
 }
 private enum State { //菜單狀態(tài)枚舉
  OPEN, COLSE
 }

–構(gòu)造方法

public SateMenu(Context context) {
  //一個(gè)參數(shù)構(gòu)造方法調(diào)用兩個(gè)參數(shù)構(gòu)造方法
  this(context, null);
 }
 public SateMenu(Context context, AttributeSet attrs) {
  //兩個(gè)參數(shù)構(gòu)造方法調(diào)用三個(gè)個(gè)參數(shù)構(gòu)造方法
  this(context, attrs, 0);
 }
 public SateMenu(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  animationTime = 500; //設(shè)置動(dòng)畫展開時(shí)間
  TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SateMenu, defStyleAttr, 0); //獲取自定義屬性值集合
  radius = (int) a.getDimension(R.styleable.SateMenu_radius,
    TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics())); //獲取半徑并轉(zhuǎn)化為像素值
  state = State.COLSE; //設(shè)置菜單默認(rèn)關(guān)閉
  pos = a.getInt(R.styleable.SateMenu_position, 0); //獲取位置
  //將位置轉(zhuǎn)化為枚舉值 (這樣就把無意義的int轉(zhuǎn)化為有意義的枚舉值)
  switch (pos) {
   case 0:
    position = Position.LEFT_TOP;
    break;
   case 1:
    position = Position.LEFT_BOTTOM;
    break;
   case 2:
    position = Position.RIGHT_TOP;
    break;
   case 3:
    position = Position.RIGHT_BOTTOM;
    break;
  }
 }

–重寫onMeasure方法

@Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  int count = getChildCount();
  //測量子view
  for (int i = 0; i < count; i++) {
   measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
  }
 }

–重寫onLayout方法

 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  if (changed)
   btnLayout();
 }
 private void btnLayout() {
  centerBtn = getChildAt(0);
  if (position == Position.RIGHT_BOTTOM || position == Position.RIGHT_TOP) {
   //如果菜單設(shè)置在屏幕的右側(cè),那么展開按鈕的l值=ViewGroup寬度-按鈕寬度
   l = getMeasuredWidth() - centerBtn.getMeasuredWidth();
  }
  if (position == Position.LEFT_BOTTOM || position == Position.RIGHT_BOTTOM) {
   //如果菜單設(shè)置在屏幕的下邊,那么展開按鈕的t值=ViewGroup高度-按鈕高度
   t = getMeasuredHeight() - centerBtn.getMeasuredHeight();
  }
  //設(shè)置展開按鈕位置
  centerBtn.layout(l, t, l + centerBtn.getMeasuredWidth(), t + centerBtn.getMeasuredHeight());
  childBtnlayout(); //設(shè)置子按鈕位置
  centerBtn.setOnClickListener(this);
 }

–設(shè)置子按鈕位置需要一點(diǎn)點(diǎn)數(shù)學(xué)知識,下面我以主菜單在右下角為例,畫一個(gè)簡圖,圖片對應(yīng)右側(cè)第一個(gè)公式

Android衛(wèi)星菜單效果的實(shí)現(xiàn)方法

 private void childBtnlayout() {
  int childMuneCount = getChildCount() - 1;
  //角度等于90度/子按鈕個(gè)數(shù)-1
  float a = (float) (Math.PI / 2 / (childMuneCount - 1));
  int cl, ct; //分別是子按鈕的 左 上 
  for (int i = 0; i < childMuneCount; i++) {
   if (position == Position.RIGHT_BOTTOM || position == Position.RIGHT_TOP) {
    cl = (int) (l - radius * Math.cos(i * a));
   } else {
    cl = (int) (l + radius * Math.cos(i * a));
   }
   if (position == Position.LEFT_TOP || position == Position.RIGHT_TOP) {
    ct = (int) (t + radius * Math.sin(i * a));
   } else {
    ct = (int) (t - radius * Math.sin(i * a));
   }
   View childView = getChildAt(i + 1);
   childView.layout(cl, ct, cl + childView.getMeasuredWidth(), ct + childView.getMeasuredHeight());
   childView.setOnClickListener(this);
   childView.setTag(i);
   childView.setVisibility(View.GONE);
  }
 }

–動(dòng)畫的展開與關(guān)閉,這里沒有用屬性動(dòng)畫,原理是:當(dāng)用戶關(guān)閉菜單的時(shí)候,將子按鈕隱藏,打開才打的時(shí)候在把子按鈕顯示出來

 private void changeState() {
  int childMuneCount = getChildCount() - 1;
  //設(shè)置展開按鈕旋轉(zhuǎn)動(dòng)畫
  Animation animation = new RotateAnimation(0, 360, centerBtn.getMeasuredWidth() / 2, centerBtn.getMeasuredHeight() / 2);
  animation.setDuration(animationTime);
  centerBtn.setAnimation(animation);
  animation.start();
  View childView;
  //子按鈕有兩個(gè)動(dòng)畫(位移、旋轉(zhuǎn)),所以這里用到動(dòng)畫集,這里也涉及到一些數(shù)學(xué)知識,和之前設(shè)置子按鈕位置差不多
  AnimationSet animationSet;
  Animation translateAnimation;
  Animation rotateAnimation;
  int cl, ct;
  float a = (float) (Math.PI / 2 / (childMuneCount - 1));
  if (state == State.OPEN) {
   state = State.COLSE;
   for (int i = 0; i < childMuneCount; i++) {
    if (position == Position.RIGHT_BOTTOM || position == Position.RIGHT_TOP)
     cl = (int) (radius * Math.cos(i * a));
    else
     cl = (int) (-radius * Math.cos(i * a));
    if (position == Position.LEFT_TOP || position == Position.RIGHT_TOP)
     ct = (int) (-radius * Math.sin(i * a));
    else
     ct = (int) (radius * Math.sin(i * a));
    childView = getChildAt(i + 1);
    childView.setVisibility(View.GONE);
    translateAnimation = new TranslateAnimation(0, cl, 0, ct);
    translateAnimation.setDuration(animationTime);
    rotateAnimation = new RotateAnimation(0, 360, childView.getMeasuredHeight() / 2, childView.getMeasuredHeight() / 2);
    rotateAnimation.setDuration(animationTime);
    animationSet = new AnimationSet(true);
    animationSet.addAnimation(rotateAnimation);
    animationSet.addAnimation(translateAnimation);
    childView.setAnimation(animationSet);
    animationSet.start();
    childView.setVisibility(View.GONE);
   }
  } else {
   state = State.OPEN;
   for (int i = 0; i < childMuneCount; i++) {
    if (position == Position.RIGHT_BOTTOM || position == Position.RIGHT_TOP)
     cl = (int) (radius * Math.cos(i * a));
    else
     cl = (int) (-radius * Math.cos(i * a));
    if (position == Position.LEFT_TOP || position == Position.RIGHT_TOP)
     ct = (int) (-radius * Math.sin(i * a));
    else
     ct = (int) (radius * Math.sin(i * a));
    childView = getChildAt(i + 1);
    childView.setVisibility(View.GONE);
    translateAnimation = new TranslateAnimation(cl, 0, ct, 0);
    translateAnimation.setDuration(animationTime);
    rotateAnimation = new RotateAnimation(360, 0, childView.getMeasuredHeight() / 2, childView.getMeasuredHeight() / 2);
    rotateAnimation.setDuration(animationTime);
    animationSet = new AnimationSet(true);
    animationSet.addAnimation(rotateAnimation);
    animationSet.addAnimation(translateAnimation);
    childView.setAnimation(animationSet);
    animationSet.start();
    childView.setVisibility(View.VISIBLE);
   }
  }
 }

–寫到這里我們的衛(wèi)星菜單已經(jīng)可以展現(xiàn)出來的,運(yùn)行一下,效果還是不錯(cuò)的。美中不足的是,子按鈕還沒有點(diǎn)擊事件,下面我們就將這個(gè)小小的不足補(bǔ)充一下。我們可以通過給子按鈕添加點(diǎn)擊事件來監(jiān)聽它,但點(diǎn)擊之后要做的事情不可能寫在ViewGroup中,這就需要用接口進(jìn)行回調(diào)。大家看一下在設(shè)置子按鈕位置的時(shí)候有這樣一句代碼 childView.setTag(i); 它的目的就是給子按鈕添加索引,接下來看一下具體怎樣實(shí)現(xiàn)的。

 @Override
 public void onClick(View v) {
  if (v.getId() == centerBtn.getId()) {
   changeState();
  } else {
   if (menuItemListener != null) {
    menuItemListener.onclick((Integer) v.getTag());
   }
  }
 }
 public interface MenuItemListener {
  void onclick(int position);
 }
 public void setMenuItemListener(MenuItemListener menuItemListener) {
  this.menuItemListener = menuItemListener;
 }

–到這里我們已經(jīng)完全實(shí)現(xiàn)了衛(wèi)星菜單的所有功能,但大家有沒有發(fā)現(xiàn),一些菜單在展開之后,我們點(diǎn)擊其他區(qū)域,菜單會(huì)自動(dòng)收起來,所以我們還要給我們的ViewGroup添加onTouchEvent事件,在菜單展開的時(shí)候,他把菜單收起來,并將此次點(diǎn)擊攔截。

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  if (state == State.OPEN) {
   changeState();
   return true; //攔截
  }
  return super.onTouchEvent(event);
 }

5.下面試用一下我們編寫的衛(wèi)星菜單,看一下成果。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

 
  
  
  
  
  
 

MainActivity.java

public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  SateMenu sateMenu = (SateMenu) findViewById(R.id.menu_id);
  sateMenu.setMenuItemListener(new SateMenu.MenuItemListener() {
   @Override
   public void onclick(int position) {
    Toast.makeText(MainActivity.this, "-- "+position, Toast.LENGTH_SHORT).show();
   }
  });
 }
}

以上所述是小編給大家介紹的Android衛(wèi)星菜單效果的實(shí)現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!


網(wǎng)站題目:Android衛(wèi)星菜單效果的實(shí)現(xiàn)方法
URL鏈接:http://weahome.cn/article/ihshjs.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部