自定义控件——旋转菜单
生活随笔
收集整理的這篇文章主要介紹了
自定义控件——旋转菜单
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. 效果圖
2. 布局文件
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><!--這里有個(gè)坑:由于View的層級(jí)關(guān)系,這樣要先放三級(jí)菜單、再放二級(jí)菜單、最后放一級(jí)菜單,--><!--否則就會(huì)出現(xiàn)一級(jí)菜單和二級(jí)菜單的點(diǎn)擊事件被蓋住了--><!--三級(jí)菜單--><RelativeLayoutandroid:id="@+id/rl_level3"android:layout_alignParentBottom="true"android:layout_centerInParent="true"android:background="@mipmap/level3"android:layout_width="250dp"android:layout_height="125dp"><ImageViewandroid:id="@+id/iv_channel1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/channel1"android:layout_alignParentBottom="true"android:layout_marginBottom="16dp"android:layout_marginLeft="16dp"/><ImageViewandroid:id="@+id/iv_channel2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/channel2"android:layout_toRightOf="@+id/iv_channel1"android:layout_above="@+id/iv_channel1"android:layout_marginBottom="20dp"android:layout_marginLeft="5dp"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/channel3"android:layout_toRightOf="@+id/iv_channel2"android:layout_above="@+id/iv_channel2"android:layout_marginBottom="3dp"android:layout_marginLeft="5dp"/><ImageViewandroid:id="@+id/iv_channel4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/channel4"android:layout_centerHorizontal="true"android:layout_marginTop="6dp"/><ImageViewandroid:id="@+id/iv_channel5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/channel5"android:layout_toLeftOf="@+id/iv_channel6"android:layout_above="@+id/iv_channel6"android:layout_marginBottom="3dp"android:layout_marginLeft="5dp"/><ImageViewandroid:id="@+id/iv_channel6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/channel2"android:layout_toLeftOf="@+id/iv_channel7"android:layout_above="@+id/iv_channel1"android:layout_marginBottom="20dp"android:layout_marginLeft="5dp"/><ImageViewandroid:id="@+id/iv_channel7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/channel7"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:layout_marginBottom="16dp"android:layout_marginRight="16dp"/></RelativeLayout><!--二級(jí)菜單--><RelativeLayoutandroid:id="@+id/rl_level2"android:background="@mipmap/level2"android:layout_width="150dp"android:layout_height="75dp"android:layout_alignParentBottom="true"android:layout_centerInParent="true"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/icon_search"android:layout_alignParentBottom="true"android:layout_marginLeft="16dp"android:layout_marginBottom="16dp"/><ImageViewandroid:id="@+id/iv_menu"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/icon_menu"android:layout_alignParentTop="true"android:layout_centerInParent="true"android:layout_marginTop="6dp"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/icon_myyouku"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:layout_marginRight="16dp"android:layout_marginBottom="16dp"/></RelativeLayout><!--一級(jí)菜單--><RelativeLayoutandroid:id="@+id/rl_level1"android:layout_alignParentBottom="true"android:layout_centerInParent="true"android:layout_width="80dp"android:layout_height="40dp"android:background="@mipmap/level1"><ImageViewandroid:id="@+id/iv_home"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerInParent="true"android:layout_marginBottom="6dp"android:background="@mipmap/icon_home"/></RelativeLayout></RelativeLayout>3. 動(dòng)畫控制邏輯
public class MainActivity extends AppCompatActivity implements View.OnClickListener {private ImageView mIv_home;private ImageView mIv_menu;private RelativeLayout mRl_level2;private RelativeLayout mRl_level3;boolean isMenu2Display = true;//記錄二級(jí)菜單是否顯示boolean isMenu3Display = true;//記錄三級(jí)菜單是否顯示@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView() {mIv_home = (ImageView) findViewById(R.id.iv_home);mIv_menu = (ImageView) findViewById(R.id.iv_menu);mRl_level2 = (RelativeLayout) findViewById(R.id.rl_level2);mRl_level3 = (RelativeLayout) findViewById(R.id.rl_level3);mIv_home.setOnClickListener(this);mIv_menu.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.iv_home://判斷當(dāng)前是否有動(dòng)畫正在執(zhí)行,如果有就返回if (AnimUtil.animationNum > 0) {return;}//點(diǎn)擊home按鈕,隱藏或顯示三級(jí)菜單int offset = 0;if (isMenu3Display) {AnimUtil.hideAnim(mRl_level3);isMenu3Display = false;offset += 200;}//點(diǎn)擊home按鈕,隱藏或顯示二級(jí)菜單if (isMenu2Display) {AnimUtil.hideAnim(mRl_level2,offset);} else {AnimUtil.showAnim(mRl_level2);}isMenu2Display = !isMenu2Display;break;case R.id.iv_menu://判斷當(dāng)前是否有動(dòng)畫正在執(zhí)行,如果有就返回if (AnimUtil.animationNum > 0) {return;}//點(diǎn)擊menu按鈕,隱藏或顯示三級(jí)菜單if (isMenu3Display) {AnimUtil.hideAnim(mRl_level3);} else {AnimUtil.showAnim(mRl_level3);}isMenu3Display = !isMenu3Display;break;}} }4. 動(dòng)畫工具類及bug分析
public class AnimUtil {/*** bug2:如果當(dāng)前動(dòng)畫沒(méi)有結(jié)束立馬有點(diǎn)擊了,那么就會(huì)出現(xiàn)動(dòng)畫的跳轉(zhuǎn)* 解決方案:使用動(dòng)畫監(jiān)聽(tīng)去控制*/private static MyAnimationListener mAnimationListener = new MyAnimationListener();public static int animationNum = 0;//記錄當(dāng)前有多少動(dòng)畫在執(zhí)行/*** 隱藏動(dòng)畫* @param container 傳入的ViewGroup* 隱藏動(dòng)畫分析:* 開(kāi)始角度:0 ;結(jié)束角度 180;旋轉(zhuǎn)中心點(diǎn):(寬度的一般,整個(gè)高度)*/public static void hideAnim(ViewGroup container) {hideAnim(container,0);}/*** 隱藏動(dòng)畫* @param container 傳入的ViewGroup* @param offset 動(dòng)畫延遲執(zhí)行的時(shí)間* 隱藏動(dòng)畫分析:* 開(kāi)始角度:0 ;結(jié)束角度 180;旋轉(zhuǎn)中心點(diǎn):(寬度的一般,整個(gè)高度)*/public static void hideAnim(ViewGroup container,int offset) {/*** bug1:補(bǔ)間動(dòng)畫沒(méi)有改變實(shí)際的位置,點(diǎn)擊事件還留在原地* 解決方案:隱藏動(dòng)畫時(shí)將當(dāng)前容器中的控件全部設(shè)置為不可點(diǎn)擊*/for (int i = 0; i < container.getChildCount(); i++) {View childView = container.getChildAt(i);childView.setEnabled(false);//設(shè)置控件不可用,相當(dāng)于點(diǎn)擊事件取消了}RotateAnimation rotateAnimation = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);rotateAnimation.setDuration(500);//設(shè)置動(dòng)畫執(zhí)行的時(shí)間rotateAnimation.setFillAfter(true);//讓視圖執(zhí)行完補(bǔ)間動(dòng)畫之后停留在結(jié)束的位置rotateAnimation.setStartOffset(offset);rotateAnimation.setAnimationListener(mAnimationListener);container.startAnimation(rotateAnimation);}/*** 顯示動(dòng)畫* @param container 傳入的ViewGroup* 顯示動(dòng)畫分析:* 開(kāi)始角度:-180 ;結(jié)束角度 0;旋轉(zhuǎn)中心點(diǎn):(寬度的一般,整個(gè)高度)*/public static void showAnim(ViewGroup container) {/*** bug1:補(bǔ)間動(dòng)畫沒(méi)有改變實(shí)際的位置,點(diǎn)擊事件還留在原地* 解決方案:顯示動(dòng)畫時(shí)將當(dāng)前容器中的控件全部設(shè)置為可點(diǎn)擊*/for (int i = 0; i < container.getChildCount(); i++) {View childView = container.getChildAt(i);childView.setEnabled(true);}RotateAnimation rotateAnimation = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);rotateAnimation.setDuration(500);//設(shè)置動(dòng)畫執(zhí)行的時(shí)間rotateAnimation.setFillAfter(true);//讓視圖執(zhí)行完補(bǔ)間動(dòng)畫之后停留在結(jié)束的位置rotateAnimation.setAnimationListener(mAnimationListener);container.startAnimation(rotateAnimation);}private static class MyAnimationListener implements Animation.AnimationListener {@Overridepublic void onAnimationStart(Animation animation) {animationNum++;}@Overridepublic void onAnimationEnd(Animation animation) {animationNum--;}@Overridepublic void onAnimationRepeat(Animation animation) {}} }非常感謝您的耐心閱讀,希望我的文章對(duì)您有幫助。歡迎點(diǎn)評(píng)、轉(zhuǎn)發(fā)或分享給您的朋友或技術(shù)群。
總結(jié)
以上是生活随笔為你收集整理的自定义控件——旋转菜单的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 第一行代码学习笔记第十章——探究服务
- 下一篇: 自定义控件——轮播广告条