From 82a219a9505bad2de02e984c3e4dafea00357bc3 Mon Sep 17 00:00:00 2001 From: Leandro Ferreira Date: Mon, 12 Jun 2017 13:27:46 -0300 Subject: [PATCH] Setters enabled. --- .idea/kotlinc.xml | 7 + .idea/misc.xml | 2 +- .idea/modules.xml | 4 +- app/build.gradle | 4 +- .../com/ebanx/swipebutton/MainActivity.java | 8 ++ app/src/main/res/drawable/shape_button2.xml | 9 ++ app/src/main/res/layout/content_main.xml | 1 + .../java/com/ebanx/swipebtn/SwipeButton.java | 134 ++++++++++++------ swipe-button/src/main/res/values/attrs.xml | 4 + 9 files changed, 128 insertions(+), 45 deletions(-) create mode 100644 .idea/kotlinc.xml create mode 100644 app/src/main/res/drawable/shape_button2.xml diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..1c24f9a --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index ff4618c..9503cdd 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,7 +37,7 @@ - + diff --git a/.idea/modules.xml b/.idea/modules.xml index 45fa57a..078c317 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,9 +2,9 @@ - - + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 680eed0..0e36ab6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } diff --git a/app/src/main/java/com/ebanx/swipebutton/MainActivity.java b/app/src/main/java/com/ebanx/swipebutton/MainActivity.java index d15be54..cb5ae63 100644 --- a/app/src/main/java/com/ebanx/swipebutton/MainActivity.java +++ b/app/src/main/java/com/ebanx/swipebutton/MainActivity.java @@ -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; @@ -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) { diff --git a/app/src/main/res/drawable/shape_button2.xml b/app/src/main/res/drawable/shape_button2.xml new file mode 100644 index 0000000..63acf57 --- /dev/null +++ b/app/src/main/res/drawable/shape_button2.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index d07cb93..34b1442 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -29,6 +29,7 @@ app:button_top_padding="20dp" app:button_bottom_padding="20dp" app:button_background="@drawable/shape_button" + app:initial_state="enabled" /> diff --git a/swipe-button/src/main/java/com/ebanx/swipebtn/SwipeButton.java b/swipe-button/src/main/java/com/ebanx/swipebtn/SwipeButton.java index dfd6d22..70b734e 100644 --- a/swipe-button/src/main/java/com/ebanx/swipebtn/SwipeButton.java +++ b/swipe-button/src/main/java/com/ebanx/swipebtn/SwipeButton.java @@ -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); @@ -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, @@ -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, @@ -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, @@ -190,23 +245,23 @@ 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; @@ -214,9 +269,8 @@ public boolean onTouch(View v, MotionEvent event) { 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(); @@ -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); } }); @@ -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); @@ -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); } }); @@ -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); } }); @@ -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); diff --git a/swipe-button/src/main/res/values/attrs.xml b/swipe-button/src/main/res/values/attrs.xml index 7cf2690..d34c16c 100644 --- a/swipe-button/src/main/res/values/attrs.xml +++ b/swipe-button/src/main/res/values/attrs.xml @@ -16,6 +16,10 @@ + + + +