最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義控件之仿優(yōu)酷菜單

 更新時(shí)間:2016年06月08日 10:36:27   作者:_江南一點(diǎn)雨  
這篇文章主要為大家詳細(xì)介紹了Android自定義控件之仿優(yōu)酷菜單的相關(guān)資料,感興趣的小伙伴們可以參考一下

去年的優(yōu)酷HD版有過(guò)這樣一種菜單,如下圖:

這里寫圖片描述

應(yīng)用打開之后,先是三個(gè)弧形的三級(jí)菜單,點(diǎn)擊實(shí)體鍵menu之后,這三個(gè)菜單依次旋轉(zhuǎn)退出,再點(diǎn)擊實(shí)體鍵menu之后,一級(jí)菜單會(huì)旋轉(zhuǎn)進(jìn)入,點(diǎn)擊一級(jí)菜單,二級(jí)菜單旋轉(zhuǎn)進(jìn)入,點(diǎn)擊二級(jí)菜單的menu鍵,三級(jí)菜單旋轉(zhuǎn)進(jìn)入,再次點(diǎn)擊二級(jí)菜單的旋轉(zhuǎn)鍵,三級(jí)菜單又會(huì)旋轉(zhuǎn)退出,這時(shí)再點(diǎn)擊一級(jí)菜單,二級(jí)菜單退出,最后點(diǎn)擊實(shí)體menu鍵,一級(jí)菜單退出。

總體來(lái)說(shuō)實(shí)現(xiàn)這樣的功能:
(1)點(diǎn)擊實(shí)體menu鍵時(shí),如果界面上有菜單顯示,不管有幾個(gè),全部依次退出,如果界面上沒(méi)有菜單顯示,則顯示一級(jí)菜單。
(2)點(diǎn)擊一級(jí)菜單的home鍵時(shí),如果此時(shí)界面只有一級(jí)菜單,則顯示二級(jí)菜單,否則讓除了一級(jí)菜單外的菜單全都依次退出。
(3)點(diǎn)擊二級(jí)菜單的menu鍵時(shí),如果三級(jí)菜單已經(jīng)顯示,則讓它旋轉(zhuǎn)退出,如果三級(jí)菜單未顯示則讓它旋轉(zhuǎn)進(jìn)入。

好了,今天我們主要實(shí)現(xiàn)上述效果。

先來(lái)看布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.customwidget.MainActivity" >

 <RelativeLayout
  android:id="@+id/menu_level1"
  android:layout_width="100dp"
  android:layout_height="50dp"
  android:layout_alignParentBottom="true"
  android:layout_centerHorizontal="true"
  android:background="@drawable/level1" >

  <ImageButton
   android:id="@+id/level1_home"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerInParent="true"
   android:layout_marginBottom="10dp"
   android:background="@drawable/icon_home"
   android:onClick="myClick" />
 </RelativeLayout>

 <RelativeLayout
  android:id="@+id/menu_level2"
  android:layout_width="200dp"
  android:layout_height="100dp"
  android:layout_alignParentBottom="true"
  android:layout_centerHorizontal="true"
  android:background="@drawable/level2" >

  <ImageButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_marginBottom="10dp"
   android:layout_marginLeft="15dp"
   android:background="@drawable/icon_search" />

  <ImageButton
   android:id="@+id/level2_menu"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerHorizontal="true"
   android:layout_marginTop="7dp"
   android:background="@drawable/icon_menu"
   android:onClick="myClick" />

  <ImageButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_alignParentRight="true"
   android:layout_marginBottom="10dp"
   android:layout_marginRight="15dp"
   android:background="@drawable/icon_myyouku" />
 </RelativeLayout>

 <RelativeLayout
  android:id="@+id/menu_level3"
  android:layout_width="320dp"
  android:layout_height="162dp"
  android:layout_alignParentBottom="true"
  android:layout_centerHorizontal="true"
  android:background="@drawable/level3" >

  <ImageButton
   android:id="@+id/level3_channel1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_marginBottom="10dp"
   android:layout_marginLeft="12dp"
   android:background="@drawable/channel1" />

  <ImageButton
   android:id="@+id/level3_channel2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_above="@id/level3_channel1"
   android:layout_marginBottom="17dp"
   android:layout_marginLeft="-5dp"
   android:layout_toRightOf="@id/level3_channel1"
   android:background="@drawable/channel2" />

  <ImageButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_above="@id/level3_channel2"
   android:layout_marginBottom="15dp"
   android:layout_marginLeft="13dp"
   android:layout_toRightOf="@id/level3_channel2"
   android:background="@drawable/channel3" />

  <ImageButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerHorizontal="true"
   android:layout_marginTop="10dp"
   android:background="@drawable/channel4" />

  <ImageButton
   android:id="@+id/level3_channel7"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_alignParentRight="true"
   android:layout_marginBottom="10dp"
   android:layout_marginRight="12dp"
   android:background="@drawable/channel7" />

  <ImageButton
   android:id="@+id/level3_channel6"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_above="@id/level3_channel7"
   android:layout_marginBottom="17dp"
   android:layout_marginRight="-5dp"
   android:layout_toLeftOf="@id/level3_channel7"
   android:background="@drawable/channel6" />

  <ImageButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_above="@id/level3_channel6"
   android:layout_marginBottom="15dp"
   android:layout_marginRight="13dp"
   android:layout_toLeftOf="@id/level3_channel6"
   android:background="@drawable/channel5" />
 </RelativeLayout>

