欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義轉(zhuǎn)盤菜單效果

 更新時間:2020年07月29日 12:23:02   作者:愛碼士_yan  
這篇文章主要為大家詳細介紹了Android自定義轉(zhuǎn)盤菜單效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近由于公司項目需要,需要開發(fā)一款轉(zhuǎn)盤菜單,費了好大功夫搞出來了,下面分享下

樣圖

具體功能如下:

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.hitomi.smlibrary.OnSpinMenuStateChangeListener;
import com.hitomi.smlibrary.TurnTableMenu;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

  private TurnTableMenu turnTableMenu;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    turnTableMenu = (TurnTableMenu) findViewById(R.id.spin_menu);

    // 設(shè)置頁面標題
    List<String> hintStrList = new ArrayList<>();
    hintStrList.add("熱門信息");
    hintStrList.add("實時新聞");
    hintStrList.add("我的論壇");
    hintStrList.add("我的信息");
    hintStrList.add("環(huán)游世界");
    hintStrList.add("閱讀空間");
    hintStrList.add("歡樂空間");
    hintStrList.add("系統(tǒng)設(shè)置");

    turnTableMenu.setHintTextStrList(hintStrList);
    turnTableMenu.setHintTextColor(Color.parseColor("#FFFFFF"));
    turnTableMenu.setHintTextSize(14);

    // 設(shè)置頁面適配器
    final List<Fragment> fragmentList = new ArrayList<>();
    fragmentList.add(Fragment1.newInstance());
    fragmentList.add(Fragment2.newInstance());
    fragmentList.add(Fragment3.newInstance());
    fragmentList.add(Fragment4.newInstance());
    fragmentList.add(Fragment5.newInstance());
    fragmentList.add(Fragment6.newInstance());
    fragmentList.add(Fragment7.newInstance());
    fragmentList.add(Fragment8.newInstance());
    FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
      @Override
      public Fragment getItem(int position) {
        return fragmentList.get(position);
      }

      @Override
      public int getCount() {
        return fragmentList.size();
      }
    };
    turnTableMenu.setFragmentAdapter(fragmentPagerAdapter);

    // 設(shè)置菜單狀態(tài)改變時的監(jiān)聽器
    turnTableMenu.setOnSpinMenuStateChangeListener(new OnSpinMenuStateChangeListener() {
      @Override
      public void onMenuOpened() {
        Toast.makeText(MainActivity.this, "SpinMenu opened", Toast.LENGTH_SHORT).show();
      }

      @Override
      public void onMenuClosed() {
        Toast.makeText(MainActivity.this, "SpinMenu closed", Toast.LENGTH_SHORT).show();
      }
    });
  }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<com.hitomi.smlibrary.TurnTableMenu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/spin_menu"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  app:hint_text_color="#FFFFFF"
  app:hint_text_size="14sp"
  app:scale_ratio="0.36"
  tools:context="com.hitomi.spinmenu.MainActivity">

  <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#333a4a"></FrameLayout>

</com.hitomi.smlibrary.TurnTableMenu>

3.自定義View TurnTableMenu

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.IdRes;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.PagerAdapter;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

public class TurnTableMenu extends FrameLayout {

  static final String TAG = "SpinMenu";

  static final String TAG_ITEM_CONTAINER = "tag_item_container";

  static final String TAG_ITEM_PAGER = "tag_item_pager";

  static final String TAG_ITEM_HINT = "tag_item_hint";

  static final int MENU_STATE_CLOSE = -2;

  static final int MENU_STATE_CLOSED = -1;

  static final int MENU_STATE_OPEN = 1;

  static final int MENU_STATE_OPENED = 2;

  /**
   * 左右菜單 Item 移動動畫的距離
   */
  static final float TRAN_SKNEW_VALUE = 160;

  /**
   * Hint 相對 頁面的上外邊距
   */
  static final int HINT_TOP_MARGIN = 15;

  /**
   * 可旋轉(zhuǎn)、轉(zhuǎn)動布局
   */
  private TurnTableMenuLayout turnTableMenuLayout;

  /**
   * 菜單打開關(guān)閉動畫幫助類
   */
  private TurnTableMenuAnimator turnTableMenuAnimator;

  /**
   * 頁面適配器
   */
  private PagerAdapter pagerAdapter;

  /**
   * 手勢識別器
   */
  private GestureDetectorCompat menuDetector;

  /**
   * 菜單狀態(tài)改變監(jiān)聽器
   */
  private OnSpinMenuStateChangeListener onSpinMenuStateChangeListener;

  /**
   * 緩存 Fragment 的集合,供 {@link #pagerAdapter} 回收使用
   */
  private List pagerObjects;

  /**
   * 菜單項集合
   */
  private List<SMItemLayout> smItemLayoutList;

