自定义SeekBar具有褪色的alpha颜色渐变,黑色细边框和平铺背景

问题描述 投票:1回答:2

The goal

我正在尝试复制这个Dialog。

TheDialog

The current state

但我只是到了这一点(仅显示相关部分):

MyDialog

What is missing

一种在我的自定义Seekbar“进度区域”周围添加细黑色边框的方法,以及仅限于“进度区域”的平铺背景。

My code

实际上,我的CustomSeekBarextends AppCompatSeekBar)可以在代码中设置起始和结束颜色。这是功能:

public void setGradientColor(@ColorInt int leftColor, @ColorInt int rightcolor) {
    grad = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
            new int[] {leftColor, rightcolor});
    setProgressDrawable(grad);
}

但是尝试设置背景的图像最终看起来就像我在The current state图像中的最后一个搜索栏(背景延伸到进度区域之外并填充所有视图的区域):

    aSeekBar.setBackground(context.getDrawable(R.drawable.checker));
    aSeekBar.setGradientColor(Color.parseColor("#00000000"), Color.parseColor("#FF000000"));

我在checker.xmlres/drawable文件:

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/checkerstile"
    android:tileMode="repeat"
    />
android seekbar android-seekbar
2个回答
1
投票

对于问题的“边框”部分,您只需向GradientDrawable添加一个笔画:

  public void setGradientColor(@ColorInt int leftColor, @ColorInt int rightcolor) {
        grad = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
            new int[] {leftColor, rightcolor});
        grad.setStroke(/* stroke width*/, /* int color*/);
        setProgressDrawable(grad);
    }

为此,它很容易,抱歉我无法帮助的背景。


1
投票

我找到了一个可行的解决方案:

    int padding = aSeekBar.getPaddingStart();
    InsetDrawable bgImg = new InsetDrawable(context.getDrawable(R.drawable.checker), padding);
    aSeekBar.setBackground(bgImg);

将此与the solution from Olivier结合用于边界,您将得到以下结果:

phone_demo

© www.soinside.com 2019 - 2024. All rights reserved.