膨胀后,自定义搜索栏拇指不显示

时间:2014-11-08 07:51:48

标签: android android-seekbar

我正在创建一个自定义搜索栏,就像新材料主题Google Play音乐中的搜索栏一样。我已经完成了为seekbar拇指带来交互性所需的一切。但问题是当视图首次充气并附加到窗口时,会绘制进度条但不会绘制拇指。

经过大量的研究和阅读后,我第一次写了一个观点。知道这种行为的原因会很棒。

我写的搜索栏的代码在这里。

public class SizableSeekBar extends DebugSeekBar {

public static String TAG = "SizableSeekBar";

/**
 * Listener Set Using setOnSeekBarChangeListner(OnSeekBarChangeListener
 * listener)
 */
private OnSeekBarChangeListener mListener;

/**
 * The Thumb Drawable used that will be resized
 */
private Drawable mThumb;

/**
 * Animator used to animate the grow animation
 */
private ValueAnimator growAnimator;

/**
 * Animator used to animate the shrink animation
 */
private ValueAnimator shrinkAnimator;

/**
 * Interpolator used for shrink and grow animation
 */
private TimeInterpolator mInterpolator = new AccelerateDecelerateInterpolator();

/**
 * Listener for animator to update size of the Drawable
 */

/**
 * Maximum Thumb Size Ratio to use for resizing the Thumb
 */
private float mMaxThumbSizeRatio = 2f;

/**
 * Current Thumb size Ratio
 */
private float mCurrentThumbSizeRatio = 1f;

private ValueAnimator.AnimatorUpdateListener mAnimationUpdateListner = new AnimatorUpdateListener() {

    @Override
    public void onAnimationUpdate(ValueAnimator animator) {
        mCurrentThumbSizeRatio = ((Float) animator.getAnimatedValue())
                .floatValue();
        mThumb.setLevel((int) (10000 * (mCurrentThumbSizeRatio / mMaxThumbSizeRatio)));
        SizableSeekBar.this.invalidate();
    }
};

/**
 * Internal OnSeekbarChangeListener used for grow and shrink animation and
 * call respective Callbacks of mListner
 */
private OnSeekBarChangeListener mInternalListener = new OnSeekBarChangeListener() {

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        Log.e(TAG, "Stoping Tracking");
        startThumbShrink();
        if (mListener != null)
            mListener.onStopTrackingTouch(SizableSeekBar.this);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        Log.e(TAG, "Starting Tracking");
        startThumbGrow();
        if (mListener != null)
            mListener.onStartTrackingTouch(SizableSeekBar.this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        if (mListener != null)
            mListener.onProgressChanged(SizableSeekBar.this, progress,
                    fromUser);
    }

};

public SizableSeekBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    super.setOnSeekBarChangeListener(mInternalListener);
    // fixes wrong thumboffset
    setThumbOffset(mThumb.getIntrinsicWidth() / 2);
}

public SizableSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    super.setOnSeekBarChangeListener(mInternalListener);
    // fixes wrong thumboffset
    setThumbOffset(mThumb.getIntrinsicWidth() / 2);
}

@Override
public void setOnSeekBarChangeListener(OnSeekBarChangeListener listener) {
    mListener = listener;
}

/**
 * Sets the Interpolator to be used for grow and shrink animations
 * 
 * @param interpolator
 *            The interpolator which is to be set
 * 
 */
public void setInterpolator(TimeInterpolator interpolator) {
    mInterpolator = interpolator;
}

@Override
public void setThumb(Drawable thumb) {
    if (thumb == null)
        return;
    // If Thumb Drawable is not ScaleDrawable then wrap it up in new
    // ScaleDrawable
    if (!ScaleDrawable.class.isInstance(thumb))
        thumb = new ScaleDrawable(thumb, Gravity.CENTER, 1f, 1f);
    mThumb = thumb;
    super.setThumb(thumb);
    mThumb.setLevel((int) (10000 * (1.f / mMaxThumbSizeRatio)));

}

/**
 * Starts the thumb grow animation
 */
private void startThumbGrow() {
    if (shrinkAnimator != null) {
        shrinkAnimator.cancel();
        shrinkAnimator = null;
    }
    growAnimator = ValueAnimator.ofFloat(1, mMaxThumbSizeRatio);
    growAnimator.setInterpolator(mInterpolator);
    growAnimator.addUpdateListener(mAnimationUpdateListner);
    growAnimator.setDuration(300);
    growAnimator.start();

}

/**
 * Starts the thumb shrink animation
 */
private void startThumbShrink() {
    if (growAnimator != null) {
        growAnimator.cancel();
        growAnimator = null;
    }
    shrinkAnimator = ValueAnimator.ofFloat(mCurrentThumbSizeRatio, 1f);
    shrinkAnimator.setInterpolator(mInterpolator);
    shrinkAnimator.addUpdateListener(mAnimationUpdateListner);
    shrinkAnimator.setDuration(300);
    shrinkAnimator.start();
}

}

1 个答案:

答案 0 :(得分:0)

我只是在构造函数中设置了draw drawable的级别,而不是set thumb()函数。

public SizableSeekBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    super.setOnSeekBarChangeListener(mInternalListener);
    // fixes wrong thumboffset
    setThumbOffset(mThumb.getIntrinsicWidth() / 2);
    mThumb.setLevel((int) (10000 * (mCurrentThumbSizeRatio / mMaxThumbSizeRatio)));
}