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

Commit

Permalink
Some code improvements. See commit for details
Browse files Browse the repository at this point in the history
  • Loading branch information
mardous committed Sep 19, 2020
1 parent 7a8ab4b commit a41a4c7
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 173 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,3 @@ You can also use the attribute **discreteSeekBarStyle** on your themes with a cu
[Animatable Drawable]:https://developer.android.com/reference/android/graphics/drawable/Animatable.html
[PopupWindow]:https://developer.android.com/reference/android/widget/PopupWindow.html
[Format]:https://developer.android.com/reference/java/util/Formatter.html

Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public NumericTransformer getNumericTransformer() {
* Also if the current progress is out of the new range, it will be set to MIN
* </p>
*
* @param max
* @param max the value.
* @see #setMin(int)
* @see #setProgress(int)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.mardous.discreteseekbar.internal;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
Expand All @@ -31,7 +32,6 @@
import android.widget.TextView;
import androidx.core.view.ViewCompat;
import com.mardous.discreteseekbar.R;
import com.mardous.discreteseekbar.internal.compat.SeekBarCompat;
import com.mardous.discreteseekbar.internal.drawable.MarkerDrawable;

/**
Expand All @@ -41,9 +41,8 @@
* and the {@link com.mardous.discreteseekbar.internal.drawable.MarkerDrawable}
* with the required positions and offsets
* </p>
*
* @hide
*/
@SuppressLint("ViewConstructor")
public class Marker extends ViewGroup implements MarkerDrawable.MarkerAnimationListener {
private static final int PADDING_DP = 4;
private static final int ELEVATION_DP = 8;
Expand All @@ -54,14 +53,16 @@ public class Marker extends ViewGroup implements MarkerDrawable.MarkerAnimationL
//some distance between the thumb and our bubble marker.
//This will be added to our measured height
private int mSeparation;
MarkerDrawable mMarkerDrawable;
private MarkerDrawable mMarkerDrawable;

public Marker(Context context, AttributeSet attrs, int defStyleAttr, String maxValue, int thumbSize, int separation) {
super(context, attrs, defStyleAttr);
//as we're reading the parent DiscreteSeekBar attributes, it may wrongly set this view's visibility.
setVisibility(View.VISIBLE);

DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();

@SuppressLint("CustomViewStyleable")
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DiscreteSeekBar,
R.attr.discreteSeekBarStyle, R.style.Widget_DiscreteSeekBar);

Expand All @@ -71,12 +72,23 @@ public Marker(Context context, AttributeSet attrs, int defStyleAttr, String maxV
mNumber = new TextView(context);
//Add some padding to this textView so the bubble has some space to breath
mNumber.setPadding(padding, 0, padding, 0);
mNumber.setTextAppearance(context, textAppearanceId);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mNumber.setTextAppearance(textAppearanceId);
} else {
//noinspection deprecation
mNumber.setTextAppearance(context, textAppearanceId);
}

mNumber.setGravity(Gravity.CENTER);
mNumber.setText(maxValue);
mNumber.setMaxLines(1);
mNumber.setSingleLine(true);
SeekBarCompat.setTextDirection(mNumber, TEXT_DIRECTION_LOCALE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mNumber.setTextDirection(TEXT_DIRECTION_LOCALE);
}

mNumber.setVisibility(View.INVISIBLE);

//add some padding for the elevation shadow not to be clipped
Expand All @@ -92,12 +104,25 @@ public Marker(Context context, AttributeSet attrs, int defStyleAttr, String maxV
mMarkerDrawable.setMarkerListener(this);
mMarkerDrawable.setExternalOffset(padding);

//Elevation for anroid 5+
//Elevation for Android 5+
float elevation = a.getDimension(R.styleable.DiscreteSeekBar_dsb_indicatorElevation, ELEVATION_DP * displayMetrics.density);
ViewCompat.setElevation(this, elevation);

/*
This code causes an Exception in Android 10+
See: https://github.com/AnderWeb/discreteSeekBar/issues/127
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
SeekBarCompat.setOutlineProvider(this, mMarkerDrawable);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setConvexPath(mMarkerDrawable.getPath());
}
});
}
*/

a.recycle();
}

Expand Down Expand Up @@ -163,20 +188,20 @@ protected void onAttachedToWindow() {
animateOpen();
}

public void setValue(CharSequence value) {
mNumber.setText(value);
}

public CharSequence getValue() {
return mNumber.getText();
}

public void animateOpen() {
void setValue(CharSequence value) {
mNumber.setText(value);
}

void animateOpen() {
mMarkerDrawable.stop();
mMarkerDrawable.animateToPressed();
}

public void animateClose() {
void animateClose() {
mMarkerDrawable.stop();
mNumber.setVisibility(View.INVISIBLE);
mMarkerDrawable.animateToNormal();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
* This will attach a View to the Window (full-width, measured-height, positioned just under the thumb)
* </p>
*
* @hide
* @see #showIndicator(android.view.View, android.graphics.Rect)
* @see #dismiss()
* @see #dismissComplete()
Expand All @@ -55,7 +54,7 @@ public class PopupIndicator {
//... phew!
private MarkerDrawable.MarkerAnimationListener mListener;
private int[] mDrawingLocation = new int[2];
Point screenSize = new Point();
private Point screenSize = new Point();

public PopupIndicator(Context context, AttributeSet attrs, int defStyleAttr, String maxValue, int thumbSize, int separation) {
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,35 @@
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.os.Build;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.graphics.drawable.DrawableCompat;
import com.mardous.discreteseekbar.internal.drawable.AlmostRippleDrawable;
import com.mardous.discreteseekbar.internal.drawable.MarkerDrawable;

/**
* Wrapper compatibility class to call some API-Specific methods
* And offer alternate procedures when possible
*
* @hide
*/
public class SeekBarCompat {

/**
* Sets the custom Outline provider on API>=21.
* Does nothing on API<21
*
* @param view
* @param markerDrawable
*/
public static void setOutlineProvider(View view, final MarkerDrawable markerDrawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
SeekBarCompatDontCrash.setOutlineProvider(view, markerDrawable);
}
}

/**
* Our DiscreteSeekBar implementation uses a circular drawable on API < 21
* because we don't set it as Background, but draw it ourselves
*
* @param colorStateList
* @return
* @param colorStateList The ripple colors.
* @return The ripple drawable.
*/
public static Drawable getRipple(ColorStateList colorStateList) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return SeekBarCompatDontCrash.getRipple(colorStateList);
return new RippleDrawable(colorStateList, null, null);
} else {
return new AlmostRippleDrawable(colorStateList);
}
}

/**
* Sets the color of the seekbar ripple
* @param drawable
* @param colorStateList The ColorStateList the track ripple will be changed to
*
* @param drawable The ripple drawable.
* @param colorStateList The ColorStateList the track ripple will be changed to.
*/
public static void setRippleColor(@NonNull Drawable drawable, ColorStateList colorStateList) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Expand All @@ -80,11 +62,7 @@ public static void setRippleColor(@NonNull Drawable drawable, ColorStateList col
* As our DiscreteSeekBar implementation uses a circular drawable on API < 21
* we want to use the same method to set its bounds as the Ripple's hotspot bounds.
*
* @param drawable
* @param left
* @param top
* @param right
* @param bottom
* @param drawable The drawable to modify.
*/
public static void setHotspotBounds(Drawable drawable, int left, int top, int right, int bottom) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Expand All @@ -95,17 +73,4 @@ public static void setHotspotBounds(Drawable drawable, int left, int top, int ri
drawable.setBounds(left, top, right, bottom);
}
}

/**
* Sets the TextView text direction attribute when possible
*
* @param textView
* @param textDirection
* @see android.widget.TextView#setTextDirection(int)
*/
public static void setTextDirection(TextView textView, int textDirection) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
SeekBarCompatDontCrash.setTextDirection(textView, textDirection);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public boolean setState(int[] stateSet) {
for (int i : oldState) {
if (i == android.R.attr.state_pressed) {
oldPressed = true;
break;
}
}
super.setState(stateSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
* <li>Animates color change from the normal state color to the pressed state color</li>
* <li>Uses a {@link android.graphics.Path} to also serve as Outline for API>=21</li>
* </ul>
*
* @hide
*/
public class MarkerDrawable extends StateDrawable implements Animatable {
private static final long FRAME_DURATION = 1000 / 60;
Expand All @@ -64,9 +62,9 @@ public class MarkerDrawable extends StateDrawable implements Animatable {
private int mStartColor;//Color when the Marker is OPEN
private int mEndColor;//Color when the arker is CLOSED

Path mPath = new Path();
RectF mRect = new RectF();
Matrix mMatrix = new Matrix();
private Path mPath = new Path();
private RectF mRect = new RectF();
private Matrix mMatrix = new Matrix();
private MarkerAnimationListener mMarkerListener;

public MarkerDrawable(@NonNull ColorStateList tintList, int closedSize) {
Expand Down Expand Up @@ -123,8 +121,7 @@ private void computePath(Rect bounds) {
int totalSize = Math.min(bounds.width(), bounds.height());

float initial = mClosedStateSize;
float destination = totalSize;
float currentSize = initial + (destination - initial) * currentScale;
float currentSize = initial + ((float) totalSize - initial) * currentScale;

float halfSize = currentSize / 2f;
float inverseScale = 1f - currentScale;
Expand Down Expand Up @@ -242,8 +239,8 @@ private static int blendColors(int color1, int color2, float factor) {
* This is the "poor's man" AnimatorListener for this Drawable
*/
public interface MarkerAnimationListener {
public void onClosingComplete();
void onClosingComplete();

public void onOpeningComplete();
void onOpeningComplete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
* <p>
* Subclasses should implement {@link #doDraw(android.graphics.Canvas, android.graphics.Paint)}
* </p>
*
* @hide
*/
public abstract class StateDrawable extends Drawable {
private ColorStateList mTintStateList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
* <p>
* This special delay is meant to help avoiding frame glitches while the {@link com.mardous.discreteseekbar.internal.Marker} is added to the Window
* </p>
*
* @hide
*/
public class ThumbDrawable extends StateDrawable implements Animatable {
//The current size for this drawable. Must be converted to real DPs
Expand Down
Loading

0 comments on commit a41a4c7

Please sign in to comment.