Android RatingBar:如果我自定义它的颜色

时间:2016-08-03 13:39:19

标签: android ratingbar

我已将ratingBar颜色设置为与Android的默认蓝色和灰色不同的颜色 - 如果选中它们,我将使用黑色背景制作我的星星并使用粉红色。

这是我的主要活动代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingbar);
        LayerDrawable layerDrawable2 = (LayerDrawable) ratingBar.getProgressDrawable();
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(0)),
                ContextCompat.getColor(getApplicationContext(), android.R.color.background_dark));
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(1)),
                ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)); // Partial star
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(2)),
                ContextCompat.getColor(getApplicationContext(), R.color.colorAccent));
        ratingBar.setIsIndicator(false);
        ratingBar.setRating(3.6f);
        ratingBar.setStepSize(0.1f);
        ratingBar.invalidate();
        ratingBar.setIsIndicator(true);
    }
}

您可以看到,如果我将评级设为3.6,则最多可选择4。

enter image description here

但是,如果我注释掉我自己的自定义颜色,则ratingBar会正确设置以仅显示填充的第4个星的一部分,因为传递给它的值为3.6:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingbar);
/*        LayerDrawable layerDrawable2 = (LayerDrawable) ratingBar.getProgressDrawable();
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(0)),
                ContextCompat.getColor(getApplicationContext(), android.R.color.background_dark));
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(1)),
                ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)); // Partial star
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(2)),
                ContextCompat.getColor(getApplicationContext(), R.color.colorAccent));*/
        ratingBar.setIsIndicator(false);
        ratingBar.setRating(3.6f);
        ratingBar.setStepSize(0.1f);
        ratingBar.invalidate();
        ratingBar.setIsIndicator(true);
    }
}

enter image description here

为什么如果我创建自己的自定义颜色,ratingBar不能正确填充?

我创建了一个小型仓库,您可以下载并在此处进行测试:https://github.com/Winghin2517/RatingBarTest2

编辑:

FlyingPumba的图书馆显示了我想要实现的一些承诺,但我需要一种风格,当它出现在屏幕上时,星星背景为白色,当用户按下星形时,它将被纯色填充。就我而言,我希望它充满绿色。

以下是我能够实现的目标,但目前尚不完善。我改变了滚动视图背景,其中星星坐在粉红色,这样你就可以看到,而不是星星的白色背景,滚动视图背景中的粉红色闪耀。对于我的用例,我希望白色星星坐在imageview前面,我不希望imageview中的图像在星空中闪耀。

此外,我根本不需要白色笔画,因为我想保持星星的造型简单。

<com.iarcuschin.simpleratingbar.SimpleRatingBar
    android:id="@+id/ratingBar3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:srb_rating="3.6"
    app:srb_fillColor="@color/material_greenA700"
    app:srb_borderColor="@color/material_white"
    app:srb_starBorderWidth="0"
    app:srb_pressedFillColor="@color/material_greenA700"
    app:srb_starSize="30dp"
    />

enter image description here

4 个答案:

答案 0 :(得分:4)

你正走在正确的轨道上。你唯一做错的就是为第二个drawable分配错误的颜色。

将第二个drawable想象为星星的背景,而不是前景。所以部分星星需要背景颜色,而不是星星的颜色,例如在你的例子中,黑色,而不是粉红色。

以下是更正后的代码,其工作原理如上所述。我稍微重新格式化了以突出显示所需的更改。

    RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingbar);
    LayerDrawable layerDrawable2 = (LayerDrawable) ratingBar.getProgressDrawable();

    // No star
    DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(0)),
            ContextCompat.getColor(getApplicationContext(),
                    android.R.color.background_dark));

    // Partial star 
    DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(1)),
            ContextCompat.getColor(getApplicationContext(),
                    // use background_dark instead of colorAccent
                    // R.color.colorAccent));
                    android.R.color.background_dark));

    // Custom star
    DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(2)),
            ContextCompat.getColor(getApplicationContext(),
                    R.color.colorAccent));

另一种方法是在RatingBar上使用适当的样式,并且在许多情况下可能更好。

这需要为评级栏设置自定义样式,然后定义该样式使用的图层可绘制对象以指向您自己的图像或颜色。

我不打算进一步详细说明,因为the actual xml code is shown in Anantham's answer

答案 1 :(得分:1)

在Activity.java中:

