【问题标题】:Android:How to custom the specific Seekbar like this?Android:如何像这样自定义特定的Seekbar?
【发布时间】:2014-08-11 11:16:33
【问题描述】:

图片:

我应该如何自定义像图片一样的特定搜索栏?

我认为重点是:

  1. 当手指在搜索栏上移动时,如何呈现一个显示进度的悬浮视图。

  2. 有一些view和seekbar的父view在同一层,这些view在seekbar附近,这些view会和悬浮的view重叠吗?

【问题讨论】:

  • 检查android sdk,有seekbars的例子:) 最好的方法:在eclipse中我们按new>new android example project>你拿api demos>你创建项目>然后你在你的手机上运行它: ) 然后你搜索它。如果你找到它,你可以去你的 Eclipse 代码资源并搜索它,以便能够将代码与手机中的应用程序进行比较:)
  • 感谢您的建议。我搜索了一些信息并知道如何自定义普通搜索栏。但是这个我不知道怎么解决。

标签: android


【解决方案1】:

看看这个答案:13887556

这可能是一个很好的起点。 尝试扩展 SeekBar 并覆盖 onMeasure() 和 onDraw 方法():

@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
 {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    if (labelBackground != null)
    {

        viewWidth = getMeasuredWidth();
        barHeight = getMeasuredHeight();// returns only the bar height (without the label);
        setMeasuredDimension(viewWidth, barHeight + labelBackground.getHeight());
    }

}



@Override
protected synchronized void onDraw(Canvas canvas)
{
    if (labelBackground != null)
    {
        barBounds.left = getPaddingLeft();
        barBounds.top = labelBackground.getHeight() + getPaddingTop();
        barBounds.right = barBounds.left + viewWidth - getPaddingRight() - getPaddingLeft();
        barBounds.bottom = barBounds.top + barHeight - getPaddingBottom() - getPaddingTop();

        progressPosX = barBounds.left + ((float) this.getProgress() / (float) this.getMax()) * barBounds.width();

        labelPos.x = (int) progressPosX - labelOffset;
        labelPos.y = getPaddingTop();

        progressDrawable = getProgressDrawable();
        progressDrawable.setBounds(barBounds.left, barBounds.top, barBounds.right, barBounds.bottom);
        progressDrawable.draw(canvas);

        labelTextPaint.getTextBounds(labelText, 0, labelText.length(), labelTextRect);

        canvas.drawBitmap(labelBackground, labelPos.x, labelPos.y, labelBackgroundPaint);
        canvas.drawText(labelText, labelPos.x + labelBackground.getWidth() / 2 - labelTextRect.width() / 2, labelPos.y + labelBackground.getHeight() / 2 + labelTextRect.height() / 2, labelTextPaint);

        thumbX = (int) progressPosX - getThumbOffset();
        thumbDrawable.setBounds(thumbX, barBounds.top, thumbX + thumbDrawable.getIntrinsicWidth(), barBounds.top + thumbDrawable.getIntrinsicHeight());
        thumbDrawable.draw(canvas);
    } else
    {
        super.onDraw(canvas);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多