ColorPickerView icon indicating copy to clipboard operation
ColorPickerView copied to clipboard

Not correct position for alphaSlideBar after first init colorPickerView color.

Open demoCrash opened this issue 4 years ago • 8 comments

open demo ,you can find the alpha is 0xff, but not at the end of the sliderbar. the problem is in your class file AbstractSlider.java

  protected int getSelectorHalfSize() {
    return (int) (selector.getMeasuredWidth());
  }

not divide by two. brightnessSlideBar extends this class may be the same problem. Hope it is useful to you.

demoCrash avatar Jan 10 '21 08:01 demoCrash

hey @skydoves, do you have plans for fixing this?

PierfrancescoSoffritti avatar Feb 16 '21 11:02 PierfrancescoSoffritti

Hi, @PierfrancescoSoffritti !

I've already fixed this issue in my local repository, but I missed push and release a new version because of migrating to maven central. I will let you know about the next release soon. Thanks!

skydoves avatar Feb 16 '21 12:02 skydoves

It has been released a snapshot 2.2.3. Please test work as well using the new snapshot. Thanks for your issue :)

implementation "com.github.skydoves:colorpickerview:2.2.3-SNAPSHOT"

skydoves avatar Feb 16 '21 13:02 skydoves

Hey I just had time to try this out, version 2.2.3 still has the same issue.

When dragging the slider, when approaching the end of the bar it snaps to the end of it.

PierfrancescoSoffritti avatar Feb 21 '21 10:02 PierfrancescoSoffritti

@PierfrancescoSoffritti
Oh, I think that's another issue. 🤦‍♂ I will look into the details. Thanks!

skydoves avatar Feb 21 '21 10:02 skydoves

Hi!

I found several problems regarding initial selector positioning and also regarding touch events which makes the selector to snap either to the front or to the end of the bar within certain distance. The initial positioning is wrong in the onInflateFinished method, since it sets the default position to getMeasuredWidth, which actually places the selector outside the visible area, which means if there is no color selected (eg. color is black and the alpha and also the brightness is 0) the selector is set outside the visible area (and not to 0 position where it should be and regardless how many times you set it to 0, the onInflateFinished is called later, and you loose what you were setting during onCreate for example). May be the default position should be initialized as 0. Other problems I've found are in the onTouchReceived method, which calculates the position of the selector totally wrong. I've modified it like this, and works like a charm: `if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) { // position thumb into click center, this will be the left side of the selector's new position float x = (eventX - (getSelectorSize() / 2f));

        if (x < 0) x = 0; // if user dragged the selector below the left bound, correct it to 0, we reached left side
        if (x > getMeasuredWidth() - getSelectorSize()) x = getMeasuredWidth() - getSelectorSize(); // if user dragged the selector above the right bound (minus the selectors width) then correct it to the maximum possible position

        // calculate new selector position according to the clicked position
        selectorPosition = x / (getMeasuredWidth() - getSelectorSize());

        // to be sure, but normally it cannot happen
        if (selectorPosition > 1f) selectorPosition = 1f;
        else if (selectorPosition < 0f) selectorPosition = 0f;

        // set the position of the selector
        selectedX = (int) x;
        selector.setX(x);
    }
    else if (event.getAction() == MotionEvent.ACTION_UP...`

This way the selector is positioned always into the click point center and does not snap to 0 or max width, like it did before... The getBoundaryX is totally useless, it modifies the X rather wrong than correct, the modification can be made during touch event or selector positioning.

Also setSelectorPosition had to be adjusted:

public void setSelectorPosition(@FloatRange(from = 0.0, to = 1.0) float selectorPosition) { this.selectorPosition = Math.min(selectorPosition, 1.0f); float x = (getMeasuredWidth() - getSelectorSize()) * selectorPosition; selectedX = (int) x; selector.setX(x); invalidate(); }

I did not touch the setSelectorByHalfSelectorPosition method, I don't know how that can be useful since it should also position the selector like the normal positioning... (if you set position to 0, the selector should start with x = 0, regardless if you are positioning it ByHalfSelector or by normal Selector position...

May be my case was special, since I use the brightnessslidebar separately not attached to the colorpicker, since I need them to be separated, therefore I subclassed it and also added a listener to get the position of the selector when the user releases the thumb, but it was acting wrong until I modified those things.

kovadam69 avatar Mar 04 '21 07:03 kovadam69

Has this issue been resolved? The selector is still snapping to the end of the bar when dragging. Using version 2.2.4

aadeshrana avatar May 27 '22 02:05 aadeshrana

#97

skydoves avatar Apr 08 '23 04:04 skydoves