Android自訂控制項:老版優酷的三級菜單(效果圖 + Demo),

來源:互聯網
上載者:User

Android自訂控制項:老版優酷的三級菜單( + Demo),


製作思路:

1、先分析這個效果,其實可以理解為把三級菜單分成level1,level2,level3,level1是始終顯示的。點擊level1後,level2會出現;點擊level2後,level3會出現;level2,level3出現後,點擊level1,level2和level3都會消失。然後消失和出現我們用到了一個動畫效果。

2、動畫效果用到的是RotateAnimation,由於我們都是用同一個效果,那麼我們只要寫一個類,把效果實現了就可以了。要是使用RotateAnimation的話,我們會不斷的複用一些代碼,這樣開發的效率會比較低。

3、RotateAnimation的旋轉進入和旋轉出去是個坑——因為他是在X軸順時針方向旋轉的。大家可以看看圖:


整體思路就是這樣,接下來就是一些操作了:

代碼:

1、MyAnimation類:

public class MyAnimation{public static void animationIn(View view){animationIn(view,0);}public static void animationOut(View view){animationOut(view,0);}public static void animationIn(View view,long delay){RotateAnimation animation = new RotateAnimation(180, 360, view.getWidth()/2, view.getHeight());animation.setDuration(500);animation.setFillAfter(true);animation.setStartOffset(delay);view.startAnimation(animation);}public static void animationOut(View view,long delay){RotateAnimation animation = new RotateAnimation(0, 180, view.getWidth()/2, view.getHeight());animation.setDuration(500);animation.setFillAfter(true);animation.setStartOffset(delay);view.startAnimation(animation);}}

2、MainActivity類:

public class MainActivity extends Activity implements OnClickListener{private boolean isLevel2showed,isLevel3showed;private RelativeLayout level1,level2,level3;private ImageButton home,menu;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);isLevel2showed = false;isLevel3showed = false;initLayout();initImageButton();}public void initLayout() {level1 = (RelativeLayout)findViewById(R.id.relate_level1);level2 = (RelativeLayout)findViewById(R.id.relate_level2);level3 = (RelativeLayout)findViewById(R.id.relate_level3);level2.setVisibility(View.INVISIBLE);level3.setVisibility(View.INVISIBLE);}public void initImageButton() {home = (ImageButton)level1.findViewById(R.id.home);menu = (ImageButton)level2.findViewById(R.id.menu);home.setOnClickListener(this);menu.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch(v.getId()){case R.id.home:if(!isLevel2showed){isLevel2showed = true;MyAnimation.animationIn(level2);}else if(!isLevel3showed){isLevel2showed = false;MyAnimation.animationOut(level2);}else{isLevel2showed = false;isLevel3showed = false;MyAnimation.animationOut(level3);MyAnimation.animationOut(level2,500);}break;case R.id.menu:if(!isLevel3showed){isLevel3showed = true;MyAnimation.animationIn(level3);}else{isLevel3showed = false;MyAnimation.animationOut(level3);}break;}}}


源碼下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.