Skip to content

Commit

Permalink
Fix some issues when initing the FabOptions (close animation on start…
Browse files Browse the repository at this point in the history
…up), Fix JoaquimLey#17
  • Loading branch information
JoaquimLey committed Jan 19, 2017
1 parent 6901f4b commit b757cd9
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 72 deletions.
38 changes: 18 additions & 20 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#FabOptions
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-FabOptions-brightgreen.svg?style=flat)](http://android-arsenal.com/details/1/4734)
[![MaterialUp](https://img.shields.io/badge/MaterialUp-FabOptions-blue.svg?style=flat)](https://material.uplabs.com/posts/faboptions)
[![Bintray](https://img.shields.io/badge/Bintray-v1.0.1-brightgreen.svg?style=flat)](https://bintray.com/joaquimleyapps/opensource/com.github.joaquimley%3Afaboptions/1.0.2)
[![Bintray](https://img.shields.io/badge/Bintray-v1.0.2-brightgreen.svg?style=flat)](https://bintray.com/joaquimleyapps/opensource/com.github.joaquimley%3Afaboptions/1.0.2)
![minSdkVersion](https://img.shields.io/badge/minSdkVersion-21-red.svg?style=true)
![compileSdkVersion](https://img.shields.io/badge/compileSdkVersion-25-green.svg?style=true)

Expand All @@ -29,7 +29,17 @@ Android implementation
dependencies {
compile 'com.github.joaquimley:faboptions:1.0.2'
}

- Add the component to your layout:

```xml
<com.joaquimley.faboptions.FabOptions
android:id="@+id/fab_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
```


- Define a `menu.xml` file with your buttons information **e.g.**

Expand All @@ -56,31 +66,19 @@ Android implementation
android:icon="@drawable/ic_share"
android:title="Share" />
</menu>
```


- Add the component to your layout:

```xml
<com.joaquimley.faboptions.FabOptions
android:id="@+id/fab_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
```
```

Via **XML**:
**XML**:

- Bind the buttons menu by adding the **custom attribute** `app:button_menu="@menu/your_fab_buttons"` to your component XML
- Bind the buttons menu by adding the **custom attribute** `app:button_menu="@menu/your_fab_buttons"` to your component XML.

**Programmatically**

- Bind the buttons menu on your FabOptions instance with FabOptions#setMenu()
- Bind the buttons menu on your FabOptions instance with FabOptions#setMenu.

```java
FabOptions fabOptions = (FabOptions) findViewById(R.id.fab_options);
fabOptions.setButtonsMenu(this, R.menu.your_fab_buttons);
fabOptions.setButtonsMenu(R.menu.your_fab_buttons);
```
Expand Down Expand Up @@ -130,4 +128,4 @@ Follow the "fork-and-pull" Git workflow.
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
2 changes: 1 addition & 1 deletion faboptions/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "com.android.support:design:$DESIGN_LIBRARY_VERSION"
}
apply from: 'https://raw.githubusercontent.com/JoaquimLey/jcenter-config/master/deploy.gradle'
apply from: 'https://raw.githubusercontent.com/JoaquimLey/jcenter-config/master/deploy.gradle'
122 changes: 84 additions & 38 deletions faboptions/src/main/java/com/joaquimley/faboptions/FabOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.AnimatedVectorDrawable;
import android.graphics.drawable.VectorDrawable;
import android.os.Build;
import android.support.annotation.MenuRes;
import android.support.annotation.RequiresApi;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.graphics.drawable.AnimatedVectorDrawableCompat;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v7.view.SupportMenuInflater;
import android.support.v7.view.menu.MenuBuilder;
import android.support.v7.widget.AppCompatImageView;
Expand Down Expand Up @@ -75,10 +80,13 @@ public FabOptions(Context context, AttributeSet attrs, int defStyleAttr) {
private void initViews(Context context) {
inflate(context, R.layout.faboptions_layout, this);
mIsOpen = false;

mBackground = findViewById(R.id.background);
mButtonContainer = (FabOptionsButtonContainer) findViewById(R.id.button_container);

mFab = (FloatingActionButton) findViewById(R.id.faboptions_fab);
mFab.setOnClickListener(this);
mButtonContainer = (FabOptionsButtonContainer) findViewById(R.id.button_container);
setInitialFabIcon();
}

private void inflateButtonsFromAttrs(Context context, AttributeSet attrs) {
Expand All @@ -88,14 +96,22 @@ private void inflateButtonsFromAttrs(Context context, AttributeSet attrs) {
}
}

public void setButtonsMenu(@MenuRes int menuId) {
Context context = getContext();
setButtonsMenu(context, menuId);
}

/**
* Deprecated. Use {@link #setButtonsMenu(int)} instead.
*/
@Deprecated
public void setButtonsMenu(Context context, @MenuRes int menuId) {
mMenu = new MenuBuilder(context);
SupportMenuInflater menuInf = new SupportMenuInflater(context);
menuInf.inflate(menuId, mMenu);

addButtonsFromMenu(context, mMenu);
mSeparator = mButtonContainer.addSeparator(context);
close();
animateButtons(false);
}

private void addButtonsFromMenu(Context context, Menu menu) {
Expand Down Expand Up @@ -132,44 +148,69 @@ public void setOnClickListener(View.OnClickListener listener) {
mListener = listener;
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (mSeparator != null) {
ViewGroup.LayoutParams separatorLayoutParams = mSeparator.getLayoutParams();
separatorLayoutParams.width = mFab.getMeasuredWidth();
separatorLayoutParams.height = mFab.getMeasuredHeight();
mSeparator.setLayoutParams(separatorLayoutParams);
private void open() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AnimatedVectorDrawable drawable = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.faboptions_ic_menu_animatable, null);
mFab.setImageDrawable(drawable);
drawable.start();
} else {
final AnimatedVectorDrawableCompat drawableCompat = AnimatedVectorDrawableCompat.create(getContext(), R.drawable.faboptions_ic_menu_animatable);
if (drawableCompat != null) {
mFab.setImageDrawable(drawableCompat);
drawableCompat.start();
}
}

if (mIsOpen) {
ViewGroup.LayoutParams backgroundLayoutParams = mBackground.getLayoutParams();
backgroundLayoutParams.width = mButtonContainer.getMeasuredWidth();
backgroundLayoutParams.height = mButtonContainer.getMeasuredHeight();
mBackground.setLayoutParams(backgroundLayoutParams);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
TransitionManager.beginDelayedTransition(this, new OpenMorphTransition(mButtonContainer));
}
}

private void open() {
AnimatedVectorDrawable drawable = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.faboptions_ic_menu_animatable, null);
mFab.setImageDrawable(drawable);
drawable.start();
TransitionManager.beginDelayedTransition(this, new OpenMorphTransition(mButtonContainer));
animateButtons(true);
animateBackground(true);
mIsOpen = true;
}

private void close() {
AnimatedVectorDrawable drawable = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.faboptions_ic_close_animatable, null);
mFab.setImageDrawable(drawable);
drawable.start();
TransitionManager.beginDelayedTransition(this, new CloseMorphTransition(mButtonContainer));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AnimatedVectorDrawable drawable = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.faboptions_ic_close_animatable, null);
mFab.setImageDrawable(drawable);
drawable.start();
} else {
final AnimatedVectorDrawableCompat drawableCompat =
AnimatedVectorDrawableCompat.create(getContext(), R.drawable.faboptions_ic_close_animatable);
if (drawableCompat != null) {
mFab.setImageDrawable(drawableCompat);
drawableCompat.start();
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
TransitionManager.beginDelayedTransition(this, new CloseMorphTransition(mButtonContainer));
}
animateButtons(false);
animateBackground(false);
mIsOpen = false;
}

private void setInitialFabIcon() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
VectorDrawable drawable = (VectorDrawable) getResources().getDrawable(R.drawable.faboptions_ic_overflow, null);
mFab.setImageDrawable(drawable);
} else {
VectorDrawableCompat drawable = (VectorDrawableCompat) getResources().getDrawable(R.drawable.faboptions_ic_overflow, null);
mFab.setImageDrawable(drawable);
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mSeparator != null) {
ViewGroup.LayoutParams separatorLayoutParams = mSeparator.getLayoutParams();
separatorLayoutParams.width = mFab.getMeasuredWidth();
separatorLayoutParams.height = mFab.getMeasuredHeight();
mSeparator.setLayoutParams(separatorLayoutParams);
}
}

private void animateBackground(final boolean isOpen) {
ViewGroup.LayoutParams backgroundLayoutParams = mBackground.getLayoutParams();
backgroundLayoutParams.width = isOpen ? mButtonContainer.getMeasuredWidth() : NO_DIMENSION;
Expand All @@ -187,33 +228,38 @@ public boolean isOpen() {
return mIsOpen;
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private static class OpenMorphTransition extends TransitionSet {
OpenMorphTransition(ViewGroup viewGroup) {

ChangeBounds changeBound = new ChangeBounds();
changeBound.excludeChildren(R.id.button_container, true);

ChangeTransform changeTransform = new ChangeTransform();
for (int i = 0; i < viewGroup.getChildCount(); i++) {
changeTransform.addTarget(viewGroup.getChildAt(i));
}
addTransition(changeBound);
addTransition(changeTransform);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
ChangeTransform changeTransform = new ChangeTransform();
for (int i = 0; i < viewGroup.getChildCount(); i++) {
changeTransform.addTarget(viewGroup.getChildAt(i));
}
addTransition(changeTransform);
}
setOrdering(TransitionSet.ORDERING_SEQUENTIAL);
}
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private static class CloseMorphTransition extends TransitionSet {
CloseMorphTransition(ViewGroup viewGroup) {
ChangeBounds changeBound = new ChangeBounds();
changeBound.excludeChildren(R.id.button_container, true);

ChangeTransform changeTransform = new ChangeTransform();
for (int i = 0; i < viewGroup.getChildCount(); i++) {
changeTransform.addTarget(viewGroup.getChildAt(i));
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
ChangeTransform changeTransform = new ChangeTransform();
for (int i = 0; i < viewGroup.getChildCount(); i++) {
changeTransform.addTarget(viewGroup.getChildAt(i));
}
changeTransform.setDuration(CLOSE_MORPH_TRANSFORM_DURATION);
addTransition(changeTransform);
}
changeTransform.setDuration(CLOSE_MORPH_TRANSFORM_DURATION);
addTransition(changeTransform);
addTransition(changeBound);
setOrdering(TransitionSet.ORDERING_TOGETHER);
}
Expand Down
3 changes: 1 addition & 2 deletions faboptions/src/main/res/animator/close_to_overflow.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) Joaquim Ley 2016. All Rights Reserved.
~ <p/>
~ Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
3 changes: 1 addition & 2 deletions faboptions/src/main/res/animator/overflow_to_close.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) Joaquim Ley 2016. All Rights Reserved.
~ <p/>
~ Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
~ Copyright (c) Joaquim Ley 2016. All Rights Reserved.
~ Copyright (c) Joaquim Ley 2017. All Rights Reserved.
~ <p/>
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
~ Copyright (c) Joaquim Ley 2016. All Rights Reserved.
~ Copyright (c) Joaquim Ley 2017. All Rights Reserved.
~ <p/>
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
-->

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/faboptions_ic_close">
xmlns:tools="http://schemas.android.com/tools"
android:drawable="@drawable/faboptions_ic_close"
tools:targetApi="lollipop">
<target
android:name="main"
android:animation="@animator/close_to_overflow" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
-->

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/faboptions_ic_overflow">
xmlns:tools="http://schemas.android.com/tools"
android:drawable="@drawable/faboptions_ic_overflow"
tools:targetApi="lollipop">
<target
android:name="main"
android:animation="@animator/overflow_to_close" />
Expand Down
3 changes: 1 addition & 2 deletions faboptions/src/main/res/layout/faboptions_layout.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) Joaquim Ley 2016. All Rights Reserved.
~ <p/>
~ Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 1 addition & 1 deletion faboptions/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<resources>

<string name="faboptions_vector_overflow_path">M 6 12 L 6 12.1 M 12 12.1 L 12 12 M 18 12.1 L 18 12</string>
<string name="faboptions_vector_overflow_path">M 6 12 L 6 12.1, M 12 12.1 L 12 12, M 18 12.1 L 18 12</string>

<string name="faboptions_vector_close_path">M 6 6 L 18 18, M 12 12 L 12 12, M 6 18 L 18 6</string>

Expand Down
3 changes: 2 additions & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ dependencies {
final FABOPTIONS_VERSION = '1.0.2'

compile "com.android.support:design:$DESIGN_LIBRARY_VERSION"
compile "com.github.joaquimley:faboptions:$FABOPTIONS_VERSION"
// compile "com.github.joaquimley:faboptions:$FABOPTIONS_VERSION"
compile project(':faboptions')
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected void onCreate(Bundle savedInstanceState) {
setSupportActionBar(mToolbar);

FabOptions fabOptions = (FabOptions) findViewById(R.id.fab_options);
fabOptions.setButtonsMenu(this, R.menu.menu_faboptions);
fabOptions.setButtonsMenu(R.menu.menu_faboptions);
fabOptions.setOnClickListener(this);
}

Expand Down
1 change: 1 addition & 0 deletions sample/src/main/res/layout/activity_sample_xml.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:autoLink="web"
android:text="@string/website" />

<com.joaquimley.faboptions.FabOptions
Expand Down

0 comments on commit b757cd9

Please sign in to comment.