  /**
   * 頁面標題字符集合
   */
  private List<String> hintStrList;

  /**
   * 頁面標題字符尺寸
   */
  private int hintTextSize = 14;

  /**
   * 頁面標題字符顏色
   */
  private int hintTextColor = Color.parseColor("#666666");

  /**
   * 默認打開菜單時頁面縮小的比率
   */
  private float scaleRatio = .36f;

  /**
   * 控件是否初始化的標記變量
   */
  private boolean init = true;

  /**
   * 是否啟用手勢識別
   */
  private boolean enableGesture;

  /**
   * 當前菜單狀態(tài),默認為打開
   */
  private int menuState = MENU_STATE_CLOSED;

  /**
   * 滑動與觸摸之間的閥值
   */
  private int touchSlop = 8;

  private OnSpinSelectedListener onSpinSelectedListener = new OnSpinSelectedListener() {
    @Override
    public void onSpinSelected(int position) {
      log("SpinMenu position:" + position);
    }
  };

  private OnMenuSelectedListener onMenuSelectedListener = new OnMenuSelectedListener() {
    @Override
    public void onMenuSelected(SMItemLayout smItemLayout) {
      closeMenu(smItemLayout);
    }
  };

  private GestureDetector.SimpleOnGestureListener menuGestureListener = new GestureDetector.SimpleOnGestureListener() {

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
      if (Math.abs(distanceX) < touchSlop && distanceY < -touchSlop * 3) {
        openMenu();
      }
      return true;
    }
  };

  public TurnTableMenu(Context context) {
    this(context, null);
  }

  public TurnTableMenu(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public TurnTableMenu(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TurnTableMenu);
    scaleRatio = typedArray.getFloat(R.styleable.TurnTableMenu_scale_ratio, scaleRatio);
    hintTextSize = typedArray.getDimensionPixelSize(R.styleable.TurnTableMenu_hint_text_size, hintTextSize);
    hintTextSize = px2Sp(hintTextColor);
    hintTextColor = typedArray.getColor(R.styleable.TurnTableMenu_hint_text_color, hintTextColor);
    typedArray.recycle();

    pagerObjects = new ArrayList();
    smItemLayoutList = new ArrayList<>();
    menuDetector = new GestureDetectorCompat(context, menuGestureListener);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) {
      ViewConfiguration conf = ViewConfiguration.get(getContext());
      touchSlop = conf.getScaledTouchSlop();
    }
  }

  @Override
  protected void onFinishInflate() {
    super.onFinishInflate();

    @IdRes final int smLayoutId = 0x6F060505;
    ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT);
    turnTableMenuLayout = new TurnTableMenuLayout(getContext());
    turnTableMenuLayout.setId(smLayoutId);
    turnTableMenuLayout.setLayoutParams(layoutParams);
    turnTableMenuLayout.setOnSpinSelectedListener(onSpinSelectedListener);
    turnTableMenuLayout.setOnMenuSelectedListener(onMenuSelectedListener);
    addView(turnTableMenuLayout);
  }

  @Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);

    if (init && smItemLayoutList.size() > 0) {
      // 根據(jù) scaleRatio 去調(diào)整菜單中 item 視圖的整體大小
      int pagerWidth = (int) (getMeasuredWidth() * scaleRatio);
      int pagerHeight = (int) (getMeasuredHeight() * scaleRatio);
      SMItemLayout.LayoutParams containerLayoutParams = new SMItemLayout.LayoutParams(pagerWidth, pagerHeight);
      SMItemLayout smItemLayout;
      FrameLayout frameContainer;
      TextView tvHint;
      for (int i = 0; i < smItemLayoutList.size(); i++) {
        smItemLayout = smItemLayoutList.get(i);
        frameContainer = (FrameLayout) smItemLayout.findViewWithTag(TAG_ITEM_CONTAINER);
        frameContainer.setLayoutParams(containerLayoutParams);
        if (i == 0) { // 初始菜單的時候,默認顯示第一個 Fragment
          FrameLayout pagerLayout = (FrameLayout) smItemLayout.findViewWithTag(TAG_ITEM_PAGER);
          // 先移除第一個包含 Fragment 的布局
          frameContainer.removeView(pagerLayout);

          // 創(chuàng)建一個用來占位的 FrameLayout
          FrameLayout holderLayout = new FrameLayout(getContext());
          LinearLayout.LayoutParams pagerLinLayParams = new LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
          holderLayout.setLayoutParams(pagerLinLayParams);

          // 將占位的 FrameLayout 添加到布局中的 frameContainer 中
          frameContainer.addView(holderLayout, 0);

          // 添加 第一個包含 Fragment 的布局添加到 SpinMenu 中
          FrameLayout.LayoutParams pagerFrameParams = new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
          pagerLayout.setLayoutParams(pagerFrameParams);
          addView(pagerLayout);
        }

        // 顯示標題
        if (hintStrList != null && !hintStrList.isEmpty() && i < hintStrList.size()) {
          tvHint = (TextView) smItemLayout.findViewWithTag(TAG_ITEM_HINT);
          tvHint.setText(hintStrList.get(i));
          tvHint.setTextSize(hintTextSize);
          tvHint.setTextColor(hintTextColor);
        }

        // 位于菜單中當前顯示 Fragment 兩邊的 SMItemlayout 左右移動 TRAN_SKNEW_VALUE 個距離
        if (turnTableMenuLayout.getSelectedPosition() + 1 == i
            || (turnTableMenuLayout.isCyclic()
              && turnTableMenuLayout.getMenuItemCount() - i == turnTableMenuLayout.getSelectedPosition() + 1)) { // 右側(cè) ItemMenu
          smItemLayout.setTranslationX(TRAN_SKNEW_VALUE);
        } else if (turnTableMenuLayout.getSelectedPosition() - 1 == i
            || (turnTableMenuLayout.isCyclic()
              && turnTableMenuLayout.getMenuItemCount() - i == 1)) { // 左側(cè) ItemMenu
          smItemLayout.setTranslationX(-TRAN_SKNEW_VALUE);
        } else {
          smItemLayout.setTranslationX(0);
        }
      }
      turnTableMenuAnimator = new TurnTableMenuAnimator(this, turnTableMenuLayout, onSpinMenuStateChangeListener);
      init = false;
      openMenu();
    }
  }

  @Override
  public boolean dispatchTouchEvent(MotionEvent ev) {
    if (enableGesture) menuDetector.onTouchEvent(ev);
    return super.dispatchTouchEvent(ev);
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    if (enableGesture) {
      menuDetector.onTouchEvent(event);
      return true;
    } else {
      return super.onTouchEvent(event);
    }
  }

  /**
   * 根據(jù)手機的分辨率從 px(像素) 的單位轉(zhuǎn)成為 sp
   * @param pxValue
   * @return
   */
  private int px2Sp(float pxValue) {
    final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
    return (int) (pxValue / fontScale + 0.5f);
  }

  private void log(String log) {
    Log.d(TAG, log);
  }

  public void setFragmentAdapter(PagerAdapter adapter) {
    if (pagerAdapter != null) {
      pagerAdapter.startUpdate(turnTableMenuLayout);
      for (int i = 0; i < adapter.getCount(); i++) {
        ViewGroup pager = (ViewGroup) turnTableMenuLayout.getChildAt(i).findViewWithTag(TAG_ITEM_PAGER);
        pagerAdapter.destroyItem(pager, i, pagerObjects.get(i));
      }
      pagerAdapter.finishUpdate(turnTableMenuLayout);
    }

    int pagerCount = adapter.getCount();
    if (pagerCount > turnTableMenuLayout.getMaxMenuItemCount())
      throw new RuntimeException(String.format("Fragment number can't be more than %d", turnTableMenuLayout.getMaxMenuItemCount()));

    pagerAdapter = adapter;

    SMItemLayout.LayoutParams itemLinLayParams = new SMItemLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
    LinearLayout.LayoutParams containerLinlayParams = new LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
    FrameLayout.LayoutParams pagerFrameParams = new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
    LinearLayout.LayoutParams hintLinLayParams = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
    hintLinLayParams.topMargin = HINT_TOP_MARGIN;
    pagerAdapter.startUpdate(turnTableMenuLayout);
    for (int i = 0; i < pagerCount; i++) {
      // 創(chuàng)建菜單父容器布局
      SMItemLayout smItemLayout = new SMItemLayout(getContext());
      smItemLayout.setId(i + 1);
      smItemLayout.setGravity(Gravity.CENTER);
      smItemLayout.setLayoutParams(itemLinLayParams);

      // 創(chuàng)建包裹FrameLayout
      FrameLayout frameContainer = new FrameLayout(getContext());
      frameContainer.setId(pagerCount + i + 1);
      frameContainer.setTag(TAG_ITEM_CONTAINER);
      frameContainer.setLayoutParams(containerLinlayParams);

      // 創(chuàng)建 Fragment 容器
      FrameLayout framePager = new FrameLayout(getContext());
      framePager.setId(pagerCount * 2 + i + 1);
      framePager.setTag(TAG_ITEM_PAGER);
      framePager.setLayoutParams(pagerFrameParams);
      Object object = pagerAdapter.instantiateItem(framePager, i);

      // 創(chuàng)建菜單標題 TextView
      TextView tvHint = new TextView(getContext());
      tvHint.setId(pagerCount * 3 + i + 1);
      tvHint.setTag(TAG_ITEM_HINT);
      tvHint.setLayoutParams(hintLinLayParams);

      frameContainer.addView(framePager);
      smItemLayout.addView(frameContainer);
      smItemLayout.addView(tvHint);
      turnTableMenuLayout.addView(smItemLayout);

      pagerObjects.add(object);
      smItemLayoutList.add(smItemLayout);
    }
    pagerAdapter.finishUpdate(turnTableMenuLayout);
  }

  public void openMenu() {
    if (menuState == MENU_STATE_CLOSED) {
      turnTableMenuAnimator.openMenuAnimator();
    }
  }

  public void closeMenu(SMItemLayout chooseItemLayout) {
    if (menuState == MENU_STATE_OPENED) {
      turnTableMenuAnimator.closeMenuAnimator(chooseItemLayout);
    }
  }

  public int getMenuState() {
    return menuState;
  }

  public void updateMenuState(int state) {
    menuState = state;
  }

  public void setEnableGesture(boolean enable) {
    enableGesture = enable;
  }

  public void setMenuItemScaleValue(float scaleValue) {
    scaleRatio = scaleValue;
  }

  public void setHintTextSize(int textSize) {
    hintTextSize = textSize;
  }

  public void setHintTextColor(int textColor) {
    hintTextColor = textColor;
  }

  public void setHintTextStrList(List<String> hintTextList) {
    hintStrList = hintTextList;
  }

  public void setOnSpinMenuStateChangeListener(OnSpinMenuStateChangeListener listener) {
    onSpinMenuStateChangeListener = listener;
  }

  public float getScaleRatio() {
    return scaleRatio;
  }
}

