java - 设置颜色搜索栏

标签 java android seekbar

我正在使用 android studio java 创建一个小型绘图 Android 应用程序。我在菜单中创建一个用于更改绘图颜色的图标。一旦用户单击它,就会出现一个新的 AlertDialog,其中显示 4 个 ARGB 颜色的搜索栏。我以这段代码结束,但我仍然不知道为什么它不起作用。有人可以帮我吗?

private Drawing draws;

private SeekBar alphaSeekBar;
private SeekBar redSeekBar;
private SeekBar greenSeekBar;
private SeekBar blueSeekBar;

private void showColorDialog(){
    currentAlertDialog = new AlertDialog.Builder(this);
    View view = getLayoutInflater().inflate(R.layout.color_dialog, null);
    alphaSeekBar = view.findViewById(R.id.alphaSeekBar);
    redSeekBar = view.findViewById(R.id.redSeekBar);
    greenSeekBar = view.findViewById(R.id.greenSeekBar);
    blueSeekBar = view.findViewById(R.id.blueSeekBar);
    colorView = view.findViewById(R.id.colorView);

    alphaSeekBar.setOnSeekBarChangeListener(colorSeekBarChanged);
    redSeekBar.setOnSeekBarChangeListener(colorSeekBarChanged);
    greenSeekBar.setOnSeekBarChangeListener(colorSeekBarChanged);
    blueSeekBar.setOnSeekBarChangeListener(colorSeekBarChanged);

    int color = draws.getDrawingColor();
    alphaSeekBar.setProgress(Color.alpha(color));
    redSeekBar.setProgress(Color.red(color));
    greenSeekBar.setProgress(Color.green(color));
    blueSeekBar.setProgress(Color.blue(color));

    Button setColorButton = view.findViewById(R.id.setColorButton);
    setColorButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            pikassoView.setDrawingColor(Color.argb(
                    alphaSeekBar.getProgress(),
                    redSeekBar.getProgress(),
                    greenSeekBar.getProgress(),
                    blueSeekBar.getProgress()
            ));

            colorDialog.dismiss();
        }
    });

    currentAlertDialog.setView(view);
    currentAlertDialog.setTitle("Choose color");
    colorDialog = currentAlertDialog.create();
    colorDialog.show();
}   private SeekBar.OnSeekBarChangeListener colorSeekBarChanged = new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        draws.setBackgroundColor(Color.argb(
                alphaSeekBar.getProgress(),
                redSeekBar.getProgress(),
                greenSeekBar.getProgress(),
                blueSeekBar.getProgress()
        ));


        colorView.setBackgroundColor(Color.argb(
                alphaSeekBar.getProgress(),
                redSeekBar.getProgress(),
                greenSeekBar.getProgress(),
                blueSeekBar.getProgress()
        ));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
};

最佳答案

首先,这个 alphaSeekBar.setProgress(Color.alpha(color)); 是错误的。 setProgress 的作用是设置 int 值来显示搜索栏的进度。

如果您的seekBar Max 设置为100 (alphaSeekBar.setMax(100);),则此alphaSeekBar.setProgress(50); 表示seekBar 已进度一半。

其次,要设置颜色,请使用:

alphaSeekBar.setProgressTintList(Color.alpha(color));
alphaSeekBar.setThumbTintList(Color.alpha(color));

或者,使用这个:

alphaSeekBar.getProgressDrawable().setColorFilter(Color.alpha(color), PorterDuff.Mode.MULTIPLY);

尝试不同的色调模式来代替 PorterDuff.Mode.MULTIPLY 以获得所需的结果

祝你好运

编辑:

用于设置警报对话框的自定义 View

currentAlertDialog = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
View view = getLayoutInflater().inflate(R.layout.color_dialog, null);
currentAlertDialog.setView(view);

然后您可以获得如下所示的那些搜索栏引用:

alphaSeekBar = currentAlertDialog.findViewById(R.id.alphaSeekBar);
redSeekBar = currentAlertDialog.findViewById(R.id.redSeekBar);
greenSeekBar = currentAlertDialog.findViewById(R.id.greenSeekBar);
blueSeekBar = currentAlertDialog.findViewById(R.id.blueSeekBar);
colorView = currentAlertDialog.findViewById(R.id.colorView);

关于java - 设置颜色搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57998520/

相关文章:

java - 寻找元素

java - 如何设置一个长整数的SeekBar进度? (或替代品)

android - 如何让SeekBar占据父级的全宽

java - 在java中将日期转换为时间戳

java - Android Studio 倒数计时器

android - 如何使 GEO Point Class Parcelable 或 Serializable 因为我想使用 Intent 传递它们

java - 在 Android Studio 中使用 OpenCV native 代码

java - 添加图像时不会调用 Firebase 存储 onSuccess

java - 设置onSeekBarChangeListener导致空对象异常

java - 使用 Java 并发读取大文件 (2GB) 并写入另一个文件