</RelativeLayout>

這里是一個(gè)相對(duì)布局中嵌套了三個(gè)相對(duì)布局,嵌套的第一個(gè)相對(duì)布局負(fù)責(zé)顯示一級(jí)菜單,嵌套的第二個(gè)相對(duì)布局負(fù)責(zé)顯示二級(jí)菜單,嵌套的第三個(gè)相對(duì)布局負(fù)責(zé)顯示三級(jí)菜單。三個(gè)不同層次的菜單的背景都是弧形。我們通過(guò)指定具體的寬高來(lái)使三個(gè)層次的菜單具有不同的大小。

效果如下:

這里寫圖片描述

再看看MainActivity.java

/**
 * 模仿優(yōu)酷菜單
 * 2015年5月19日
 */
public class MainActivity extends Activity {

 //分別拿到不同等級(jí)的菜單
 private RelativeLayout lv1;
 private RelativeLayout lv2;
 private RelativeLayout lv3;
 private Animation animation;
 //各級(jí)菜單是否顯示,默認(rèn)全都顯示
 private boolean isDisplaylv1 = true;
 private boolean isDisplaylv2 = true;
 private boolean isDisplaylv3 = true;
 //動(dòng)畫是否正在執(zhí)行,默認(rèn)動(dòng)畫沒(méi)有執(zhí)行
 private boolean isAnimationRunning = false;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  lv1 = (RelativeLayout) this.findViewById(R.id.menu_level1);
  lv2 = (RelativeLayout) this.findViewById(R.id.menu_level2);
  lv3 = (RelativeLayout) this.findViewById(R.id.menu_level3);
 }

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
  //如果動(dòng)畫正在執(zhí)行,則不處理此事件
  if (isAnimationRunning)
   return super.onKeyDown(keyCode, event);
  //如果點(diǎn)擊的是菜單鍵
  if (keyCode == KeyEvent.KEYCODE_MENU) {
   //如果一級(jí)菜單已經(jīng)顯示,判斷二級(jí)菜單是否顯示
   if (isDisplaylv1) {
    //設(shè)置動(dòng)畫啟動(dòng)延遲時(shí)間
    int startOffset = 0;
    //如果二級(jí)菜單已經(jīng)顯示,判斷三級(jí)菜單是否顯示,然后退出二級(jí)菜單
    if (isDisplaylv2) {
     if (isDisplaylv3) {
      //如果三級(jí)菜單已經(jīng)顯示,執(zhí)行退出動(dòng)畫
      exitAnimation(lv3, startOffset);
      //三級(jí)菜單退出動(dòng)畫執(zhí)行完畢之后,動(dòng)畫的啟動(dòng)時(shí)間延遲500ms
      startOffset += 500;
      isDisplaylv3 = !isDisplaylv3;
     }
     //二級(jí)菜單退出,此時(shí)startOffset=500,即動(dòng)畫啟動(dòng)時(shí)間延遲500ms
     exitAnimation(lv2, startOffset);
     //二級(jí)菜單退出動(dòng)畫執(zhí)行完畢之后,動(dòng)畫的啟動(dòng)時(shí)間延遲500ms
     startOffset += 500;
     isDisplaylv2 = !isDisplaylv2;
    }
    //一級(jí)菜單退出,此時(shí)startOffset=1000,即動(dòng)畫啟動(dòng)時(shí)間延遲1000ms
    exitAnimation(lv1, startOffset);
   //如果一級(jí)菜單未顯示,則一級(jí)菜單進(jìn)入
   } else {
    enterAnimation(lv1);
   }
   isDisplaylv1 = !isDisplaylv1;
   return true;
  }
  return super.onKeyDown(keyCode, event);
 }

 public void myClick(View v) {
  //如果動(dòng)畫正在執(zhí)行,則不處理此事件
  if (isAnimationRunning)
   return;
  switch (v.getId()) {
  /**
   * 當(dāng)點(diǎn)擊二級(jí)菜單的menu時(shí),如果三級(jí)菜單已經(jīng)顯示,則執(zhí)行退出動(dòng)畫,
   * 否則執(zhí)行進(jìn)入動(dòng)畫
   */
  case R.id.level2_menu:
   if (isDisplaylv3) {
    exitAnimation(lv3, 0);
   } else {
    enterAnimation(lv3);
   }
   isDisplaylv3 = !isDisplaylv3;
   break;
  case R.id.level1_home:
   // 如果二級(jí)菜單已經(jīng)顯示,再判斷三級(jí)菜單是否顯示
   if (isDisplaylv2) {
    //通過(guò)設(shè)置動(dòng)畫啟動(dòng)延遲時(shí)間,來(lái)實(shí)現(xiàn)動(dòng)畫依次退出效果
    int startOffset = 0;
    // 如果三級(jí)菜單也顯示了,則讓他們依次退出
    if (isDisplaylv3) {
     exitAnimation(lv3, startOffset);
     startOffset = 700;
     isDisplaylv3 = !isDisplaylv3;
    }
    exitAnimation(lv2, startOffset);
    isDisplaylv2 = !isDisplaylv2;
    // 如果二級(jí)菜單沒(méi)有顯示,就讓二級(jí)菜單顯示出來(lái)
   } else {
    enterAnimation(lv2);
    isDisplaylv2 = !isDisplaylv2;
   }
   break;
  }
 }

 /**
  * 退出動(dòng)畫
  * @param layout 執(zhí)行動(dòng)畫的布局文件
  * @param startOffset 動(dòng)畫啟動(dòng)的延遲時(shí)間
  */
 public void exitAnimation(RelativeLayout layout, long startOffset) {
  animation = AnimationUtils.loadAnimation(this, R.anim.exit_menu);
  animation.setFillAfter(true);
  animation.setStartOffset(startOffset);
  animation.setAnimationListener(new MyAnimationListener());
  layout.startAnimation(animation);
 }

 /**
  * 進(jìn)入動(dòng)畫
  * @param layout 執(zhí)行動(dòng)畫的布局文件
  */
 public void enterAnimation(RelativeLayout layout) {
  animation = AnimationUtils.loadAnimation(this, R.anim.enter_menu);
  animation.setFillAfter(true);
  animation.setAnimationListener(new MyAnimationListener());
  layout.startAnimation(animation);
 }

 /**
  * 判斷動(dòng)畫是否正在執(zhí)行
  * @author 王松
  *
  */
 private class MyAnimationListener implements AnimationListener {

  //動(dòng)畫開始執(zhí)行
  @Override
  public void onAnimationStart(Animation animation) {
   isAnimationRunning = true;
  }

  //動(dòng)畫執(zhí)行結(jié)束
  @Override
  public void onAnimationEnd(Animation animation) {
   isAnimationRunning = false;
  }

  @Override
  public void onAnimationRepeat(Animation animation) {
  }

 }
}

