Skip to content
This repository has been archived by the owner on Mar 11, 2022. It is now read-only.

Commit

Permalink
Setters enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
leandroBorgesFerreira committed Jun 12, 2017
1 parent 70b551b commit 82a219a
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 45 deletions.
7 changes: 7 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ dependencies {
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
testCompile 'junit:junit:4.12'
// compile project(':swipe-button')
compile 'com.ebanx:swipe-button:0.4.2'
compile project(':swipe-button')
// compile 'com.ebanx:swipe-button:0.4.2'
}
8 changes: 8 additions & 0 deletions app/src/main/java/com/ebanx/swipebutton/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ebanx.swipebutton;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.widget.Toast;
Expand All @@ -16,6 +17,13 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

SwipeButton swipeButton = (SwipeButton) findViewById(R.id.swipe_btn);

swipeButton.setBackground(ContextCompat.getDrawable(this, R.drawable.shape_button2));
swipeButton.setSlidingButtonBackground(ContextCompat.getDrawable(this, R.drawable.shape_rounded2));
swipeButton.setDisabledDrawable(ContextCompat.getDrawable(this, R.drawable.ic_lock_outline_black_24dp));
swipeButton.setText("lala");
swipeButton.setInnerTextPadding(10, 10, 10, 10);

// swipeButton.setOnStateChangeListener(new OnStateChangeListener() {
// @Override
// public void onStateChange(boolean active) {
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/shape_button2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:state_selected="false">
<shape android:shape="rectangle">
<corners android:radius="50dp" />
<solid android:color="#444" />
</shape>
</item>
</selector>
1 change: 1 addition & 0 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
app:button_top_padding="20dp"
app:button_bottom_padding="20dp"
app:button_background="@drawable/shape_button"
app:initial_state="enabled"
/>

</LinearLayout>
134 changes: 94 additions & 40 deletions swipe-button/src/main/java/com/ebanx/swipebtn/SwipeButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@

public class SwipeButton extends RelativeLayout {

private ImageView slidingButton;
private ImageView swipeButton;
private float initialX;
private boolean active;
private int initialButtonWidth;
private TextView centerText;
private ViewGroup background;

private Drawable disabledDrawable;
private Drawable enabledDrawable;

private OnStateChangeListener onStateChangeListener;

private static final int ENABLED = 0;
private static final int DISABLED = 1;

public SwipeButton(Context context) {
super(context);

Expand Down Expand Up @@ -68,12 +72,48 @@ public boolean isActive() {
return active;
}

public void setText(String text) {
centerText.setText(text);
}

public void setBackground(Drawable drawable) {
background.setBackground(drawable);
}

public void setSlidingButtonBackground(Drawable drawable) {
background.setBackground(drawable);
}

public void setDisabledDrawable(Drawable drawable) {
disabledDrawable = drawable;

if (!active) {
swipeButton.setImageDrawable(drawable);
}
}

public void setEnabledDrawable(Drawable drawable) {
enabledDrawable = drawable;

if (active) {
swipeButton.setImageDrawable(drawable);
}
}

public void setOnStateChangeListener(OnStateChangeListener onStateChangeListener) {
this.onStateChangeListener = onStateChangeListener;
}

public void setInnerTextPadding(int left, int top, int right, int bottom) {
centerText.setPadding(left, top, right, bottom);
}

public void setSwipeButtonPadding(int left, int top, int right, int bottom) {
swipeButton.setPadding(left, top, right, bottom);
}

private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
RelativeLayout background = new RelativeLayout(context);
background = new RelativeLayout(context);

LayoutParams layoutParamsView = new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
Expand All @@ -91,23 +131,12 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr, int def
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);


layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);

background.addView(centerText, layoutParams);

final ImageView swipeButton = new ImageView(context);

this.slidingButton = swipeButton;

LayoutParams layoutParamsButton = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);

layoutParamsButton.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
layoutParamsButton.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);

addView(swipeButton, layoutParamsButton);
this.swipeButton = swipeButton;

if (attrs != null && defStyleAttr == -1 && defStyleRes == -1) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SwipeButton,
Expand Down Expand Up @@ -145,6 +174,32 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr, int def
float innerTextBottomPadding = typedArray.getDimension(
R.styleable.SwipeButton_inner_text_bottom_padding, 0);

int initialState = typedArray.getInt(R.styleable.SwipeButton_initial_state, DISABLED);

if (initialState == ENABLED) {
LayoutParams layoutParamsButton = new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);

layoutParamsButton.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
layoutParamsButton.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);

addView(swipeButton, layoutParamsButton);

active = true;
} else {
LayoutParams layoutParamsButton = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);

layoutParamsButton.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
layoutParamsButton.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);

addView(swipeButton, layoutParamsButton);

active = false;
}

