Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

android - Spacing between the stars of a rating bar?

ho can I set a spacing beween the stars? Thats my ratingbar:

ratingBar = (RatingBar) inflater.inflate(R.layout.ratingbar, null);
ratingBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

The layout:

<?xml version="1.0" encoding="utf-8"?>
<RatingBar xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/myStyle" android:layout_alignParentRight="true"
    android:layout_height="19dp" android:layout_width="wrap_content"
    android:numStars="5">
</RatingBar>

The style:

 <style name="myStyle" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/android_r2_ratingstar_yellow</item>
    <item name="android:indeterminateDrawable">@drawable/android_r2_ratingstar_grey</item>
 </style>

Thanks, cheers

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to add the padding to the png itself. While there is something known as an Inset Drawable (http://developer.android.com/guide/topics/resources/drawable-resource.html#Inset) that you could wrap your drawable into, the code for 'tileify'-ing (as referred to in the android source code) doesn't handle the case of drawable being an inset drawable, and hence doesn't tile the image.

Here's the tilefy method (in ProgressBar.java, which is the ancestor to RatingBar.java: https://gist.github.com/CyanogenMod/android_frameworks_base/blob/gingerbread/core/java/android/widget/ProgressBar.java)

private Drawable tileify(Drawable drawable, boolean clip) {

    if (drawable instanceof LayerDrawable) {
        LayerDrawable background = (LayerDrawable) drawable;
        final int N = background.getNumberOfLayers();
        Drawable[] outDrawables = new Drawable[N];

        for (int i = 0; i < N; i++) {
            int id = background.getId(i);
            outDrawables[i] = tileify(background.getDrawable(i),
                    (id == R.id.progress || id == R.id.secondaryProgress));
        }

        LayerDrawable newBg = new LayerDrawable(outDrawables);

        for (int i = 0; i < N; i++) {
            newBg.setId(i, background.getId(i));
        }

        return newBg;

    } else if (drawable instanceof StateListDrawable) {
        StateListDrawable in = (StateListDrawable) drawable;
        StateListDrawable out = new StateListDrawable();
        int numStates = in.getStateCount();
        for (int i = 0; i < numStates; i++) {
            out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
        }
        return out;

    } else if (drawable instanceof BitmapDrawable) {
        final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
        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 (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT,
                ClipDrawable.HORIZONTAL) : shapeDrawable;
    }

    return drawable;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...