Android RatingBar: getProgressDrawable doesn't cast to right Class

久未见 提交于 2019-12-01 20:48:57

What happens is clearly explained in the error message :

TintDrawableWrapper cannot be cast to android.graphics.drawable.LayerDrawable

The support lib creates a wrapper in order to handle retro compatibility, you just need to take it into account. Have a look at the lib implementation, it probably wraps around a LayerDrawable.

You will need to do something along the lines of :

LayerDrawable stars = getProgressDrawable(rating_bar);
}

private LayerDrawable getProgressDrawable(RatingBar ratingBar) {
  if (Build.VERSION >= LOLLIPOP) {
    return (LayerDrawable) ratingBar.getProgressDrawable();
  } else {
    // fetch the LayerDrawable based on Google's support implementation,  
    // probably a simple findViewById()
  }
}

LayerDrawable is used in API v21.

Creating a subclass of RatingDialog and using that instead ofandroid.widget.RatingBar worked for me. Here is the code I used.

Subclass:

public class RatingBar extends android.widget.RatingBar {
public RatingBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public RatingBar(Context context, AttributeSet attrs) {
    super(context, attrs);
}
}

Then in my layouts:

 <your.package.RatingBar
    android:id="@+id/rating_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    />

Then in the classes where you are casting the class, just use your.package.RatingBar instead of android.widget.RatingBar

Came accross the same issue today when creating a RatingBar programmatically. The following workaround is working for me also on Android 4.x devices using the AppCompatRatingBar:

AppCompatRatingBar rb = new AppCompatRatingBar(activity);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
rb.setLayoutParams(layoutParams);
rb.setStepSize(1.0f);
rb.setMax(5);

LayerDrawable layerDrawable = null;
if (rb.getProgressDrawable() instanceof LayerDrawable) {
    layerDrawable = (LayerDrawable) rb.getProgressDrawable();
} else if (rb.getProgressDrawable() instanceof DrawableWrapper) {
    DrawableWrapper wrapper = (DrawableWrapper) rb.getProgressDrawable();
    if (wrapper.getWrappedDrawable() instanceof LayerDrawable) {
        layerDrawable = (LayerDrawable) wrapper.getWrappedDrawable();
    }
}

if (layerDrawable != null) {
    layerDrawable.getDrawable(2).setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
    layerDrawable.getDrawable(0).setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
    layerDrawable.getDrawable(1).setColorFilter(ContextCompat.getColor(activity, R.color.primary), PorterDuff.Mode.SRC_ATOP);
}

Please try this trick, It will work fine by adding getCurrent() method to return a drawable.

RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);

LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable().getCurrent();

stars.getDrawable(2).setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP); stars.getDrawable(0).setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP); stars.getDrawable(1).setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!