Android (6) slice menu implementation, android slice
I. Slice menu implementation:
After learning from the source code of the experts, let's take a look at how the slice menu is implemented:
:
Main Interface layout:
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: background = "@ drawable/background"> <! -- Android: clipChildren: whether to restrict the sub-View to its range. android: clipToPadding indicates whether the control's draw area is in the padding. --> <RelativeLayout android: id = "@ + id/buttons_wrapper_layout" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: Rule = "true" android: layout_alignParentRight = "true" android: clipChildren = "false" android: clipToPadding = "false"> <ImageButton android: id = "@ + id/button_photo" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: Layout = "true" android: layout_alignParentRight = "true" android: layout_marginBottom = "142dp" android: layout_marginRight = "10dp" android: background = "@ drawable/path2_composer_camera" android: visibility = "gone"/> <ImageButton android: id = "@ + id/button_people" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: Layout = "true" android: layout_alignParentRight = "true" android: layout_marginBottom = "135dp" android: layout_marginRight = "52dp" android: background = "@ drawable/path2_composer_with" android: visibility = "gone"/> <ImageButton android: id = "@ + id/button_place" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: Layout = "true" android: layout_alignParentRight = "true" android: layout_marginBottom = "116dp" android: layout_marginRight = "89dp" android: background = "@ drawable/path2_composer_place" android: visibility = "gone"/> <ImageButton android: id = "@ + id/button_music" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: Layout = "true" android: layout_alignParentRight = "true" android: layout_marginBottom = "87dp" android: layout_marginRight = "118dp" android: background = "@ drawable/path2_composer_music" android: visibility = "gone"/> <ImageButton android: id = "@ + id/button_thought" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: Layout = "true" android: layout_alignParentRight = "true" android: layout_marginBottom = "50dp" android: layout_marginRight = "137dp" android: background = "@ drawable/path2_composer_thought" android: visibility = "gone"/> <ImageButton android: id = "@ + id/button_sleep" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: Layout = "true" android: layout_alignParentRight = "true" android: layout_marginBottom = "8dp" android: layout_marginRight = "144dp" android: background = "@ drawable/path2_composer_sleep" android: visibility = "gone"/> </RelativeLayout> <RelativeLayout android: id = "@ + id/buttons_show_hide_button_layout" android: layout_width = "60dp" android: layout_height = "57dp" android: layout_alignParentBottom = "true" android: layout_alignParentRight = "true" android: background = "@ drawable/path2_composer_button"> <ImageView android: id = "@ + id/buttons_show_hide_button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerInParent = "true" android: src = "@ drawable/path2_composer_icn_plus"/> </RelativeLayout>
Activity:
public class MainActivity extends Activity {private boolean isShowing;private RelativeLayout buttons_wrapper_layout;private ImageView buttons_show_hide_button;private RelativeLayout buttons_show_hide_button_layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sector_menu); MyAnimations.initOffset(MainActivity.this);buttons_wrapper_layout = (RelativeLayout) findViewById(R.id.buttons_wrapper_layout);buttons_show_hide_button_layout = (RelativeLayout) findViewById(R.id.buttons_show_hide_button_layout);buttons_show_hide_button = (ImageView) findViewById(R.id.buttons_show_hide_button);buttons_show_hide_button_layout.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (!isShowing) {MyAnimations.startAnimationsIn(buttons_wrapper_layout, 300);buttons_show_hide_button.startAnimation(MyAnimations.getRotateAnimation(0,-270, 300));} else {MyAnimations.startAnimationsOut(buttons_wrapper_layout, 300);buttons_show_hide_button.startAnimation(MyAnimations.getRotateAnimation(-270, 0, 300));}isShowing = !isShowing;}});for (int i = 0; i < buttons_wrapper_layout.getChildCount(); i++) {buttons_wrapper_layout.getChildAt(i).setOnClickListener(new OnClickImageButton());} } class OnClickImageButton implements View.OnClickListener{@Overridepublic void onClick(View arg0) {switch(arg0.getId()){case R.id.button_photo:Toast.makeText(MainActivity.this, "photo", Toast.LENGTH_SHORT).show();break;case R.id.button_people:Toast.makeText(MainActivity.this, "people", Toast.LENGTH_SHORT).show();break;case R.id.button_place:Toast.makeText(MainActivity.this, "place", Toast.LENGTH_SHORT).show();break;case R.id.button_music:Toast.makeText(MainActivity.this, "music", Toast.LENGTH_SHORT).show();break;case R.id.button_thought:Toast.makeText(MainActivity.this, "thought", Toast.LENGTH_SHORT).show();break;case R.id.button_sleep:Toast.makeText(MainActivity.this, "sleep", Toast.LENGTH_SHORT).show();break;}} }}
Animation implementation:
Public class MyAnimations {private static int xOffset = 15; private static int yOffset =-13; public static void initOffset (Context context) {// obtain the screen density context. getResources (). getDisplayMetrics (). density sets the moving distance xOffset = (int) (10 * context. getResources (). getDisplayMetrics (). density); yOffset =-(int) (8 * context. getResources (). getDisplayMetrics (). density);} public static Animation getRotateAnimation (float fromDegrees, float toDegrees, int durationMillis) {// rotate the first two parameters to set the rotation angle, the last four sets the rotation center RotateAnimation rotate = new RotateAnimation (fromDegrees, toDegrees, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f); // rotate for the duration. setDuration (durationMillis); // After the animation ends, it stays at the last second rotate. setFillAfter (true); return rotate;} public static void startAnimationsIn (ViewGroup viewgroup, int durationMillis) {for (int I = 0; I <viewgroup. getChildCount (); I ++) {ImageButton inoutimagebutton = (ImageButton) viewgroup. getChildAt (I); // display the image inoutimagebutton. setVisibility (View. VISIBLE); MarginLayoutParams mlp = (MarginLayoutParams) inoutimagebutton. getLayoutParams (); // displacement distance from Animation animation = new TranslateAnimation (mlp. rightMargin-xOffset, 0F, yOffset + mlp. bottomMargin, 0F); // The animation stops at the last animation frame. setFillAfter (true); // animation duration animation. setDuration (durationMillis); // the start time of animation. setStartOffset (I * 100)/(-1 + viewgroup. getChildCount (); animation. setInterpolator (new OvershootInterpolator (2F); // Add the animation inoutimagebutton. startAnimation (animation);} public static void startAnimationsOut (ViewGroup viewgroup, int durationMillis) {for (int I = 0; I <viewgroup. getChildCount (); I ++) {final ImageButton inoutimagebutton = (ImageButton) viewgroup. getChildAt (I); MarginLayoutParams mlp = (MarginLayoutParams) inoutimagebutton. getLayoutParams (); Animation animation = new TranslateAnimation (0F, mlp. rightMargin-xOffset, 0F, yOffset + mlp. bottomMargin); animation. setFillAfter (true); animation. setDuration (durationMillis); animation. setStartOffset (viewgroup. getChildCount ()-I) * 100)/(-1 + viewgroup. getChildCount (); animation. setInterpolator (new AnticipateInterpolator (2F); // sets the animation listening to animation. setAnimationListener (new Animation. animationListener () {@ Overridepublic void Merge (Animation arg0) {}@ Overridepublic void Merge (Animation arg0) {}// hide imageButton @ Overridepublic void Merge (Animation arg0) after the Animation ends) {inoutimagebutton. setVisibility (View. GONE) ;}}); inoutimagebutton. startAnimation (animation );}}}
This is all the implementations.