Skip to content

Commit

Permalink
Merge pull request #15 from btmccord/elevation-fix
Browse files Browse the repository at this point in the history
Elevation Support
  • Loading branch information
iammert authored Jan 8, 2018
2 parents 02be8e3 + e1af9db commit 6cfd01f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_fab.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:background="#ffffff"
android:orientation="vertical"
android:id="@+id/rootLayout">

Expand All @@ -14,6 +14,7 @@
android:layout_margin="16dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:elevation="4dp"
app:radiusFactor="1">

<RelativeLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;
Expand Down Expand Up @@ -68,6 +70,12 @@ public class ScalingLayout extends FrameLayout {
*/
private ScalingLayoutListener scalingLayoutListener;

/**
* CustomOutline for elevation shadows
*/
private ScalingLayoutOutlineProvider viewOutline;


public ScalingLayout(@NonNull Context context) {
super(context);
init(context, null);
Expand Down Expand Up @@ -137,9 +145,11 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
settings.initialize(w, h);
currentWidth = w;
currentRadius = settings.getMaxRadius();
viewOutline = new ScalingLayoutOutlineProvider(w, h, currentRadius);
}

rectF.set(0, 0, w, h);
updateViewOutline(h, currentWidth, currentRadius);
invalidate();
}

Expand Down Expand Up @@ -206,6 +216,26 @@ public void setListener(ScalingLayoutListener scalingLayoutListener) {
this.scalingLayoutListener = scalingLayoutListener;
}

/**
* Updates view outline borders and radius
*
* @param height
* @param width
* @param radius
*/
private void updateViewOutline(int height, int width, float radius) {
viewOutline.setHeight(height);
viewOutline.setWidth(width);
viewOutline.setRadius(radius);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && ViewCompat.getElevation(this) > 0f) {
try {
setOutlineProvider(viewOutline);
} catch (Exception e) {
e.printStackTrace();
}
}
}

/**
* Set radius will update layout radius
* Also layouts margins and width depend on
Expand Down Expand Up @@ -290,7 +320,7 @@ private void updateState(float currentRadius) {
/**
* Notify observers about change
*/
private void notifyListener(){
private void notifyListener() {
if (scalingLayoutListener != null) {
if (state == State.COLLAPSED) {
scalingLayoutListener.onCollapsed();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package iammert.com.view.scalinglib;

import android.graphics.Outline;
import android.view.View;
import android.view.ViewOutlineProvider;

/**
* Created by mertsimsek on 08/01/2018.
*/
public class ScalingLayoutOutlineProvider extends ViewOutlineProvider {

private int width;
private int height;
private float radius;

ScalingLayoutOutlineProvider(int width, int height, float radius) {
this.width = width;
this.height = height;
this.radius = radius;
}

@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(0, 0, width, height, radius);
}

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

public float getRadius() {
return radius;
}

public void setRadius(float radius) {
this.radius = radius;
}
}

0 comments on commit 6cfd01f

Please sign in to comment.