代碼中注釋已經(jīng)寫的很詳細(xì)了,這里不再贅述。最后在給大家看看兩個(gè)動(dòng)畫文件:

enter_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:shareInterpolator="true">
 <rotate
  android:duration="1000"
  android:fromDegrees="-180"
  android:toDegrees="0"
  android:pivotX="50%"
  android:pivotY="100%" />
</set>

exit_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:shareInterpolator="true">
 <rotate
  android:duration="1000"
  android:fromDegrees="0"
  android:toDegrees="-180"
  android:pivotX="50%"
  android:pivotY="100%" />
</set>

關(guān)于動(dòng)畫如果不太懂可以看這里:Android基礎(chǔ)知識(shí)之tween動(dòng)畫效果    Android基礎(chǔ)知識(shí)之frame動(dòng)畫效果

源碼下載:http://xiazai.jb51.net/201606/yuanma/Androidyouku(jb51.net).rar

原文鏈接:http://blog.csdn.net/u012702547/article/details/45842963

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android多線程之同步鎖的使用

    Android多線程之同步鎖的使用

    本篇文章主要介紹了Android多線程之同步鎖的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • android 實(shí)現(xiàn)控件左右或上下抖動(dòng)教程

    android 實(shí)現(xiàn)控件左右或上下抖動(dòng)教程

    這篇文章主要介紹了android 實(shí)現(xiàn)控件左右或上下抖動(dòng)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • Android 路徑查詢具體實(shí)現(xiàn)

    Android 路徑查詢具體實(shí)現(xiàn)

    可以通過(guò)RasterMap的getDirection()方法來(lái)查詢路徑,和查詢地址類似,路徑查詢的結(jié)果也是通過(guò)回調(diào)函數(shù)的方式來(lái)通知應(yīng)用程序的,下面的例子返回南京到北京的路徑
    2013-10-10
  • AndroidStudio4.0 New Class的坑(小結(jié))

    AndroidStudio4.0 New Class的坑(小結(jié))

    這篇文章主要介紹了AndroidStudio4.0 New Class的坑,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • android自定義環(huán)形對(duì)比圖效果

    android自定義環(huán)形對(duì)比圖效果

    這篇文章主要為大家詳細(xì)介紹了android自定義環(huán)形對(duì)比圖,外環(huán)有類似進(jìn)度條的旋轉(zhuǎn)動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Android 手機(jī)防止休眠的兩種實(shí)現(xiàn)方法

    Android 手機(jī)防止休眠的兩種實(shí)現(xiàn)方法

    這篇文章主要介紹了Android 手機(jī)防止休眠方法的相關(guān)資料,一種是在Manifest.xml文件里面聲明,另外一種方法是在代碼里面修改LayoutParams的標(biāo)志位,需要的朋友可以參考下
    2017-08-08
  • Android沉浸式狀態(tài)欄的實(shí)現(xiàn)代碼

    Android沉浸式狀態(tài)欄的實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了Android沉浸式狀態(tài)欄的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Android實(shí)現(xiàn)圖片隨手指旋轉(zhuǎn)功能

    Android實(shí)現(xiàn)圖片隨手指旋轉(zhuǎn)功能

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片隨手指旋轉(zhuǎn)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android使用Canvas?2D實(shí)現(xiàn)循環(huán)菜單效果

    Android使用Canvas?2D實(shí)現(xiàn)循環(huán)菜單效果

    循環(huán)菜單有很多種自定義方式,我們可以利用ViewPager或者RecyclerView?+?CarouselLayoutManager?或者RecyclerView?+?PageSnapHelper來(lái)實(shí)現(xiàn)這種效果,今天我們使用Canvas?2D來(lái)實(shí)現(xiàn)這種效果,感興趣的朋友可以參考下
    2024-01-01
  • 實(shí)例詳解Android快速開發(fā)工具類總結(jié)

    實(shí)例詳解Android快速開發(fā)工具類總結(jié)

    這篇文章主要介紹了實(shí)例詳解Android快速開發(fā)工具類總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2016-01-01

最新評(píng)論

莱州市| 长春市| 惠州市| 鹿泉市| 资阳市| 巴楚县| 桦南县| 景德镇市| 云霄县| 桃江县| 米林县| 鲜城| 铜山县| 新郑市| 安陆市| 曲靖市| 永胜县| 台中县| 珲春市| 嘉荫县| 邳州市| 保德县| 满洲里市| 彭州市| 五华县| 新干县| 三门县| 河北省| 台湾省| 离岛区| 普定县| 长顺县| 哈尔滨市| 凤阳县| 崇左市| 濮阳县| 平顺县| 淳化县| 定安县| 新源县| 宜州市|