centerText.setPadding((int) innerTextLeftPadding,
(int) innerTextTopPadding,
(int) innerTextRightPadding,
Expand Down Expand Up @@ -190,33 +245,32 @@ public boolean onTouch(View v, MotionEvent event) {
return true;
case MotionEvent.ACTION_MOVE:
if (initialX == 0) {
initialX = slidingButton.getX();
initialX = swipeButton.getX();
}

if (event.getX() > initialX + slidingButton.getWidth() / 2 &&
event.getX() + slidingButton.getWidth() / 2 < getWidth()) {
slidingButton.setX(event.getX() - slidingButton.getWidth() / 2);
centerText.setAlpha(1 - 1.3f * (slidingButton.getX() + slidingButton.getWidth()) / getWidth());
if (event.getX() > initialX + swipeButton.getWidth() / 2 &&
event.getX() + swipeButton.getWidth() / 2 < getWidth()) {
swipeButton.setX(event.getX() - swipeButton.getWidth() / 2);
centerText.setAlpha(1 - 1.3f * (swipeButton.getX() + swipeButton.getWidth()) / getWidth());
}

if (event.getX() + slidingButton.getWidth() / 2 > getWidth() &&
slidingButton.getX() + slidingButton.getWidth() / 2 < getWidth()) {
slidingButton.setX(getWidth() - slidingButton.getWidth());
if (event.getX() + swipeButton.getWidth() / 2 > getWidth() &&
swipeButton.getX() + swipeButton.getWidth() / 2 < getWidth()) {
swipeButton.setX(getWidth() - swipeButton.getWidth());
}

if (event.getX() < slidingButton.getWidth() / 2 &&
slidingButton.getX() > 0) {
slidingButton.setX(0);
if (event.getX() < swipeButton.getWidth() / 2 &&
swipeButton.getX() > 0) {
swipeButton.setX(0);
}

return true;
case MotionEvent.ACTION_UP:
if (active) {
collapseButton();
} else {
initialButtonWidth = slidingButton.getWidth();

if (slidingButton.getX() + slidingButton.getWidth() > getWidth() * 0.85) {
if (swipeButton.getX() + swipeButton.getWidth() > getWidth() * 0.85) {
initialButtonWidth = swipeButton.getWidth();
expandButton();
} else {
moveButtonBack();
Expand All @@ -233,26 +287,26 @@ public boolean onTouch(View v, MotionEvent event) {

private void expandButton() {
final ValueAnimator positionAnimator =
ValueAnimator.ofFloat(slidingButton.getX(), 0);
ValueAnimator.ofFloat(swipeButton.getX(), 0);
positionAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float x = (Float) positionAnimator.getAnimatedValue();
slidingButton.setX(x);
swipeButton.setX(x);
}
});


final ValueAnimator widthAnimator = ValueAnimator.ofInt(
slidingButton.getWidth(),
swipeButton.getWidth(),
getWidth());

widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams params = slidingButton.getLayoutParams();
ViewGroup.LayoutParams params = swipeButton.getLayoutParams();
params.width = (Integer) widthAnimator.getAnimatedValue();
slidingButton.setLayoutParams(params);
swipeButton.setLayoutParams(params);
}
});

Expand All @@ -264,7 +318,7 @@ public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);

active = true;
slidingButton.setImageDrawable(enabledDrawable);
swipeButton.setImageDrawable(enabledDrawable);

if (onStateChangeListener != null) {
onStateChangeListener.onStateChange(active);
Expand All @@ -278,13 +332,13 @@ public void onAnimationEnd(Animator animation) {

private void moveButtonBack() {
final ValueAnimator positionAnimator =
ValueAnimator.ofFloat(slidingButton.getX(), 0);
ValueAnimator.ofFloat(swipeButton.getX(), 0);
positionAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
positionAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float x = (Float) positionAnimator.getAnimatedValue();
slidingButton.setX(x);
swipeButton.setX(x);
}
});

Expand All @@ -300,15 +354,15 @@ public void onAnimationUpdate(ValueAnimator animation) {

private void collapseButton() {
final ValueAnimator widthAnimator = ValueAnimator.ofInt(
slidingButton.getWidth(),
swipeButton.getWidth(),
initialButtonWidth);

widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams params = slidingButton.getLayoutParams();
ViewGroup.LayoutParams params = swipeButton.getLayoutParams();
params.width = (Integer) widthAnimator.getAnimatedValue();
slidingButton.setLayoutParams(params);
swipeButton.setLayoutParams(params);
}
});

Expand All @@ -317,7 +371,7 @@ public void onAnimationUpdate(ValueAnimator animation) {
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
active = false;
slidingButton.setImageDrawable(disabledDrawable);
swipeButton.setImageDrawable(disabledDrawable);

if (onStateChangeListener != null) {
onStateChangeListener.onStateChange(active);
Expand Down
4 changes: 4 additions & 0 deletions swipe-button/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<attr name="button_top_padding" format="dimension"/>
<attr name="button_bottom_padding" format="dimension"/>
<attr name="button_background" format="reference"/>
<attr name="initial_state" format="enum">
<enum name="enabled" value="0"/>
<enum name="disabled" value="1"/>
</attr>


</declare-styleable>
Expand Down

0 comments on commit 82a219a

Please sign in to comment.