RatingBar android - 自定义绘制运行时

时间:2016-04-20 13:46:17

标签: android ratingbar

我在列表中显示了评级栏。

当人们对1至4颗星的物品进行评级时 - 评级栏应该改变星星的颜色或背景或覆盖物或某些东西来表示变化(组织中客户要求使用颜色的颜色)识别状态,例如绿色=好)

我在想,改变星星的颜色会让人满意。但是,大多数解决方案都围绕更改raing栏中使用的默认图形,而不是在用户更改评级后如何进行提款。

1 个答案:

答案 0 :(得分:1)

我想你正在寻找色彩:

ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
        final LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();
        int color;
        switch ((int) rating) {
            case 1:
                color = Color.RED;
                break;
            case 2:
            case 3:
                color = Color.YELLOW;
                break;
            case 4:
            default:
                color = Color.GREEN;
                break;
        }
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), color);
    }
});

它的工作不是很顺利,但它应该是一个重点。

enter image description here

另外,你可以在触摸监听器中设置颜色色调 - 这种方式对你来说会更好,我猜:

ratingBar.setOnTouchListener(new View.OnTouchListener() {
    private int lastColoredProgress = 0;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int progress = ratingBar.getProgress();
        if (progress != lastColoredProgress) {
            lastColoredProgress = progress;
            final LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();
            int color;
            switch (lastColoredProgress) {
                case 0:
                case 1:
                    color = Color.RED;
                    break;
                case 2:
                case 3:
                    color = Color.YELLOW;
                    break;
                case 4:
                default:
                    color = Color.GREEN;
                    break;
            }
            DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), color);
        }
        return false;
    }
});

enter image description here