2013-08-30 9 views
5

私は5種類の色を持つことができ、星の下に点を持つ長方形のバーを持つカスタムRatingBarを作成しました。ここではそれがどのように見えるかです:ジンジャーブレッドデバイスのカスタム評価バー

enter image description here

私は場所のカップルで、このビューを使用していますが、すべては、Android 4.0以降でokです。しかし、ジンジャーブレッドに私はいくつかの問題があります。ここでは、ビュークラスのコードは次のとおりです。mStars.setProgressDrawable(GUI.buildRatingBarDrawables(new Bitmap[]{bmp_empty, bmp_half, bmp_full}));が、私は(唯一の延伸スターが表示されている)setProgressDrawable()呼び出し後に星をレンダリングする際に問題があるため、これを持っていた:

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 + ""); 
} 
} 

この行は混乱しています。

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で[OK]をクリックし、新しいですが、ジンジャーブレッドの上に、私はいくつかの奇妙な状況があります。ここでは方法buildRatingBarDrawablesです。すべての最初のジンジャーブレッドで時々すべてはちょうどいいです。しかし、多くの場合、星は全く存在しません。ポイントを持つ長方形の棒は常に正しいですが、星が消えることがあります。

enter image description here

あなたが見ることができるように、時々、okです時にはそれがありません:/私は私のナビゲーションslidingMenuでこの格付けバーを持ってここで、すべて一列に評価バーを使用ListViewからの画面例です私はゆっくりスライドすると星が表示されますが、スライドが終わったら(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クラスにそれを割り当てます。それはアンドロジインジンジャーブレッドと私の前で動作します。

<?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" 
     ... 
/> 
関連する問題