3

Ratingbar の progressDrawable として ShapeDrawable を設定できないようです。私は次のことを試みましたが失敗しました:

<RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:numStars="5"
android:stepSize="1.0"
android:rating="3.0"
style="@style/myRatingBar"
/>

myRatingbar スタイル:

<style name="myRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/ratingbar_full</item>
<item name="android:indeterminateDrawable">@drawable/ratingbar_full</item>
<item name="android:minHeight">48dip</item>
<item name="android:maxHeight">48dip</item>
<item name="android:scaleType">center</item>
</style>

ratingbar_full.xml:

    <shape 
       android:shape="ring"
       android:innerRadius="10dip"
       android:thickness="1dip">
       <solid android:color="@color/red" />
       <size
           android:width="48dip"
           android:height="48dip"
       />
    </shape>

画面に何も表示されません。

編集:この行では、形状の代わりに .png を使用します。

@drawable/ratingbar_full

4

4 に答える 4

8

Shape XML Drawables + RatingBar は完全に混乱しています。

これは、まったく新しいクラスを作成せずにソリューションに到達できる限り近いものです。

ProgressBar私の拡張クラスは、必要に応じてクランプするためにプログレス ドローアブルを正しく構築します。

空の状態と完全な状態を、事前に入力した状態に置き換えます。現時点ではあまり柔軟ではありませんが、空の/完全な星の状態の設定を簡単に抽象化できます。

/**
 * Created by chris on 28/08/2014.
 * For Yoyo-Android.
 */
public class ShapeDrawableRatingBar extends RatingBar {


    /**
     * TileBitmap to base the width off of.
     */
    @Nullable
    private Bitmap mSampleTile;

    public ShapeDrawableRatingBar(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        setProgressDrawable(createProgressDrawable());
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (mSampleTile != null) {
            final int width = mSampleTile.getWidth() * getNumStars();
            setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, 0),
                    getMeasuredHeight());
        }
    }

    protected LayerDrawable createProgressDrawable() {
        final Drawable backgroundDrawable = createBackgroundDrawableShape();
        LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{
                backgroundDrawable,
                backgroundDrawable,
                createProgressDrawableShape()
        });
        layerDrawable.setId(0, android.R.id.background);
        layerDrawable.setId(1, android.R.id.secondaryProgress);
        layerDrawable.setId(2, android.R.id.progress);
        return layerDrawable;
    }

    protected Drawable createBackgroundDrawableShape() {
        final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stamp_circle_empty));
        if (mSampleTile == null) {
            mSampleTile = tileBitmap;
        }
        final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
        final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
        shapeDrawable.getPaint().setShader(bitmapShader);
        return shapeDrawable;
    }

    protected Drawable createProgressDrawableShape() {
        final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stamp_circle_full));
        final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
        final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
        shapeDrawable.getPaint().setShader(bitmapShader);
        return new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
    }

    Shape getDrawableShape() {
        final float[] roundedCorners = new float[]{5, 5, 5, 5, 5, 5, 5, 5};
        return new RoundRectShape(roundedCorners, null, null);
    }

    public static Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        int width = drawable.getIntrinsicWidth();
        width = width > 0 ? width : 1;
        int height = drawable.getIntrinsicHeight();
        height = height > 0 ? height : 1;

        final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }

} 

コールを呼び出すsetMax setMaxStarsrequestLayout、幅が正しく測定されます。を解決する必要はありません。android:minWidth設定するだけですandroid:layout_width="wrap_content"

ShapeDrawables端から端まで繰り返されるため、少しパディングを追加する必要があることを覚えておいてください.

于 2014-08-29T10:09:15.813 に答える
6

@ChrisJenkins メソッドをカスタマイズしたので、RatingBar アイコンのサイズと評価画像のカスタム色合いを設定できます。RatingBar にパディングを設定するか、カスタムの幅と高さを設定できます。@ChrisJenkins ソリューションは、完全にカスタマイズ可能な RatingBar を最終的に思いつくのに大いに役立ちました。XML でパラメーターを設定することもできます。

public class DrawableRatingBar extends RatingBar
{
// TileBitmap to base the width and hight off of.
@Nullable
private Bitmap iconTile;
private float scaleIconFactor;
private @ColorInt int iconBackgroundColor;
private @ColorInt int iconForegroundColor;
private @DrawableRes int iconDrawable;

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

public DrawableRatingBar(Context context, AttributeSet attrs)
{
    super(context, attrs);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DrawableRatingBarItem);
    scaleIconFactor = a.getFloat(R.styleable.DrawableRatingBarItem_scaleIconFactor, 1);
    iconBackgroundColor = a.getColor(R.styleable.DrawableRatingBarItem_iconBackgroundColor, Color.BLACK);
    iconForegroundColor = a.getColor(R.styleable.DrawableRatingBarItem_iconForegroundColor, Color.WHITE);
    iconDrawable = a.getResourceId(R.styleable.DrawableRatingBarItem_iconDrawable, -1);
    a.recycle();

