为什么颜色的阴影会改变?

时间:2018-04-15 12:39:24

标签: java android colors

我有一些按钮,我想根据最后按下哪一个来改变这些按钮的背景。唯一的问题是颜色变化,我不知道为什么。

更改背景的代码

public static void swap(View on, List<View> all, int defaultColor) {
        for (View v : all) {
            Drawable backgroundOff = v.getBackground();
            backgroundOff.setTint(defaultColor);
            v.setBackground(backgroundOff);
        }
        Drawable backgroundOn = on.getBackground();
        backgroundOn.setTint(manipulateColor(defaultColor, 0.8f));
        on.setBackground(backgroundOn);
    }

使默认颜色变暗的代码

public static int manipulateColor(int color, float factor) {
    int a = Color.alpha(color);
    int r = Math.round(Color.red(color) * factor);
    int g = Math.round(Color.green(color) * factor);
    int b = Math.round(Color.blue(color) * factor);
    return Color.argb(a,
            Math.min(r,255),
            Math.min(g,255),
            Math.min(b,255));
}

按钮代码

final View btnTrue = (findViewById(R.id.tofTrueInput));
final View btnFalse = (findViewById(R.id.tofFalseInput));
final int defColor = getResources().getColor(R.color.colorAccent);
final List<View> btns = new ArrayList<>();
btns.add(btnTrue);
btns.add(btnFalse);
btnTrue.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        userAnswer = true;
        ExtensionMethods.swap(btnTrue, btns, defColor);
    }
});
btnFalse.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        userAnswer = false;
        ExtensionMethods.swap(btnFalse, btns, defColor);
    }
});

颜色

默认颜色:defaultColor
更改的默认颜色:changedDefaultColor

它的黑暗版本同样发生了变化。
但为什么?

1 个答案:

答案 0 :(得分:0)

我不知道为什么,但它正在发挥作用。我刚刚将这一行添加到foreach循环中:

<?php
    require('file.php');

    $ReviewID = $_POST['reviewiduser'];

    $ReviewID = explode(",",$ReviewID);

    $results = array();

    foreach($ReviewID as $ReviewID) {
        $sql2 = "SELECT * FROM review WHERE review_id = ?";
        $stmt2 = $con->prepare($sql2) or die(mysqli_error($con));
        $stmt2->bind_param('i', $ReviewID) or die ("MySQLi-stmt binding failed ".$stmt2->error);
        $stmt2->execute() or die ("MySQLi-stmt execute failed ".$stmt2->error);
        $result2 = $stmt2->get_result();

        while($row = mysqli_fetch_array($result2)) {//make an array called $results
            $results[] = array(
                'category' => $row['cat_name'],
                'name' => $row['name'],
                'phone' => $row['phone'],
                'comment' => $row['comment'],
                'reviewid' => $row['review_id'],
            );
        }
    }

        echo json_encode($results);

?>
backgroundOff.setTintMode(PorterDuff.Mode.DARKEN);