Skip to content

Commit

Permalink
Add RTL support. Upgrade dependencies version.
Browse files Browse the repository at this point in the history
  • Loading branch information
hluhovskyi committed Oct 16, 2017
1 parent 54bba69 commit 5f22020
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Button which is visible while user holds it. Main use case is controlling audio
Add library as dependency to your `build.gradle`.

```
compile 'com.dewarder:holdingbutton:0.0.9'
compile 'com.dewarder:holdingbutton:0.1.0'
```

## How to use
Expand Down
9 changes: 7 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ buildscript {
}
}

repositories {
mavenCentral()
allprojects {
repositories {
mavenCentral()
maven {
url 'https://maven.google.com'
}
}
}
17 changes: 7 additions & 10 deletions holdingbutton/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

ext {
artifactVersion = '0.0.9'
artifactVersion = '0.1.0'
artifactName = 'holdingbutton'
siteUrl = 'https://github.com/dewarder/HoldingButton'
gitUrl = 'https://github.com/dewarder/HoldingButton.git'
Expand All @@ -13,18 +13,16 @@ version = artifactVersion
group = 'com.dewarder'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
compileSdkVersion 26
buildToolsVersion "25.0.3"

defaultConfig {
minSdkVersion 15
targetSdkVersion 25
targetSdkVersion 26
versionCode 1
versionName artifactVersion

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
release {
minifyEnabled false
Expand All @@ -35,10 +33,9 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-annotations:25.2.0'
compile 'com.android.support:support-annotations:25.3.1'
}


install {
repositories.mavenInstaller {
pom {
Expand Down Expand Up @@ -110,7 +107,7 @@ bintray {
vcsUrl = gitUrl
version {
name = artifactVersion
desc = 'Fixed touch listener area'
desc = 'Add RTL support. Upgrade dependencies versions.'
released = new Date()
vcsTag = "$artifactVersion"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import android.graphics.Color;

public final class ColorUtils {
final class ColorHelper {

public static int blend(int from, int to, float ratio) {
static int blend(int from, int to, float ratio) {
float inverseRatio = 1f - ratio;

float a = Color.alpha(to) * ratio + Color.alpha(from) * inverseRatio;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.dewarder.holdinglibrary;

import android.os.Build;
import android.view.View;

import com.dewarder.holdinglibrary.HoldingButtonLayout.Direction;
import com.dewarder.holdinglibrary.HoldingButtonLayout.LayoutDirection;

final class DirectionHelper {

static LayoutDirection resolveLayoutDirection(HoldingButtonLayout view) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return LayoutDirection.LTR;
}

int rawDirection = view.getResources().getConfiguration().getLayoutDirection();
if (rawDirection == View.LAYOUT_DIRECTION_LTR) {
return LayoutDirection.LTR;
} else {
return LayoutDirection.RTL;
}
}

static Direction resolveDefaultSlidingDirection(HoldingButtonLayout view) {
if (resolveLayoutDirection(view) == LayoutDirection.LTR) {
return Direction.START;
} else {
return Direction.END;
}
}

static Direction adaptSlidingDirection(HoldingButtonLayout view, Direction direction) {
if (resolveLayoutDirection(view) == LayoutDirection.LTR) {
return direction;
} else {
return direction.toRtl();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@

public class HoldingButtonLayout extends FrameLayout {

private int mHoldingViewId = -1;
private static final float DEFAULT_CANCEL_OFFSET = 0.3f;
private static final float COLLAPSING_SCALE_Y_VALUE_START = 0.8f;
private static final float COLLAPSING_SCALE_Y_VALUE_END = 1f;
private static final float COLLAPSING_ALPHA_VALUE_START = 0f;
private static final float COLLAPSING_ALPHA_VALUE_END = 1f;

private int mHoldingViewId = NO_ID;
private View mHoldingView;
private Rect mHoldingViewRect = new Rect();
private float mCancelOffset = 0.3f;
private float mCancelOffset = DEFAULT_CANCEL_OFFSET;
private float mDeltaX;

private View mHoldingCircle;
Expand All @@ -54,12 +60,15 @@ public class HoldingButtonLayout extends FrameLayout {
private int[] mViewLocation = new int[2];
private int[] mHoldingViewLocation = new int[2];

private Direction mDirection = Direction.START;
private LayoutDirection mLayoutDirection = LayoutDirection.LTR;
private Direction mDirection;
private boolean mAnimateHoldingView = true;
private boolean mButtonEnabled = true;
private boolean mIsCancel = false;
private boolean mIsExpanded = false;

private int mCollapsingAnimationDuration;

private HoldingButtonTouchListener mTouchListener = new SimpleHoldingButtonTouchListener();
private final DrawableListener mDrawableListener = new DrawableListener();
private final List<HoldingButtonLayoutListener> mListeners = new ArrayList<>();
Expand Down Expand Up @@ -95,8 +104,11 @@ public HoldingButtonLayout(@NonNull Context context,
}

private void init(Context context, AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
mCollapsingAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);

mHoldingDrawable = new HoldingDrawable();
mHoldingDrawable.setListener(mDrawableListener);
mLayoutDirection = DirectionHelper.resolveLayoutDirection(this);

if (attrs != null) {
TypedArray array = context.getTheme().obtainStyledAttributes(attrs,
Expand Down Expand Up @@ -130,7 +142,7 @@ private void init(Context context, AttributeSet attrs, @AttrRes int defStyleAttr
}

if (array.hasValue(R.styleable.HoldingButtonLayout_hbl_holding_view)) {
mHoldingViewId = array.getResourceId(R.styleable.HoldingButtonLayout_hbl_holding_view, -1);
mHoldingViewId = array.getResourceId(R.styleable.HoldingButtonLayout_hbl_holding_view, NO_ID);
}

if (array.hasValue(R.styleable.HoldingButtonLayout_hbl_animate_holding_view)) {
Expand Down Expand Up @@ -166,12 +178,18 @@ private void init(Context context, AttributeSet attrs, @AttrRes int defStyleAttr
}

if (array.hasValue(R.styleable.HoldingButtonLayout_hbl_direction)) {
int flag = array.getInt(R.styleable.HoldingButtonLayout_hbl_direction, 0);
mDirection = Direction.fromFlag(flag);
int directionFlag = array.getInt(R.styleable.HoldingButtonLayout_hbl_direction, -1);
if (directionFlag != -1) {
mDirection = DirectionHelper.adaptSlidingDirection(this, Direction.fromFlag(directionFlag));
}
}

array.recycle();
}

if (mDirection == null) {
mDirection = DirectionHelper.resolveDefaultSlidingDirection(this);
}
}

@Override
Expand All @@ -196,16 +214,20 @@ public boolean onTouchEvent(MotionEvent event) {
mHoldingView.getLocationInWindow(mHoldingViewLocation);
getLocationInWindow(mViewLocation);

int centerX = mHoldingViewLocation[0] + mHoldingView.getWidth() / 2;
int centerY = mHoldingViewLocation[1] + mHoldingView.getHeight() / 2;
int layoutWidth = getWidth();
int viewCenterX = mHoldingViewLocation[0] + mHoldingView.getWidth() / 2;
int circleCenterX = mHoldingCircle.getWidth() / 2;
int offsetX = mDirection.getOffsetX(mOffset[0]);
float translationX = mLayoutDirection.calculateTranslationX(
layoutWidth, viewCenterX, circleCenterX, offsetX);

float translationX = centerX - mHoldingCircle.getWidth() / 2f + mDirection.getOffsetX(mOffset[0]);
int centerY = mHoldingViewLocation[1] + mHoldingView.getHeight() / 2;
float translationY = centerY - mHoldingCircle.getHeight() / 2f + mOffset[1];

mHoldingCircle.setTranslationX(translationX);
mHoldingCircle.setTranslationY(translationY);

mDeltaX = event.getRawX() - centerX - mDirection.getOffsetX(mOffset[0]);
mDeltaX = event.getRawX() - viewCenterX - offsetX;

mHoldingDrawable.expand();
mIsCancel = false;
Expand Down Expand Up @@ -250,7 +272,7 @@ protected void onAttachedToWindow() {
@Override
protected void onFinishInflate() {
super.onFinishInflate();
if (mHoldingView == null && mHoldingViewId != -1) {
if (mHoldingView == null && mHoldingViewId != NO_ID) {
mHoldingView = findViewById(mHoldingViewId);
}

Expand Down Expand Up @@ -394,7 +416,7 @@ public Direction getDirection() {
}

public void setDirection(Direction direction) {
mDirection = direction;
mDirection = DirectionHelper.adaptSlidingDirection(this, direction);
}

public void setAnimateHoldingView(boolean animate) {
Expand Down Expand Up @@ -488,21 +510,72 @@ public void onCollapse() {
mHoldingCircle.setVisibility(GONE);

if (mAnimateHoldingView) {
mHoldingView.setAlpha(0f);
mHoldingView.setScaleY(0.8f);
mHoldingView.setAlpha(COLLAPSING_ALPHA_VALUE_START);
mHoldingView.setScaleY(COLLAPSING_SCALE_Y_VALUE_START);
mHoldingView.setVisibility(VISIBLE);
mHoldingView.animate()
.alpha(1f)
.scaleY(1f)
.setDuration(getResources().getInteger(android.R.integer.config_shortAnimTime))
.alpha(COLLAPSING_ALPHA_VALUE_END)
.scaleY(COLLAPSING_SCALE_Y_VALUE_END)
.setDuration(mCollapsingAnimationDuration)
.start();
}
}
}

public enum Direction {
enum LayoutDirection {
LTR {
@Override
public float calculateTranslationX(int layoutWidth, int viewCenterX, int circleCenterX, int offsetX) {
return viewCenterX - circleCenterX + offsetX;
}
},
RTL {
@Override
public float calculateTranslationX(int layoutWidth, int viewCenterX, int circleCenterX, int offsetX) {
return -layoutWidth + viewCenterX + circleCenterX + offsetX;
}
};

public abstract float calculateTranslationX(
int layoutWidth, int viewCenterX, int circleCenterX, int offsetX);
}

public enum Direction {
START(0) {
@Override
int getOffsetX(int offsetX) {
return LEFT.getOffsetX(offsetX);
}

@Override
float getSlideOffset(float x, float circleCenterX, int[] viewLocation, int viewWidth, int[] holdingViewLocation, int holdingViewWidth, int[] offset) {
return LEFT.getSlideOffset(x, circleCenterX, viewLocation, viewWidth, holdingViewLocation, holdingViewWidth, offset);
}

@Override
Direction toRtl() {
return END;
}
},

END(1) {
@Override
int getOffsetX(int offsetX) {
return RIGHT.getOffsetX(offsetX);
}

@Override
float getSlideOffset(float x, float circleCenterX, int[] viewLocation, int viewWidth, int[] holdingViewLocation, int holdingViewWidth, int[] offset) {
return RIGHT.getSlideOffset(x, circleCenterX, viewLocation, viewWidth, holdingViewLocation, holdingViewWidth, offset);
}

@Override
Direction toRtl() {
return START;
}
},

LEFT(2) {
@Override
int getOffsetX(int offsetX) {
return offsetX;
Expand All @@ -514,9 +587,14 @@ float getSlideOffset(float x, float circleCenterX, int[] viewLocation, int viewW
float minX = viewLocation[0] + circleCenterX;
return (x + circleCenterX - holdingViewCenterX - offset[0]) / (minX - holdingViewCenterX);
}

@Override
Direction toRtl() {
return LEFT;
}
},

END(1) {
RIGHT(3) {
@Override
int getOffsetX(int offsetX) {
return -offsetX;
Expand All @@ -528,6 +606,11 @@ float getSlideOffset(float x, float circleCenterX, int[] viewLocation, int viewW
float maxX = viewLocation[0] + viewWidth - circleCenterX;
return (x + circleCenterX - holdingViewCenterX + offset[0]) / (maxX - holdingViewCenterX);
}

@Override
Direction toRtl() {
return RIGHT;
}
};

private final int mFlag;
Expand All @@ -540,7 +623,9 @@ float getSlideOffset(float x, float circleCenterX, int[] viewLocation, int viewW

abstract float getSlideOffset(float x, float circleCenterX, int[] viewLocation, int viewWidth, int[] holdingViewLocation, int holdingViewWidth, int[] offset);

public static Direction fromFlag(int flag) {
abstract Direction toRtl();

static Direction fromFlag(int flag) {
for (Direction direction : values()) {
if (direction.mFlag == flag) {
return direction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ private ValueAnimator createCancelValueAnimator() {
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator a) {
int color = ColorUtils.blend(from, to, (float) a.getAnimatedValue());
int color = ColorHelper.blend(from, to, (float) a.getAnimatedValue());
mPaint.setColor(color);
mSecondPaint.setColor(color);
mSecondPaint.setAlpha(mSecondAlpha);
Expand Down
2 changes: 2 additions & 0 deletions holdingbutton/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<attr name="hbl_direction" format="enum">
<enum name="start" value="0"/>
<enum name="end" value="1"/>
<enum name="left" value="2"/>
<enum name="right" value="3"/>
</attr>
</declare-styleable>

Expand Down
Loading

0 comments on commit 5f22020

Please sign in to comment.