【问题标题】:Custom ratingbar on gingerbread devices姜饼设备上的自定义评分栏
【发布时间】:2013-10-02 20:37:32
【问题描述】:

我创建了一个自定义 RatingBar,它可以有 5 种不同的颜色,并且有一个矩形条,点在星号下方。这是它的样子:

我在几个地方使用了这个视图,并且在 Android 4.0+ 上一切正常。但是在姜饼上我有一些问题。这是视图类的代码:

public class KrinsenRating extends RelativeLayout {

private TextView mRanking;
private RatingBar mStars;

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

    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layoutInflater.inflate(R.layout.krinsen_rating, this);

    loadViews();
}

public KrinsenRating(Context context) {
    super(context);

    loadViews();
}

private void loadViews() {
    mRanking = (TextView) findViewById(R.id.ranking_bar_position);
    mStars = (RatingBar) findViewById(R.id.ranking_bar);
}

public void setRating(long rating){
    RatingConverter rc = new RatingConverter(rating);

    Bitmap bmp_empty = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_empty)).getBitmap();
    Bitmap bmp_half = null;
    Bitmap bmp_full = null;

    switch (rc.getColor()) {
    case 1:
        bmp_half = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_half1)).getBitmap();
        bmp_full = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_full1)).getBitmap();

        mRanking.setBackgroundColor(getResources().getColor(R.color.ranking1));
        break;
    case 2:
        bmp_half = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_half2)).getBitmap();
        bmp_full = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_full2)).getBitmap();

        mRanking.setBackgroundColor(getResources().getColor(R.color.ranking2));
        break;
    case 3:
        bmp_half = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_half3)).getBitmap();
        bmp_full = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_full3)).getBitmap();

        mRanking.setBackgroundColor(getResources().getColor(R.color.ranking3));
        break;
    case 4:
        bmp_half = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_half4)).getBitmap();
        bmp_full = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_full4)).getBitmap();

        mRanking.setBackgroundColor(getResources().getColor(R.color.ranking4));
        break;
    case 5:
        bmp_half = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_half5)).getBitmap();
        bmp_full = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_full5)).getBitmap();

        mRanking.setBackgroundColor(getResources().getColor(R.color.ranking5));
        break;
    }

    mStars.setProgressDrawable(GUI.buildRatingBarDrawables(new Bitmap[]{bmp_empty, bmp_half, bmp_full}));
    mStars.setRating(rc.getRealRating());
    mRanking.setText(rating + "");
}
}

这条线令人困惑:mStars.setProgressDrawable(GUI.buildRatingBarDrawables(new Bitmap[]{bmp_empty, bmp_half, bmp_full})); 但我不得不这样做,因为在setProgressDrawable() 调用后渲染星星出现问题(只显示一个拉伸的星星)。这是方法buildRatingBarDrawables

public static Drawable buildRatingBarDrawables(Bitmap[] images) {
    final int[] requiredIds = { android.R.id.background,
            android.R.id.secondaryProgress, android.R.id.progress };
    final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
    Drawable[] pieces = new Drawable[3];
    for (int i = 0; i < 3; i++) {
        ShapeDrawable sd = new ShapeDrawable(new RoundRectShape(
                roundedCorners, null, null));
        BitmapShader bitmapShader = new BitmapShader(images[i],
                Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
        sd.getPaint().setShader(bitmapShader);
        ClipDrawable cd = new ClipDrawable(sd, Gravity.LEFT,
                ClipDrawable.HORIZONTAL);
        if (i == 0) {
            pieces[i] = sd;
        } else {
            pieces[i] = cd;
        }
    }
    LayerDrawable ld = new LayerDrawable(pieces);
    for (int i = 0; i < 3; i++) {
        ld.setId(i, requiredIds[i]);
    }
    return ld;
}

所以,在 Android ICS 和更新版本上一切正常,但在 Gingerbread 上我遇到了一些奇怪的情况。首先,有时在姜饼上一切都很好。但往往是根本没有星星。带点的矩形条总是正确的,但星星有时会消失。以下是来自ListView 的示例屏幕,它在每一行中都使用了评分栏:

如您所见,有时还可以,有时不行:/我的导航滑动菜单上有这个评分栏,当我慢慢滑动时会显示星星,但在滑动结束后(应该调用 onOpened 时)星星都消失了。

你有什么让我更开心的想法吗?:)

我为评分布局添加了 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/avatar_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >

<RatingBar
    android:id="@+id/ranking_bar"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:layout_centerHorizontal="true"
    android:clickable="false"
    android:focusable="false"
    android:isIndicator="true"
    android:numStars="3"
    android:progressDrawable="@drawable/krinsen_rating"
    android:stepSize="0.1" />

<TextView
    android:id="@+id/ranking_bar_position"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/ranking_bar"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/register_avatar"
    android:paddingBottom="2dp"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="2dp"
    android:textColor="@android:color/white"
    android:textSize="14sp"
    android:textStyle="bold" />
    </RelativeLayout>

【问题讨论】:

  • 星星消失后,您是否使用层次结构查看器查看组合视图以查看它们的状态(如果它们具有适当的大小)?
  • 你试过不同屏幕尺寸的姜饼设备可能是屏幕尺寸问题吗?

标签: android ratingbar


【解决方案1】:

将您的评分可绘制对象定义为可绘制图层列表,然后将其分配给您的 RatingBar 类,这将是一种更简单的方法。它适用于我的安卓姜饼和之前的版本。

<?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/star_empty" />
    <item android:id="@+android:id/secondaryProgress"
          android:drawable="@drawable/star_empty" />
    <item android:id="@+android:id/progress"
          android:drawable="@drawable/star_full" />
</layer-list>

将其保存到 xml 文件到 drawables 文件夹中,例如,myCustomRatingBarDrawable.xml

然后在任何 RatingBar 视图上:

<RatingBar
        android:id="@+id/ratingbar"
        android:progressDrawable="@drawable/myCustomRatingBarDrawable"
        android:isIndicator="true"
        android:max="5"
        android:numStars="5"
        ...
/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多