    init();
}

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

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public DrawableRatingBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
    super(context, attrs, defStyleAttr, defStyleRes);
    init();
}

private void init()
{
    setProgressDrawable(createProgressDrawable());
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    if (iconTile != null)
    {
        final int width = iconTile.getWidth() * getNumStars();
        final int height = iconTile.getHeight();
        setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, 0),
                             resolveSizeAndState(height, heightMeasureSpec, 0));
    }
}

protected LayerDrawable createProgressDrawable()
{
    final Drawable backgroundDrawable = createBackgroundDrawableShape();
    LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] {backgroundDrawable,
                                                                    backgroundDrawable,
                                                                    createProgressDrawableShape()});
    layerDrawable.setId(0, android.R.id.background);
    layerDrawable.setId(1, android.R.id.secondaryProgress);
    layerDrawable.setId(2, android.R.id.progress);
    return layerDrawable;
}

protected Drawable createBackgroundDrawableShape()
{
    Drawable drawable = ContextCompat.getDrawable(getContext(), iconDrawable);

    final Bitmap tileBitmap = scaleImageWithColor(drawable, scaleIconFactor, iconBackgroundColor);
    if (iconTile == null)
    {
        iconTile = tileBitmap;
    }
    final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
    final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
    shapeDrawable.getPaint().setShader(bitmapShader);

    return shapeDrawable;
}


private void setRatingStarColor(Drawable drawable, @ColorInt int color)
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
        DrawableCompat.setTint(drawable, color);
    }
    else
    {
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
    }
}

protected Drawable createProgressDrawableShape()
{
    Drawable drawable = ContextCompat.getDrawable(getContext(), iconDrawable);

    final Bitmap tileBitmap = scaleImageWithColor(drawable, scaleIconFactor, iconForegroundColor);
    final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
    final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
    shapeDrawable.getPaint().setShader(bitmapShader);
    return new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
}

Shape getDrawableShape()
{
    return new RectShape();
}

public Bitmap scaleImageWithColor(Drawable drawable, float scaleFactor, @ColorInt int color)
{
    Bitmap b = ((BitmapDrawable) drawable).getBitmap();

    int sizeX = Math.round(drawable.getIntrinsicWidth() * scaleFactor);
    int sizeY = Math.round(drawable.getIntrinsicHeight() * scaleFactor);

    Bitmap bitmapResized = Bitmap.createScaledBitmap(b, sizeX, sizeY, true);

    Canvas canvas = new Canvas(bitmapResized);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    paint.setAntiAlias(true);
    ColorFilter filter = new LightingColorFilter(color, 1);
    paint.setColorFilter(filter);
    canvas.drawBitmap(bitmapResized, 0, 0, paint);

    return bitmapResized;

}

protected Bitmap fromDrawable(final Drawable drawable, final int height, final int width) {
    final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

public float getScaleIconFactor() {
    return scaleIconFactor;
}

public void setScaleIconFactor(float scaleIconFactor) {
    this.scaleIconFactor = scaleIconFactor;
}

public int getIconForegroundColor() {
    return iconForegroundColor;
}

public void setIconForegroundColor(int iconForegroundColor) {
    this.iconForegroundColor = iconForegroundColor;
}

public int getIconBackgroundColor() {
    return iconBackgroundColor;
}

public void setIconBackgroundColor(int iconBackgroundColor) {
    this.iconBackgroundColor = iconBackgroundColor;
}

public int getIconDrawable() {
    return iconDrawable;
}

public void setIconDrawable(int iconDrawable) {
    this.iconDrawable = iconDrawable;
}

}

カスタム属性:

<declare-styleable name="DrawableRatingBarItem">
    <attr name="scaleIconFactor" format="float"/>
    <attr name="iconBackgroundColor" format="color"/>
    <attr name="iconForegroundColor" format="color"/>
    <attr name="iconDrawable" format="reference"/>
</declare-styleable>
于 2016-05-26T12:19:31.310 に答える
0

ここで ステップごとの評価バーのスタイリングを見つけることができます。形状ではなく画像に
設定<item name="android:progressDrawable">してみてください。機能する場合は、形状が問題を引き起こしていることを意味します

于 2013-10-10T12:01:34.417 に答える