diff --git a/library/src/main/java/co/ceryle/segmentedbutton/util/AnimationCollapse.java b/library/src/main/java/co/ceryle/segmentedbutton/AnimationCollapse.java similarity index 88% rename from library/src/main/java/co/ceryle/segmentedbutton/util/AnimationCollapse.java rename to library/src/main/java/co/ceryle/segmentedbutton/AnimationCollapse.java index 33be224..e99efa3 100644 --- a/library/src/main/java/co/ceryle/segmentedbutton/util/AnimationCollapse.java +++ b/library/src/main/java/co/ceryle/segmentedbutton/AnimationCollapse.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package co.ceryle.segmentedbutton.util; +package co.ceryle.segmentedbutton; import android.animation.ValueAnimator; import android.view.View; @@ -22,9 +22,9 @@ /** * Created by EGE on 16/08/2016. */ -public class AnimationCollapse { +class AnimationCollapse { - public static void expand(final View v, Interpolator interpolator, int duration, int targetWidth) { + static void expand(final View v, Interpolator interpolator, int duration, int targetWidth) { int prevWidth = v.getWidth(); diff --git a/library/src/main/java/co/ceryle/segmentedbutton/ButtonAttributes.java b/library/src/main/java/co/ceryle/segmentedbutton/ButtonAttributes.java index 8272996..89d107e 100644 --- a/library/src/main/java/co/ceryle/segmentedbutton/ButtonAttributes.java +++ b/library/src/main/java/co/ceryle/segmentedbutton/ButtonAttributes.java @@ -21,8 +21,8 @@ /** * Created by EGE on 2.10.2016. */ - -public class ButtonAttributes { + +class ButtonAttributes { private int tintColor, textColor, rippleColor, width = 0; private float weight = 0; @@ -31,115 +31,107 @@ public class ButtonAttributes { private View rippleView; private View dividerView; - public View getRippleView() { + View getRippleView() { return rippleView; } - public LinearLayout.LayoutParams getRippleViewParams() { + LinearLayout.LayoutParams getRippleViewParams() { return (LinearLayout.LayoutParams) rippleView.getLayoutParams(); } - public void setRippleView(View rippleView) { + void setRippleView(View rippleView) { this.rippleView = rippleView; } - public View getDividerView() { + View getDividerView() { return dividerView; } - public LinearLayout.LayoutParams getDividerViewParams() { + LinearLayout.LayoutParams getDividerViewParams() { return (LinearLayout.LayoutParams) dividerView.getLayoutParams(); } - public void setDividerView(View dividerView) { + void setDividerView(View dividerView) { this.dividerView = dividerView; } - public float getWeight() { + float getWeight() { return weight; } - public void setWeight(float weight) { + void setWeight(float weight) { this.weight = weight; } - - - public int getWidth() { + + int getWidth() { return width; } - public boolean hasWidth() { + boolean hasWidth() { return hasWidth; } - public void setHasWidth(boolean hasWidth) { + void setHasWidth(boolean hasWidth) { this.hasWidth = hasWidth; } - public boolean hasWeight() { + boolean hasWeight() { return hasWeight; } - public void setHasWeight(boolean hasWeight) { + void setHasWeight(boolean hasWeight) { this.hasWeight = hasWeight; } - public void setWidth(int width) { - this.width = width; - } - - public ButtonAttributes() { - } - - public ButtonAttributes(int width) { + void setWidth(int width) { this.width = width; } - public int getTintColor() { + int getTintColor() { return tintColor; } - public void setTintColor(int tintColor) { + void setTintColor(int tintColor) { this.tintColor = tintColor; } - public int getTextColor() { + int getTextColor() { return textColor; } - public void setTextColor(int textColor) { + void setTextColor(int textColor) { this.textColor = textColor; } - public boolean hasTintColor() { + boolean hasTintColor() { return hasTintColor; } - public void setTintColor(boolean hasTintColor) { + void setTintColor(boolean hasTintColor) { this.hasTintColor = hasTintColor; } - public boolean hasTextColor() { + boolean hasTextColor() { return hasTextColor; } - public void setTextColor(boolean hasTextColor) { + void setTextColor(boolean hasTextColor) { this.hasTextColor = hasTextColor; } - public boolean hasRippleColor() { + boolean hasRippleColor() { return hasRippleColor; } - public void setRippleColor(boolean hasRippleColor) { + void setRippleColor(boolean hasRippleColor) { this.hasRippleColor = hasRippleColor; } - public int getRippleColor() { + int getRippleColor() { return rippleColor; } - public void setRippleColor(int rippleColor) { + void setRippleColor(int rippleColor) { this.rippleColor = rippleColor; } } \ No newline at end of file diff --git a/library/src/main/java/co/ceryle/segmentedbutton/util/RippleHelper.java b/library/src/main/java/co/ceryle/segmentedbutton/RippleHelper.java similarity index 91% rename from library/src/main/java/co/ceryle/segmentedbutton/util/RippleHelper.java rename to library/src/main/java/co/ceryle/segmentedbutton/RippleHelper.java index 1e38a24..7d4e039 100644 --- a/library/src/main/java/co/ceryle/segmentedbutton/util/RippleHelper.java +++ b/library/src/main/java/co/ceryle/segmentedbutton/RippleHelper.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package co.ceryle.segmentedbutton.util; +package co.ceryle.segmentedbutton; import android.annotation.TargetApi; import android.content.Context; @@ -30,9 +30,9 @@ /** * Created by EGE on 18.8.2016. */ -public class RippleHelper { +class RippleHelper { - public static void setSelectableItemBackground(Context context, View view) { + static void setSelectableItemBackground(Context context, View view) { int[] attrs = new int[]{android.R.attr.selectableItemBackground}; TypedArray ta = context.obtainStyledAttributes(attrs); Drawable drawableFromTheme = ta.getDrawable(0 /* index */); @@ -44,7 +44,7 @@ public static void setSelectableItemBackground(Context context, View view) { } } - public static void setRipple(View view, int pressedColor) { + static void setRipple(View view, int pressedColor) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { view.setBackground(getPressedColorRippleDrawable(pressedColor)); } else { @@ -52,7 +52,7 @@ public static void setRipple(View view, int pressedColor) { } } - public static Drawable createRipple(int normalColor, int pressedColor) { + static Drawable createRipple(int normalColor, int pressedColor) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { return getPressedColorRippleDrawable(pressedColor); } else { @@ -90,5 +90,4 @@ private static ColorStateList getPressedColorSelector(int pressedColor) { private static ColorDrawable getColorDrawableFromColor(int color) { return new ColorDrawable(color); } - } diff --git a/library/src/main/java/co/ceryle/segmentedbutton/util/RoundHelper.java b/library/src/main/java/co/ceryle/segmentedbutton/RoundHelper.java similarity index 87% rename from library/src/main/java/co/ceryle/segmentedbutton/util/RoundHelper.java rename to library/src/main/java/co/ceryle/segmentedbutton/RoundHelper.java index 0b3e3ca..4dffe5d 100644 --- a/library/src/main/java/co/ceryle/segmentedbutton/util/RoundHelper.java +++ b/library/src/main/java/co/ceryle/segmentedbutton/RoundHelper.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package co.ceryle.segmentedbutton.util; +package co.ceryle.segmentedbutton; import android.graphics.drawable.Drawable; import android.graphics.drawable.GradientDrawable; @@ -24,7 +24,7 @@ /** * Created by EGE on 22/08/2016. */ -public class RoundHelper { +class RoundHelper { private static GradientDrawable getGradientDrawable(int dividerColor, int dividerRadius, int dividerSize) { GradientDrawable gradient = @@ -35,7 +35,7 @@ private static GradientDrawable getGradientDrawable(int dividerColor, int divide return gradient; } - public static void makeRound(View view, int dividerColor, int dividerRadius, int dividerSize) { + static void makeRound(View view, int dividerColor, int dividerRadius, int dividerSize) { GradientDrawable gradient = getGradientDrawable(dividerColor, dividerRadius, dividerSize); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) @@ -44,7 +44,7 @@ public static void makeRound(View view, int dividerColor, int dividerRadius, int view.setBackgroundDrawable(gradient); } - public static void makeDividerRound(LinearLayout layout, int dividerColor, int dividerRadius, int dividerSize, Drawable drawable) { + static void makeDividerRound(LinearLayout layout, int dividerColor, int dividerRadius, int dividerSize, Drawable drawable) { GradientDrawable gradient = null; if (null != drawable) { if (drawable instanceof GradientDrawable) { diff --git a/library/src/main/java/co/ceryle/segmentedbutton/RoundedCornerLayout.java b/library/src/main/java/co/ceryle/segmentedbutton/RoundedCornerLayout.java index eb35f91..300d36d 100644 --- a/library/src/main/java/co/ceryle/segmentedbutton/RoundedCornerLayout.java +++ b/library/src/main/java/co/ceryle/segmentedbutton/RoundedCornerLayout.java @@ -34,7 +34,7 @@ import android.view.ViewOutlineProvider; import android.widget.FrameLayout; -public class RoundedCornerLayout extends FrameLayout { +class RoundedCornerLayout extends FrameLayout { private final static float CORNER_RADIUS = 6.0f; private float cornerRadius; @@ -63,7 +63,7 @@ private void init(Context context, AttributeSet attrs, int defStyle) { } } - //Fixes incorrect outline drawn by default + // Fixes incorrect outline drawn by default @TargetApi(Build.VERSION_CODES.LOLLIPOP) private class ButtonOutlineProvider extends ViewOutlineProvider { @Override @@ -72,7 +72,6 @@ public void getOutline(View view, Outline outline) { } } - @Override protected void dispatchDraw(Canvas canvas) { int count = canvas.save(); @@ -86,118 +85,7 @@ protected void dispatchDraw(Canvas canvas) { canvas.restoreToCount(count); } - - public void setRadius(float radius) { + protected void setRadius(float radius) { cornerRadius = radius; } -} - -/* - This layout first draws its contents to an offscreen bitmap, - masks the offscreen bitmap with a rounded rect and then draws the offscreen bitmap on the actual canvas. - I tried it and it seems to work (at least for my simple testcase). - It will of course affect performance compared to a regular layout. - http://stackoverflow.com/questions/26074784/how-to-make-a-view-in-android-with-rounded-corners -*/ - -/* - import android.content.Context; - import android.graphics.Bitmap; - import android.graphics.Canvas; - import android.graphics.Color; - import android.graphics.Outline; - import android.graphics.Paint; - import android.graphics.PorterDuff; - import android.graphics.PorterDuffXfermode; - import android.graphics.RectF; - import android.os.Build; - import android.support.annotation.RequiresApi; - import android.util.AttributeSet; - import android.util.DisplayMetrics; - import android.util.TypedValue; - import android.view.View; - import android.view.ViewOutlineProvider; - import android.widget.FrameLayout; - - -public class RoundedCornerLayout extends FrameLayout { - private float CORNER_RADIUS = 40.0f; - - private Bitmap maskBitmap; - private Paint paint, maskPaint; - private float cornerRadius; - - public void setRadius(float radius){ - CORNER_RADIUS = radius; - } - - public RoundedCornerLayout(Context context) { - super(context); - init(context, null, 0); - } - - public RoundedCornerLayout(Context context, AttributeSet attrs) { - super(context, attrs); - init(context, attrs, 0); - } - - public RoundedCornerLayout(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - init(context, attrs, defStyle); - } - - private void init(Context context, AttributeSet attrs, int defStyle) { - DisplayMetrics metrics = context.getResources().getDisplayMetrics(); - cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, metrics); - - paint = new Paint(Paint.ANTI_ALIAS_FLAG); - - maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); - maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); - - setWillNotDraw(false); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - setOutlineProvider(new ButtonOutlineProvider()); - } - } - - @Override - public void draw(Canvas canvas) { - Bitmap offscreenBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888); - Canvas offscreenCanvas = new Canvas(offscreenBitmap); - - super.draw(offscreenCanvas); - - if (maskBitmap == null) { - maskBitmap = createMask(canvas.getWidth(), canvas.getHeight()); - } - - offscreenCanvas.drawBitmap(maskBitmap, 0f, 0f, maskPaint); - canvas.drawBitmap(offscreenBitmap, 0f, 0f, paint); - } - - private Bitmap createMask(int width, int height) { - Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8); - Canvas canvas = new Canvas(mask); - - Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); - paint.setColor(Color.WHITE); - - canvas.drawRect(0, 0, width, height, paint); - - paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); - canvas.drawRoundRect(new RectF(0, 0, width, height), cornerRadius, cornerRadius, paint); - - return mask; - } - - //Fixes incorrect outline drawn by default - @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) - private class ButtonOutlineProvider extends ViewOutlineProvider{ - @Override - public void getOutline(View view, Outline outline) { - outline.setRoundRect(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight(), cornerRadius); - } - } -} - */ \ No newline at end of file +} \ No newline at end of file diff --git a/library/src/main/java/co/ceryle/segmentedbutton/SegmentedButton.java b/library/src/main/java/co/ceryle/segmentedbutton/SegmentedButton.java index 687ccbe..05e6551 100644 --- a/library/src/main/java/co/ceryle/segmentedbutton/SegmentedButton.java +++ b/library/src/main/java/co/ceryle/segmentedbutton/SegmentedButton.java @@ -27,6 +27,7 @@ import android.text.TextPaint; import android.text.TextUtils; import android.util.AttributeSet; +import android.util.Log; import android.widget.Button; import com.ceryle.segmentedbutton.R; @@ -69,17 +70,9 @@ private void init(AttributeSet attrs) { setTransformationMethod(null); } - public boolean hasImageTint() { - return hasButtonImageTint; - } - - public boolean hasSelectorTint() { - return hasSelectedImageTint; - } - - private int imageTint, selectedImageTint, selectedTextColor, rippleColor; - private boolean hasButtonImageTint, hasSelectedImageTint, hasSelectedTextColor, hasRippleColor; - private float buttonImageScale; + private int imageTint, selectedImageTint, selectedTextColor, rippleColor, buttonWidth; + private boolean hasButtonImageTint, hasSelectedImageTint, hasSelectedTextColor, hasRippleColor, hasButtonWidth, hasButtonWeight; + private float buttonImageScale, buttonWeight; private void getAttributes(AttributeSet attrs) { TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.SegmentedButton); @@ -104,91 +97,12 @@ private void getAttributes(AttributeSet attrs) { hasButtonWidth = typedArray.hasValue(R.styleable.SegmentedButton_android_layout_width); } catch (Exception ignored) { + Log.d("SegmentedButton", ignored.toString()); } typedArray.recycle(); } - private int buttonWidth; - private float buttonWeight; - private boolean hasButtonWidth, hasButtonWeight; - - public void setDrawableTop(int drawableId) { - setCompoundDrawablesWithIntrinsicBounds(0, drawableId, 0, 0); - setImageTint(imageTint); - } - - public void setDrawableBottom(int drawableId) { - setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, drawableId); - setImageTint(imageTint); - } - - public void setDrawableLeft(int drawableId) { - setCompoundDrawablesWithIntrinsicBounds(drawableId, 0, 0, 0); - setImageTint(imageTint); - } - - public void setDrawableRight(int drawableId) { - setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableId, 0); - setImageTint(imageTint); - } - - public boolean hasButtonWidth() { - return hasButtonWidth; - } - - public void setHasButtonWidth(boolean hasButtonWidth) { - this.hasButtonWidth = hasButtonWidth; - } - - public boolean hasButtonWeight() { - return hasButtonWeight; - } - - public void setHasButtonWeight(boolean hasButtonWeight) { - this.hasButtonWeight = hasButtonWeight; - } - - public float getButtonWeight() { - return buttonWeight; - } - - public void setButtonWeight(float buttonWeight) { - this.buttonWeight = buttonWeight; - } - - public int getButtonWidth() { - return buttonWidth; - } - - public void setButtonWidth(int buttonWidth) { - this.buttonWidth = buttonWidth; - } - - public int getRippleColor() { - return rippleColor; - } - - public boolean hasRippleColor() { - return hasRippleColor; - } - - public int getSelectedTextColor() { - return selectedTextColor; - } - - public void setSelectedTextColor(int selectedTextColor) { - this.selectedTextColor = selectedTextColor; - } - - public boolean hasSelectedTextColor() { - return hasSelectedTextColor; - } - - public void setHasSelectedTextColor(boolean hasSelectedTextColor) { - this.hasSelectedTextColor = hasSelectedTextColor; - } - @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); @@ -197,11 +111,10 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto if (buttonImageScale != 1) scaleButtonDrawables(buttonImageScale); - drawButton(); + drawButton(); } - // Pre-allocate objects for layout measuring private Rect textBounds = new Rect(); private Rect drawableBounds = new Rect(); @@ -275,9 +188,8 @@ public void scaleButtonDrawables(double fitFactor) { public void removeImageTint() { for (int i = 0; i < getCompoundDrawables().length; i++) { - if (getCompoundDrawables()[i] != null) { + if (getCompoundDrawables()[i] != null) getCompoundDrawables()[i].clearColorFilter(); - } } } @@ -323,4 +235,90 @@ public float getButtonImageScale() { public boolean hasButtonImageTint() { return hasButtonImageTint; } + + + public void setDrawableTop(int drawableId) { + setCompoundDrawablesWithIntrinsicBounds(0, drawableId, 0, 0); + setImageTint(imageTint); + } + + public void setDrawableBottom(int drawableId) { + setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, drawableId); + setImageTint(imageTint); + } + + public void setDrawableLeft(int drawableId) { + setCompoundDrawablesWithIntrinsicBounds(drawableId, 0, 0, 0); + setImageTint(imageTint); + } + + public void setDrawableRight(int drawableId) { + setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableId, 0); + setImageTint(imageTint); + } + + public boolean hasImageTint() { + return hasButtonImageTint; + } + + public boolean hasSelectorTint() { + return hasSelectedImageTint; + } + + public boolean hasButtonWidth() { + return hasButtonWidth; + } + + public void setHasButtonWidth(boolean hasButtonWidth) { + this.hasButtonWidth = hasButtonWidth; + } + + public boolean hasButtonWeight() { + return hasButtonWeight; + } + + public void setHasButtonWeight(boolean hasButtonWeight) { + this.hasButtonWeight = hasButtonWeight; + } + + public float getButtonWeight() { + return buttonWeight; + } + + public void setButtonWeight(float buttonWeight) { + this.buttonWeight = buttonWeight; + } + + public int getButtonWidth() { + return buttonWidth; + } + + public void setButtonWidth(int buttonWidth) { + this.buttonWidth = buttonWidth; + } + + public int getRippleColor() { + return rippleColor; + } + + public boolean hasRippleColor() { + return hasRippleColor; + } + + public int getSelectedTextColor() { + return selectedTextColor; + } + + public void setSelectedTextColor(int selectedTextColor) { + this.selectedTextColor = selectedTextColor; + } + + public boolean hasSelectedTextColor() { + return hasSelectedTextColor; + } + + public void setHasSelectedTextColor(boolean hasSelectedTextColor) { + this.hasSelectedTextColor = hasSelectedTextColor; + } + } diff --git a/library/src/main/java/co/ceryle/segmentedbutton/SegmentedButtonGroup.java b/library/src/main/java/co/ceryle/segmentedbutton/SegmentedButtonGroup.java index 5186cdc..a50c695 100644 --- a/library/src/main/java/co/ceryle/segmentedbutton/SegmentedButtonGroup.java +++ b/library/src/main/java/co/ceryle/segmentedbutton/SegmentedButtonGroup.java @@ -53,10 +53,6 @@ import java.util.ArrayList; -import co.ceryle.segmentedbutton.util.AnimationCollapse; -import co.ceryle.segmentedbutton.util.RippleHelper; -import co.ceryle.segmentedbutton.util.RoundHelper; - /** * Created by EGE on 20.8.2016. */ diff --git a/sample/src/main/java/co/ceryle/segmentedbutton/MainActivity.java b/sample/src/main/java/co/ceryle/segmentedbutton/MainActivity.java index 5b92a9b..f95eb8b 100644 --- a/sample/src/main/java/co/ceryle/segmentedbutton/MainActivity.java +++ b/sample/src/main/java/co/ceryle/segmentedbutton/MainActivity.java @@ -7,10 +7,38 @@ public class MainActivity extends AppCompatActivity { + private Button button; + private SegmentedButtonGroup group; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + group = (SegmentedButtonGroup) findViewById(R.id.segmentedButtonGroup); + button = (Button) findViewById(R.id.button); + + updateButton(group.getPosition()); + + group.setOnClickedButtonPosition(new SegmentedButtonGroup.OnClickedButtonPosition() { + @Override + public void onClickedButtonPosition(int position) { + updateButton(position); + } + }); + + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + int position = group.getPosition(); + position = ++position % 3; + updateButton(position); + group.setPosition(position, true); + } + }); + } + + private void updateButton(int position) { + button.setText("Position: " + position); } } diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 46daf7d..a3c8e98 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -51,21 +51,21 @@