电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Android自定義轉盤菜單效果

瀏覽:52日期:2022-09-23 09:22:42

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

樣圖

Android自定義轉盤菜單效果

具體功能如下:

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); // 設置頁面標題 List<String> hintStrList = new ArrayList<>(); hintStrList.add('熱門信息'); hintStrList.add('實時新聞'); hintStrList.add('我的論壇'); hintStrList.add('我的信息'); hintStrList.add('環游世界'); hintStrList.add('閱讀空間'); hintStrList.add('歡樂空間'); hintStrList.add('系統設置'); turnTableMenu.setHintTextStrList(hintStrList); turnTableMenu.setHintTextColor(Color.parseColor('#FFFFFF')); turnTableMenu.setHintTextSize(14); // 設置頁面適配器 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); // 設置菜單狀態改變時的監聽器 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: 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; /** * 可旋轉、轉動布局 */ private TurnTableMenuLayout turnTableMenuLayout; /** * 菜單打開關閉動畫幫助類 */ private TurnTableMenuAnimator turnTableMenuAnimator; /** * 頁面適配器 */ private PagerAdapter pagerAdapter; /** * 手勢識別器 */ private GestureDetectorCompat menuDetector; /** * 菜單狀態改變監聽器 */ 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; /** * 當前菜單狀態,默認為打開 */ 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) { // 根據 scaleRatio 去調整菜單中 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); // 創建一個用來占位的 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)) { // 右側 ItemMenu smItemLayout.setTranslationX(TRAN_SKNEW_VALUE);} else if (turnTableMenuLayout.getSelectedPosition() - 1 == i || (turnTableMenuLayout.isCyclic() && turnTableMenuLayout.getMenuItemCount() - i == 1)) { // 左側 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); } } /** * 根據手機的分辨率從 px(像素) 的單位轉成為 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++) { // 創建菜單父容器布局 SMItemLayout smItemLayout = new SMItemLayout(getContext()); smItemLayout.setId(i + 1); smItemLayout.setGravity(Gravity.CENTER); smItemLayout.setLayoutParams(itemLinLayParams); // 創建包裹FrameLayout FrameLayout frameContainer = new FrameLayout(getContext()); frameContainer.setId(pagerCount + i + 1); frameContainer.setTag(TAG_ITEM_CONTAINER); frameContainer.setLayoutParams(containerLinlayParams); // 創建 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); // 創建菜單標題 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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 预制直埋蒸汽保温管-直埋管道-聚氨酯发泡保温管厂家 - 唐山市吉祥保温工贸有限公司 | 仓储货架_南京货架_钢制托盘_仓储笼_隔离网_环球零件盒_诺力液压车_货架-南京一品仓储设备制造公司 | 并网柜,汇流箱,电控设备,中高低压开关柜,电气电力成套设备,PLC控制设备订制厂家,江苏昌伟业新能源科技有限公司 | 安全光栅|射频导纳物位开关|音叉料位计|雷达液位计|两级跑偏开关|双向拉绳开关-山东卓信机械有限公司 | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 智成电子深圳tdk一级代理-提供TDK电容电感贴片蜂鸣器磁芯lambda电源代理经销,TDK代理商有哪些TDK一级代理商排名查询。-深圳tdk一级代理 | uv机-uv灯-uvled光固化机-生产厂家-蓝盾机电 | 报警器_家用防盗报警器_烟雾报警器_燃气报警器_防盗报警系统厂家-深圳市刻锐智能科技有限公司 | TPE塑胶原料-PPA|杜邦pom工程塑料、PPSU|PCTG材料、PC/PBT价格-悦诚塑胶 | 安规_综合测试仪,电器安全性能综合测试仪,低压母线槽安规综合测试仪-青岛合众电子有限公司 | 盘式曝气器-微孔曝气器-管式曝气器-曝气盘-斜管填料 | 郑州市前程水处理有限公司 | 代理记账_公司起名核名_公司注册_工商注册-睿婕实业有限公司 | 卧涛科技有限公司科技项目申报公司|高新技术企业申报|专利申请 | 智能汉显全自动量热仪_微机全自动胶质层指数测定仪-鹤壁市科达仪器仪表有限公司 | 柔性输送线|柔性链板|齿形链-上海赫勒输送设备有限公司首页[输送机] | 水厂自动化|污水处理中控系统|水利信息化|智慧水务|智慧农业-山东德艾自动化科技有限公司 | Type-c防水母座|贴片母座|耳机接口|Type-c插座-深圳市步步精科技有限公司 | 汽车整车综合环境舱_军标砂尘_盐雾试验室试验箱-无锡苏南试验设备有限公司 | PVC地板|PVC塑胶地板|PVC地板厂家|地板胶|防静电地板-无锡腾方装饰材料有限公司-咨询热线:4008-798-128 | 厌氧反应器,IC厌氧反应器,厌氧三相分离器-山东创博环保科技有限公司 | 制氮设备-变压吸附制氮设备-制氧设备-杭州聚贤气体设备制造有限公司 | 微信小程序定制,广州app公众号商城网站开发公司-广东锋火 | 银川美容培训-美睫美甲培训-彩妆纹绣培训-新娘化妆-学化妆-宁夏倍莱妮职业技能培训学校有限公司 临时厕所租赁_玻璃钢厕所租赁_蹲式|坐式厕所出租-北京慧海通 | elisa试剂盒价格-酶联免疫试剂盒-猪elisa试剂盒-上海恒远生物科技有限公司 | 首页-恒温恒湿试验箱_恒温恒湿箱_高低温试验箱_高低温交变湿热试验箱_苏州正合 | 陕西华春网络科技股份有限公司 | 磁力反应釜,高压釜,实验室反应釜,高温高压反应釜-威海自控反应釜有限公司 | 上海电子秤厂家,电子秤厂家价格,上海吊秤厂家,吊秤供应价格-上海佳宜电子科技有限公司 | 水厂自动化-水厂控制系统-泵站自动化|控制系统-闸门自动化控制-济南华通中控科技有限公司 | 超细|超微气流粉碎机|气流磨|气流分级机|粉体改性机|磨粉机|粉碎设备-山东埃尔派粉体科技 | 柴油机_柴油发电机_厂家_品牌-江苏卡得城仕发动机有限公司 | 氟氨基酮、氯硝柳胺、2-氟苯甲酸、异香兰素-新晨化工 | 法兰连接型电磁流量计-蒸汽孔板节流装置流量计-北京凯安达仪器仪表有限公司 | 铸铝门厂家,别墅大门庭院大门,别墅铸铝门铜门[十大品牌厂家]军强门业 | led全彩屏-室内|学校|展厅|p3|户外|会议室|圆柱|p2.5LED显示屏-LED显示屏价格-LED互动地砖屏_蕙宇屏科技 | 机构创新组合设计实验台_液压实验台_气动实训台-戴育教仪厂 | ERP企业管理系统永久免费版_在线ERP系统_OA办公_云版软件官网 | 游动电流仪-流通式浊度分析仪-杰普仪器(上海)有限公司 | 座椅式升降机_无障碍升降平台_残疾人升降平台-南京明顺机械设备有限公司 | 危废处理系统,水泥厂DCS集散控制系统,石灰窑设备自动化控制系统-淄博正展工控设备 | pos机办理,智能/扫码/二维码/微信支付宝pos机-北京万汇通宝商贸有限公司 |