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;}}}
源碼下載