Skip to content

Commit 87695d4

Browse files
committed
add ViewAnimatorListener
1 parent 9accb9d commit 87695d4

2 files changed

Lines changed: 82 additions & 69 deletions

File tree

app/src/main/java/yalantis/com/sidemenu/MainActivity.java

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.res.Configuration;
44
import android.graphics.Color;
5+
import android.graphics.drawable.BitmapDrawable;
56
import android.os.Bundle;
67
import android.support.v4.widget.DrawerLayout;
78
import android.support.v7.app.ActionBarActivity;
@@ -10,22 +11,30 @@
1011
import android.view.Menu;
1112
import android.view.MenuItem;
1213
import android.view.View;
14+
import android.view.animation.AccelerateInterpolator;
1315
import android.widget.LinearLayout;
1416

1517
import java.util.ArrayList;
1618
import java.util.List;
1719

20+
import io.codetail.animation.SupportAnimator;
21+
import io.codetail.animation.ViewAnimationUtils;
1822
import yalantis.com.sidemenu.fragment.ContentFragment;
23+
import yalantis.com.sidemenu.interfaces.Resourceble;
24+
import yalantis.com.sidemenu.interfaces.ScreenShotable;
1925
import yalantis.com.sidemenu.model.SlideMenuItem;
2026
import yalantis.com.sidemenu.util.ViewAnimator;
2127

2228

