2016-08-03 54 views
10

我已将ratingBar颜色设置为与Android的默认蓝色和灰色不同的颜色 - 如果选中它们,我使用黑色背景和粉色制作了我的星星。Android RatingBar:无法填充部分ratingBar如果我定制它的颜色

这是我为我的mainactivity代码:

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自行设置正确,只显示第四颗星的一部分,因为传递给它的值为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图书馆显示了什么,我想实现一些希望,但是我需要一个风格,其中星星背景在屏幕上出现时为白色,当用户按下星号时,它将以纯色填充。就我而言,我希望它充满绿色。

以下是我所能达到的,但还没有完成。我改变了滚动视图的背景,其中的星星坐着粉红色,以便你可以看到,而不是星星的白色背景,来自scrollview背景的粉红色闪耀。对于我的使用案例,我希望白色星星坐在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

+0

我在库中添加了一些功能,可以让你完成你想要的功能(查看我添加到答案中的示例布局)。 – FlyingPumba

回答

4

你在正确的跟踪你正在尝试的东西。你做错了唯一的事情就是把错误的颜色分配给第二个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

+2

你是对的。谢谢! – Simon

-1

你只需要.mutate()更新DrawableCompat.wrap(..)绘制。你的代码是如下样子:

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)); 

如果颜色变为dynammicaly提拉必须可变

见问题:Android: Tint using DrawableCompat

和答案: https://stackoverflow.com/a/30928051/2949612

您可以看到Drawable mutations博客由罗曼盖伊和也见mutate()方法Android开发人员文档。

我希望它能帮助你。

+0

请在发布答案之前用我的回购测试此问题 - 我刚刚尝试使用mutate()进行回购,但它不起作用。 – Simon

+1

repo更新为mutate,因此您可以下载并自行测试。 – Simon

+0

好的。我将测试 – pRaNaY

-1

使用股票RatingBar可以很痛苦,你应该尝试这个库: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" 
    /> 

我希望它是有用的。

+0

非常令人印象深刻的库,并且您将这一切都用视图类从android扩展到ratingbar类。我对你的图书馆有疑问,我会将其发布在我的问题中,你可以在那里回答。 – Simon

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(); 
    } 
} 

然后尝试一下本作smallstar的评价吧:

<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 /样式。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> 
+0

这看起来不像指标评级栏? – Simon

+0

addListenerOnRatingBar()在这个方法中,我实现了“setOnRatingBarChangeListener”我也实现了我的应用程序的评分栏 – YUVRAJ

+0

你可以告诉我什么是你的风格/ foodratingbar吗? – Simon