package com.willy.ratingbar;

import android.content.Context;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/**
 * Created by willy on 2017/6/3.
 */

public class PartialView extends RelativeLayout {

    private ImageView mFilledView;
    private ImageView mEmptyView;

    public PartialView(Context context) {
        super(context);
        init();
    }

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

    public PartialView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mFilledView = new ImageView(getContext());
        mFilledView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        mEmptyView = new ImageView(getContext());
        mEmptyView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        addView(mFilledView);
        addView(mEmptyView);
    }

    public void setFilledDrawable(Drawable drawable) {
        ClipDrawable clipDrawable = new ClipDrawable(drawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
        mFilledView.setImageDrawable(clipDrawable);
    }

    public void setEmptyDrawable(Drawable drawable) {
        ClipDrawable clipDrawable = new ClipDrawable(drawable, Gravity.RIGHT, ClipDrawable.HORIZONTAL);
        mEmptyView.setImageDrawable(clipDrawable);
    }

    public void setFilled() {
        mFilledView.setImageLevel(10000);
        mEmptyView.setImageLevel(0);
    }

    public void setPartialFilled(float rating) {
        float percentage = rating % 1;
        int level = (int) (10000 * percentage);
        level = level == 0 ? 10000 : level;
        mFilledView.setImageLevel(level);
        mEmptyView.setImageLevel(10000 - level);
    }

    public void setEmpty() {
        mFilledView.setImageLevel(0);
        mEmptyView.setImageLevel(10000);
    }

}

@@ -1169,32 +1169,114 @@
 ble drawable) %7B%0A
+        if (drawable.getConstantState() == null) %7B%0A            return;%0A        %7D%0A%0A
         ClipDraw
@@ -1312,32 +1312,65 @@
 rawable(drawable
+.getConstantState().newDrawable()
 , Gravity.LEFT, 
@@ -1367,11 +1367,12 @@
 ity.
-LEF
+STAR
 T, C
@@ -1501,32 +1501,114 @@
 ble drawable) %7B%0A
+        if (drawable.getConstantState() == null) %7B%0A            return;%0A        %7D%0A%0A
         ClipDraw
@@ -1652,16 +1652,49 @@
 drawable
+.getConstantState().newDrawable()
 , Gravit
@@ -1699,13 +1699,11 @@
 ity.
-RIGHT
+END
 , Cl

Fix stars disappear bug (#14, #15, #17, #18, #20)