23-
public class MainActivity extends ActionBarActivity {
29+
public class MainActivity extends ActionBarActivity implements ViewAnimator.ViewAnimatorListener {
2430
private DrawerLayout drawerLayout;
2531
private ActionBarDrawerToggle drawerToggle;
2632
private List<SlideMenuItem> list = new ArrayList<>();
2733
private ContentFragment contentFragment;
2834
private ViewAnimator viewAnimator;
35+
private int res = R.drawable.content_music;
36+
private LinearLayout linearLayout;
37+
2938

3039
@Override
3140
protected void onCreate(Bundle savedInstanceState) {
@@ -39,10 +48,18 @@ protected void onCreate(Bundle savedInstanceState) {
3948
}
4049
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
4150
drawerLayout.setScrimColor(Color.TRANSPARENT);
51+
linearLayout = (LinearLayout) findViewById(R.id.left_drawer);
52+
linearLayout.setOnClickListener(new View.OnClickListener() {
53+
@Override
54+
public void onClick(View v) {
55+
drawerLayout.closeDrawers();
56+
}
57+
});
58+
4259

4360
setActionBar();
4461
createMenuList();
45-
viewAnimator = new ViewAnimator<>(this, list, (LinearLayout) findViewById(R.id.left_drawer), contentFragment, drawerLayout);
62+
viewAnimator = new ViewAnimator<>(this, list, contentFragment, drawerLayout, this);
4663
}
4764

4865
private void createMenuList() {
@@ -81,14 +98,14 @@ private void setActionBar() {
8198
/** Called when a drawer has settled in a completely closed state. */
8299
public void onDrawerClosed(View view) {
83100
super.onDrawerClosed(view);
84-
viewAnimator.getLinearLayout().removeAllViews();
85-
viewAnimator.getLinearLayout().invalidate();
101+
linearLayout.removeAllViews();
102+
linearLayout.invalidate();
86103
}
87104

88105
@Override
89106
public void onDrawerSlide(View drawerView, float slideOffset) {
90107
super.onDrawerSlide(drawerView, slideOffset);
91-
if (slideOffset > 0.6 && viewAnimator.getLinearLayout().getChildCount() == 0)
108+
if (slideOffset > 0.6 && linearLayout.getChildCount() == 0)
92109
viewAnimator.showMenuContent();
93110
}
94111

@@ -132,4 +149,46 @@ public boolean onOptionsItemSelected(MenuItem item) {
132149
}
133150
}
134151

152+
private ScreenShotable replaceFragment(ScreenShotable screenShotable, int topPosition) {
153+
this.res = this.res == R.drawable.content_music ? R.drawable.content_films : R.drawable.content_music;
154+
View view = findViewById(R.id.content_frame);
155+
int finalRadius = Math.max(view.getWidth(), view.getHeight());
156+
SupportAnimator animator = ViewAnimationUtils.createCircularReveal(view, 0, topPosition, 0, finalRadius);
157+
animator.setInterpolator(new AccelerateInterpolator());
158+
animator.setDuration(ViewAnimator.CIRCULAR_REVEAL_ANIMATION_DURATION);
159+
160+
findViewById(R.id.content_overlay).setBackgroundDrawable(new BitmapDrawable(getResources(), screenShotable.getBitmap()));
161+
animator.start();
162+
ContentFragment contentFragment = ContentFragment.newInstance(this.res);
163+
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, contentFragment).commit();
164+
return contentFragment;
165+
}
166+
167+
@Override
168+
public ScreenShotable onSwitch(Resourceble slideMenuItem, ScreenShotable screenShotable, int position) {
169+
switch (slideMenuItem.getName()) {
170+
case ContentFragment.CLOSE:
171+
return screenShotable;
172+
default:
173+
return replaceFragment(screenShotable, position);
174+
}
175+
}
176+
177+
@Override
178+
public void disableHomeButton() {
179+
getSupportActionBar().setHomeButtonEnabled(false);
180+
181+
}
182+
183+
@Override
184+
public void enableHomeButton() {
185+
getSupportActionBar().setHomeButtonEnabled(true);
186+
drawerLayout.closeDrawers();
187+
188+
}
189+
190+
@Override
191+
public void addViewToContainer(View view) {
192+
linearLayout.addView(view);
193+
}
135194
}

app/src/main/java/yalantis/com/sidemenu/util/ViewAnimator.java

Lines changed: 18 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package yalantis.com.sidemenu.util;
22

3-
import android.graphics.drawable.BitmapDrawable;
43
import android.os.Handler;
54
import android.support.v4.widget.DrawerLayout;
65
import android.support.v7.app.ActionBarActivity;
@@ -13,11 +12,8 @@
1312
import java.util.ArrayList;
1413
import java.util.List;
1514

16-
import io.codetail.animation.SupportAnimator;
17-
import io.codetail.animation.ViewAnimationUtils;
1815
import yalantis.com.sidemenu.R;
1916
import yalantis.com.sidemenu.animation.FlipAnimation;
20-
import yalantis.com.sidemenu.fragment.ContentFragment;
2117
import yalantis.com.sidemenu.interfaces.Resourceble;
2218
import yalantis.com.sidemenu.interfaces.ScreenShotable;
2319

@@ -30,30 +26,22 @@ public class ViewAnimator<T extends Resourceble> {
3026

3127
private ActionBarActivity actionBarActivity;
3228
private List<T> list;
33-
private LinearLayout linearLayout;
29+
3430
private List<View> viewList = new ArrayList<>();
3531
private ScreenShotable screenShotable;
3632
private DrawerLayout drawerLayout;
37-
38-
private int res = R.drawable.content_music;
39-
33+
private ViewAnimatorListener animatorListener;
4034

4135
public ViewAnimator(ActionBarActivity activity,
4236
List<T> items,
43-
LinearLayout linearLayout,
4437
ScreenShotable screenShotable,
45-
final DrawerLayout drawerLayout) {
38+
final DrawerLayout drawerLayout,
39+
ViewAnimatorListener animatorListener) {
4640
this.actionBarActivity = activity;
4741
this.list = items;
48-
this.linearLayout = linearLayout;
49-
this.linearLayout.setOnClickListener(new View.OnClickListener() {
50-
@Override
51-
public void onClick(View v) {
52-
drawerLayout.closeDrawers();
53-
}
54-
});
5542
this.screenShotable = screenShotable;
5643
this.drawerLayout = drawerLayout;
44+
this.animatorListener = animatorListener;
5745
}
5846

5947
public void showMenuContent() {
@@ -75,7 +63,7 @@ public void onClick(View v) {
7563
viewMenu.setVisibility(View.GONE);
7664
viewMenu.setEnabled(false);
7765
viewList.add(viewMenu);
78-
linearLayout.addView(viewMenu);
66+
animatorListener.addViewToContainer(viewMenu);
7967
final double position = i;
8068
final double delay = 3 * ANIMATION_DURATION * (position / size);
8169
new Handler().postDelayed(new Runnable() {
@@ -111,7 +99,7 @@ public void run() {
11199
}
112100

113101
private void setViewsClickable(boolean clickable) {
114-
actionBarActivity.getSupportActionBar().setHomeButtonEnabled(false);
102+
animatorListener.disableHomeButton();
115103
for (View view : viewList) {
116104
view.setEnabled(clickable);
117105
}
@@ -163,7 +151,7 @@ public void onAnimationEnd(Animation animation) {
163151
view.clearAnimation();
164152
view.setVisibility(View.INVISIBLE);
165153
if (position == viewList.size() - 1) {
166-
actionBarActivity.getSupportActionBar().setHomeButtonEnabled(true);
154+
animatorListener.enableHomeButton();
167155
drawerLayout.closeDrawers();
168156
}
169157
}
@@ -178,53 +166,19 @@ public void onAnimationRepeat(Animation animation) {
178166
}
179167

180168
private void switchItem(Resourceble slideMenuItem, int topPosition) {
181-
switch (slideMenuItem.getName()) {
182-
case ContentFragment.CLOSE:
183-
break;
184-
case ContentFragment.BUILDING:
185-
replaceFragment(topPosition);
186-
break;
187-
case ContentFragment.BOOK:
188-
replaceFragment(topPosition);
189-
break;
190-
case ContentFragment.PAINT:
191-
replaceFragment(topPosition);
192-
break;
193-
case ContentFragment.CASE:
194-
replaceFragment(topPosition);
195-
break;
196-
case ContentFragment.SHOP:
197-
replaceFragment(topPosition);
198-
break;
199-
case ContentFragment.PARTY:
200-
replaceFragment(topPosition);
201-
break;
202-
case ContentFragment.MOVIE:
203-
replaceFragment(topPosition);
204-
break;
205-
}
169+
this.screenShotable = animatorListener.onSwitch(slideMenuItem, screenShotable, topPosition);
206170
hideMenuContent();
207171
}
208172

209-
private void replaceFragment(int topPosition) {
210-
this.res = this.res == R.drawable.content_music ? R.drawable.content_films : R.drawable.content_music;
211-
View view = actionBarActivity.findViewById(R.id.content_frame);
212-
int finalRadius = Math.max(view.getWidth(), view.getHeight());
213-
SupportAnimator animator =
214-
ViewAnimationUtils.createCircularReveal(view, 0, topPosition, 0, finalRadius);
215-
animator.setInterpolator(new AccelerateInterpolator());
216-
animator.setDuration(ViewAnimator.CIRCULAR_REVEAL_ANIMATION_DURATION);
217-
218-
actionBarActivity.findViewById(R.id.content_overlay).setBackgroundDrawable(new BitmapDrawable(actionBarActivity.getResources(), screenShotable.getBitmap()));
219-
animator.start();
220-
ContentFragment contentFragment = ContentFragment.newInstance(this.res);
221-
actionBarActivity.getSupportFragmentManager().beginTransaction()
222-
.replace(R.id.content_frame, contentFragment)
223-
.commit();
224-
this.screenShotable = contentFragment;
225-
}
173+
public interface ViewAnimatorListener {
174+
175+
public ScreenShotable onSwitch(Resourceble slideMenuItem, ScreenShotable screenShotable, int position);
176+
177+
public void disableHomeButton();
178+
179+
public void enableHomeButton();
180+
181+
public void addViewToContainer(View view);
226182

227-
public LinearLayout getLinearLayout() {
228-
return linearLayout;
229183
}
230184
}

0 commit comments

Comments
 (0)