Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 姜饼设备上的自定义分级条_Android_Ratingbar - Fatal编程技术网

Android 姜饼设备上的自定义分级条

Android 姜饼设备上的自定义分级条,android,ratingbar,Android,Ratingbar,我已经创建了一个自定义的分级条,它可以有5种不同的颜色,并且有一个矩形条,上面有星星下面的点。下面是它的样子: 我在几个地方使用这个视图,在安卓4.0+上一切正常。但是关于姜饼,我有一些问题。下面是view类的代码: public class KrinsenRating extends RelativeLayout { private TextView mRanking; private RatingBar mStars; public KrinsenRating(Context cont

我已经创建了一个自定义的分级条,它可以有5种不同的颜色,并且有一个矩形条,上面有星星下面的点。下面是它的样子:

我在几个地方使用这个视图,在安卓4.0+上一切正常。但是关于姜饼,我有一些问题。下面是view类的代码:

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(新位图[]{bmp_空,bmp_半,bmp_满])但我不得不这样做,因为在
setProgressDrawable()
调用之后呈现星形时出现问题(只显示一个拉伸星形)。下面是方法
构建BarDrawables

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;
}

更简单的方法是将您的评级可绘制项定义到可绘制的图层列表中,然后将其分配给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>

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

然后在任何比率栏视图上:

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


当星星消失时,您是否使用层次结构查看器查看合成视图以查看它们的状态(如果大小合适)?您是否尝试过不同屏幕大小的ginger bread设备?可能是屏幕大小问题?
<RatingBar
        android:id="@+id/ratingbar"
        android:progressDrawable="@drawable/myCustomRatingBarDrawable"
        android:isIndicator="true"
        android:max="5"
        android:numStars="5"
        ...
/>