oncreate:

       RatingBar   setRatingBar = (RatingBar) findViewById(R.id.setRating);
       float rating = Float.parseFloat(listString);
      // onRatingChanged(setRatingBar, 0.0f, true);

        public void onRatingChanged(RatingBar rateBar, float rating, boolean fromUser) {
    try {
        DecimalFormat decimalFormat = new DecimalFormat("#.#");
        curRate = Float.valueOf(decimalFormat.format((curRate * count + rating)/ ++count));
        float userRatePoint = curRate/**20*/;

        setRatingBar.setRating(userRatePoint);
        countText = (TextView) findViewById(R.id.countText);
        countText.setText(userRatePoint + " Ratings");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

然后尝试使用小星评级栏:

    <RatingBar
     android:id="@+id/setRating"
     style="?android:attr/ratingBarStyleSmall"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginLeft="10dp"
     android:layout_marginTop="3dp"
     android:isIndicator="true"
     android:numStars="5"
     android:stepSize="0.1" />
       //if you want to style please use "foodRatingBar"
       //for custom star image use  "ratingstar"

style.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/ratingstar</item>
    <item name="android:minHeight">22dip</item>
    <item name="android:maxHeight">22dip</item>
</style>

值v11 / styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/ratingstar</item>
    <item name="android:minHeight">22dip</item>
    <item name="android:maxHeight">22dip</item>
</style>

值v14 / styles.xml

 <resources>
   <style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/ratingstar</item>
    <item name="android:minHeight">22dip</item>
    <item name="android:maxHeight">22dip</item>
</style>
<!--
    Base application theme for API 14+. This theme completely replaces
    AppBaseTheme from BOTH res/values/styles.xml and
    res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!-- API 14 theme customizations can go here. -->
</style>
</resources>

ratingstar.xml - 这是定义自定义进度图像的位置

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@android:id/background"
            android:drawable="@drawable/ratingstar_default" />
        <item android:id="@android:id/secondaryProgress"
            android:drawable="@drawable/ratingstar_default" />
        <item android:id="@android:id/progress"
            android:drawable="@drawable/ratingstar_selected" />
    </layer-list>
</resources>

答案 2 :(得分:-1)

您只需要使用DrawableCompat.wrap(..)更新.mutate() drawable。您的代码如下所示:

DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(0).mutate()),
            ContextCompat.getColor(getApplicationContext(), android.R.color.background_dark));
    DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(1).mutate()),
            ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)); // Partial star
    DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(2).mutate()),
            ContextCompat.getColor(getApplicationContext(), R.color.colorAccent));
  

如果动态改变颜色,则drawable必须可变

参见问题:Android: Tint using DrawableCompat

并回答: https://stackoverflow.com/a/30928051/2949612

您可以查看Romain Guy的Drawable mutations博客,另请参阅mutate()方法android开发者文档。

我希望它会帮助你。

答案 3 :(得分:-1)

使用股票评级栏可能非常痛苦,您应该尝试使用此库:SimpleRatingBar

(我有点偏颇,因为我是创作者)

它的特点:

  • 完全正常工作android:layout_width:可以将其设置为wrap_contentmatch_parent或abritary dp。
  • 任意数量的明星。
  • 任意步长。
  • 可以精确控制星星的大小,也可以通过设置最大大小来控制。
  • 正常状态下的可自定义颜色(星星的边框,填充和背景)。
  • 按下状态下的可自定义颜色(星星的边框,填充和背景)。
  • 明星之间可定制的尺寸分离。
  • 可自定义的星星边框宽度。
  • 可自定义的星角半径。
  • 允许设置OnRatingBarChangeListener
  • 星星填充可以设置为从左到右或从右到左(RTL语言支持)。
  • 在视图中集成了AnimationBuilder,以动画方式设置评级。

Here is a preview of it

您可以在jcenterMaven Central中找到它。因此,在build.gradle文件中,只需添加依赖项:

compile 'com.iarcuschin:simpleratingbar:0.1.+'

修改 我最近添加了更改星星背景颜色(空白颜色)和禁用边框的可能性。 要实现您想要的效果,您可以添加到您的布局中:

<com.iarcuschin.simpleratingbar.SimpleRatingBar
    android:id="@+id/ratingBar3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:srb_rating="3.6"
    app:srb_starBackgroundColor="@color/white"
    app:srb_fillColor="@color/material_greenA700"
    app:srb_borderColor="@color/material_white"
    app:srb_pressedFillColor="@color/material_greenA700"
    app:srb_starSize="30dp"
    app:srb_drawBorderEnabled="false"
    />

我希望它有用。