2013-08-30 50 views
5

我創建了一個自定義評分欄,它可以有5種不同的顏色,並且具有矩形條,點下面有星星。這裏是什麼樣子:薑餅設備上的自定義評分欄

enter image description here

我使用的幾個地方這種觀點,一切都在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和更新,但在薑餅我有一些奇怪的情況。首先有時在薑餅上一切都很好。但往往是沒有明星。帶點的矩形條總是正確的,但星星有時會消失。下面是示例屏幕從ListView其使用的評價欄的每一個行:

enter image description here

就像你所看到的,有時候是好的,有時不:/我有我的導航slidingMenu這個等級吧當我緩慢地滑動時會顯示星星,但在滑動結束後(當onOpened應該調用時),星星會消失。

有你,讓我與快樂任何thougths :)

我添加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> 
+1

當星星消失了,沒有你看用層次結構查看器在組成視圖中查看其狀態(如果它們具有適當的大小)? – Luksprog

+0

您是否嘗試過不同屏幕尺寸的姜麪包設備,可能是屏幕尺寸問題? – user1634451

回答

2

這是定義你的等級可繪製成層的更簡單的方法列表可繪製,然後將其分配給您的RatingBar類。它適用於Android薑餅和我之前。

<?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文件到文件夾可繪,例如,myCustomRatingBarDrawable.xml

然後在任意的RatingBar觀點:

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