Github:SlidMenu

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android跳轉(zhuǎn)系統(tǒng)設(shè)置Settings的各個界面詳解

    Android跳轉(zhuǎn)系統(tǒng)設(shè)置Settings的各個界面詳解

    系統(tǒng)設(shè)置Settings中定義的一些常用的各界面ACTION常量,下面這篇文章主要給大家介紹了關(guān)于Android跳轉(zhuǎn)系統(tǒng)設(shè)置Settings的各個界面,文中介紹非常詳細,需要的朋友可以參考下
    2023-01-01
  • Android幾種消息推送方案總結(jié)

    Android幾種消息推送方案總結(jié)

    本文主要總結(jié)下Android平臺下幾種推送方案的基本情況以及優(yōu)缺點,具有一定的參考作用,下面跟著小編一起來看下吧
    2017-01-01
  • Android GPS獲取當前經(jīng)緯度坐標

    Android GPS獲取當前經(jīng)緯度坐標

    這篇文章主要為大家詳細介紹了Android GPS獲取當前經(jīng)緯度坐標,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Android自定義九宮格輸入框

    Android自定義九宮格輸入框

    這篇文章主要為大家詳細介紹了Android自定義九宮格輸入框,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Android異常處理最佳實踐

    Android異常處理最佳實踐

    這篇文章主要為大家詳細介紹了Android異常處理最佳實踐,介紹了一個優(yōu)秀的app異常處理機制包括什么,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Kotlin對象的懶加載方式by?lazy?與?lateinit?異同詳解

    Kotlin對象的懶加載方式by?lazy?與?lateinit?異同詳解

    這篇文章主要為大家介紹了Kotlin對象的懶加載方式by?lazy?與?lateinit?異同詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • Android編程使用WebView實現(xiàn)文件下載功能的兩種方法

    Android編程使用WebView實現(xiàn)文件下載功能的兩種方法

    這篇文章主要介紹了Android編程使用WebView實現(xiàn)文件下載功能的兩種方法,涉及Android基于WebView的相關(guān)文件傳輸與線程操作技巧,需要的朋友可以參考下
    2018-02-02
  • Android Studio自動排版的兩種實現(xiàn)方式

    Android Studio自動排版的兩種實現(xiàn)方式

    這篇文章主要介紹了Android Studio自動排版的兩種實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • android實現(xiàn)定時拍照功能

    android實現(xiàn)定時拍照功能

    這篇文章主要為大家詳細介紹了android實現(xiàn)定時拍照功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android 使用Zbar實現(xiàn)掃一掃功能

    Android 使用Zbar實現(xiàn)掃一掃功能

    這篇文章主要介紹了Android 使用Zbar實現(xiàn)掃一掃功能,本文用的是Zbar實現(xiàn)掃一掃,因為根據(jù)本人對兩個庫的使用比較,發(fā)現(xiàn)Zbar解碼比Zxing速度要快,實現(xiàn)方式也簡單,需要的朋友可以參考下
    2023-03-03

最新評論