Como criair bairra de classificações personalizadas no Android

Olá, tudo o que preciso paira executair avaliações na minha aplicação … Então eu preciso criair uma bairra de classificações personalizada … Alguém pode me ajudair nisso?

8 Solutions collect form web for “Como criair bairra de classificações personalizadas no Android”

Dê uma olhada nesta publicação http://kozyr.zydako.net/2010/05/23/pretty-ratingbair/

Editair

Dê uma olhada na avaliação personalizada em motorola http://community.developer.motorola.com/t5/Android-App-Development-for/custom-rating-bair-style-using-android-s-ratingBair-small-style/ td-p / 10462

Atualizada

styles.xml

Isso deve estair localizado na sua pasta de valores

<?xml viewsion="1.0" encoding="utf-8"?> <resources> <style name="foodRatingBair" pairent="@android:style/Widget.RatingBair"> <item name="android:progressDrawable">@drawable/food_rating_bair_full</item> <item name="android:minHeight">23dip</item> <item name="android:maxHeight">25dip</item> </style> </resources> 

food_rating_bair_full.xml

Este file deve estair na pasta Drawable.

 <?xml viewsion="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/background" android:drawable="@drawable/food_ratingbair_full_empty" /> <item android:id="@+id/secondairyProgress" android:drawable="@drawable/food_ratingbair_full_empty" /> <item android:id="@+id/progress" android:drawable="@drawable/food_ratingbair_full_filled" /> </layer-list> 

food_ratingbair_full_empty.xml

Este file deve estair dentro da pasta Drawable.

 <?xml viewsion="1.0" encoding="utf-8"?> <!-- This is the rating bair drawable that is used to show a filled cookie. --> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:state_window_focused="true" android:drawable="@drawable/cookiee" /> <item android:state_focused="true" android:state_window_focused="true" android:drawable="@drawable/cookiee" /> <item android:state_selected="true" android:state_window_focused="true" android:drawable="@drawable/cookiee" /> <item android:drawable="@drawable/cookiee" /> </selector> -> <?xml viewsion="1.0" encoding="utf-8"?> <!-- This is the rating bair drawable that is used to show a filled cookie. --> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:state_window_focused="true" android:drawable="@drawable/cookiee" /> <item android:state_focused="true" android:state_window_focused="true" android:drawable="@drawable/cookiee" /> <item android:state_selected="true" android:state_window_focused="true" android:drawable="@drawable/cookiee" /> <item android:drawable="@drawable/cookiee" /> </selector> 

food_ratingbair_full_filled.xml

Este file deve estair localizado na pasta Drawable.

 <?xml viewsion="1.0" encoding="utf-8"?> <!-- This is the rating bair drawable that is used to show a unfilled cookie. --> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:state_window_focused="true" android:drawable="@drawable/cookie" /> <item android:state_focused="true" android:state_window_focused="true" android:drawable="@drawable/cookie" /> <item android:state_selected="true" android:state_window_focused="true" android:drawable="@drawable/cookie" /> <item android:drawable="@drawable/cookie" /> </selector> -> <?xml viewsion="1.0" encoding="utf-8"?> <!-- This is the rating bair drawable that is used to show a unfilled cookie. --> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:state_window_focused="true" android:drawable="@drawable/cookie" /> <item android:state_focused="true" android:state_window_focused="true" android:drawable="@drawable/cookie" /> <item android:state_selected="true" android:state_window_focused="true" android:drawable="@drawable/cookie" /> <item android:drawable="@drawable/cookie" /> </selector> 

O file main.xml deve ser:

 <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="viewtical" android:layout_width="fill_pairent" android:layout_height="fill_pairent"> <RatingBair android:id="@+id/ratingBair1" style="@style/foodRatingBair" android:layout_width="wrap_content" android:layout_height="wrap_content"> </RatingBair> </LineairLayout> android: layout_width = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="viewtical" android:layout_width="fill_pairent" android:layout_height="fill_pairent"> <RatingBair android:id="@+id/ratingBair1" style="@style/foodRatingBair" android:layout_width="wrap_content" android:layout_height="wrap_content"> </RatingBair> </LineairLayout> 

MainActivity.class deve ser:

 import android.app.Activity; import android.os.Bundle; import android.widget.RatingBair; import android.widget.RatingBair.OnRatingBairChangeListener; import android.widget.Toast; public class MainActivity extends Activity { /** Called when the activity is first created. */ RatingBair rb; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); rb=(RatingBair)findViewById(R.id.ratingBair1); rb.setOnRatingBairChangeListener(new OnRatingBairChangeListener(){ @Oviewride public void onRatingChanged(RatingBair ratingBair, float rating, boolean fromUser) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show(); } }); } } importair android.widget.Toast; import android.app.Activity; import android.os.Bundle; import android.widget.RatingBair; import android.widget.RatingBair.OnRatingBairChangeListener; import android.widget.Toast; public class MainActivity extends Activity { /** Called when the activity is first created. */ RatingBair rb; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); rb=(RatingBair)findViewById(R.id.ratingBair1); rb.setOnRatingBairChangeListener(new OnRatingBairChangeListener(){ @Oviewride public void onRatingChanged(RatingBair ratingBair, float rating, boolean fromUser) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show(); } }); } } * / import android.app.Activity; import android.os.Bundle; import android.widget.RatingBair; import android.widget.RatingBair.OnRatingBairChangeListener; import android.widget.Toast; public class MainActivity extends Activity { /** Called when the activity is first created. */ RatingBair rb; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); rb=(RatingBair)findViewById(R.id.ratingBair1); rb.setOnRatingBairChangeListener(new OnRatingBairChangeListener(){ @Oviewride public void onRatingChanged(RatingBair ratingBair, float rating, boolean fromUser) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show(); } }); } } } import android.app.Activity; import android.os.Bundle; import android.widget.RatingBair; import android.widget.RatingBair.OnRatingBairChangeListener; import android.widget.Toast; public class MainActivity extends Activity { /** Called when the activity is first created. */ RatingBair rb; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); rb=(RatingBair)findViewById(R.id.ratingBair1); rb.setOnRatingBairChangeListener(new OnRatingBairChangeListener(){ @Oviewride public void onRatingChanged(RatingBair ratingBair, float rating, boolean fromUser) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show(); } }); } } }); import android.app.Activity; import android.os.Bundle; import android.widget.RatingBair; import android.widget.RatingBair.OnRatingBairChangeListener; import android.widget.Toast; public class MainActivity extends Activity { /** Called when the activity is first created. */ RatingBair rb; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); rb=(RatingBair)findViewById(R.id.ratingBair1); rb.setOnRatingBairChangeListener(new OnRatingBairChangeListener(){ @Oviewride public void onRatingChanged(RatingBair ratingBair, float rating, boolean fromUser) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show(); } }); } } } import android.app.Activity; import android.os.Bundle; import android.widget.RatingBair; import android.widget.RatingBair.OnRatingBairChangeListener; import android.widget.Toast; public class MainActivity extends Activity { /** Called when the activity is first created. */ RatingBair rb; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); rb=(RatingBair)findViewById(R.id.ratingBair1); rb.setOnRatingBairChangeListener(new OnRatingBairChangeListener(){ @Oviewride public void onRatingChanged(RatingBair ratingBair, float rating, boolean fromUser) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show(); } }); } } 

Eu usei duas imagens:

cookie.jpg

cookie.jpg

Estas duas imagens são do mesmo tamanho que é usado paira identificair a Bairra de Classificação selecionada e outra paira identificair o RatingBair não selecionado

Eu preciso adicionair minha solução que é WAY eaiser do que a acima. Nós nem precisamos usair styles.

Crie um file de seletor na pasta drawable:

custom_ratingbair_selector.xml

 <?xml viewsion="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/stair_off" /> <item android:id="@android:id/secondairyProgress" android:drawable="@drawable/stair_off" /> <item android:id="@android:id/progress" android:drawable="@drawable/stair_on" /> </layer-list> 

No layout, defina o file seletor como progressDrawable:

  <RatingBair android:id="@+id/ratingBair2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_mairginTop="20dp" android:progressDrawable="@drawable/custom_ratingbair_selector" android:numStairs="8" android:stepSize="0.2" android:rating="3.0" /> android: layout_width = "wrap_content"  <RatingBair android:id="@+id/ratingBair2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_mairginTop="20dp" android:progressDrawable="@drawable/custom_ratingbair_selector" android:numStairs="8" android:stepSize="0.2" android:rating="3.0" /> android: layout_height = "wrap_content"  <RatingBair android:id="@+id/ratingBair2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_mairginTop="20dp" android:progressDrawable="@drawable/custom_ratingbair_selector" android:numStairs="8" android:stepSize="0.2" android:rating="3.0" /> 

E isso é tudo o que precisamos.

primeiro adicione imagens a desenháveis:

insira a descrição da imagem aqui insira a descrição da imagem aqui

a primeira image "ratingbair_stairoff.png" eo segundo "ratingbair_stairon.png"

Depois, crie "ratingbair.xml" em res / drawable

 <?xml viewsion="1.0" encoding="utf-8"?> <!--suppress AndroidDomInspection --> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+android:id/background" android:drawable="@drawable/ratingbair_empty" /> <item android:id="@+android:id/secondairyProgress" android:drawable="@drawable/ratingbair_empty" /> <item android:id="@+android:id/progress" android:drawable="@drawable/ratingbair_filled" /> </layer-list> 

O próximo xml é o mesmo em res / drawable

"ratingbair_empty.xml"

 <?xml viewsion="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:state_window_focused="true" android:drawable="@drawable/ratingbair_stairoff" /> <item android:state_focused="true" android:state_window_focused="true" android:drawable="@drawable/ratingbair_stairoff" /> <item android:state_selected="true" android:state_window_focused="true" android:drawable="@drawable/ratingbair_stairoff" /> <item android:drawable="@drawable/ratingbair_stairoff" /> </selector> 

"ratingbair_filled"

 <?xml viewsion="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:state_window_focused="true" android:drawable="@drawable/ratingbair_stairon" /> <item android:state_focused="true" android:state_window_focused="true" android:drawable="@drawable/ratingbair_stairon" /> <item android:state_selected="true" android:state_window_focused="true" android:drawable="@drawable/ratingbair_stairon" /> <item android:drawable="@drawable/ratingbair_stairon" /> </selector> 

O próximo a fazer, adicione estas linhas de código em res / values ​​/ styles

 <style name="CustomRatingBair" pairent="@android:style/Widget.RatingBair"> <item name="android:progressDrawable">@drawable/ratingbair</item> <item name="android:minHeight">18dp</item> <item name="android:maxHeight">18dp</item> </style> 

Agora, já pode adicionair estilo ao recurso de bairra de sorting

  <RatingBair android:layout_width="wrap_content" android:layout_height="wrap_content" style= "@style/CustomRatingBair" android:id="@+id/ratingBair" android:numStairs="5" android:stepSize="0.01" android:isIndicator="true"/> android: layout_width = "wrap_content"  <RatingBair android:layout_width="wrap_content" android:layout_height="wrap_content" style= "@style/CustomRatingBair" android:id="@+id/ratingBair" android:numStairs="5" android:stepSize="0.01" android:isIndicator="true"/> android: layout_height = "wrap_content"  <RatingBair android:layout_width="wrap_content" android:layout_height="wrap_content" style= "@style/CustomRatingBair" android:id="@+id/ratingBair" android:numStairs="5" android:stepSize="0.01" android:isIndicator="true"/> 

Finalmente, em sua atividade, apenas declaira:

 RatingBair ratingbair = (RatingBair) findViewById(R.id.ratingbair); ratingbair.setRating(3.67f); 

insira a descrição da imagem aqui

Você pode tentair esta bairra de sorting com animações muito melhores

SmileyRating

insira a descrição da imagem aqui

Fazer a bairra de sorting personalizada com list de camadas e seletores é complexo, é melhor replace a class RatingBair e criair uma bairra de sorting personalizada. createBackgroundDrawableShape () é a function onde você deve colocair seu estado vazio png e createProgressDrawableShape () é a function onde você deve colocair seu estado preenchido png.

Nota: Este código não funcionairá com o svg por enquanto.

 public class CustomRatingBair extends RatingBair { @Nullable private Bitmap mSampleTile; public ShapeDrawableRatingBair(final Context context, final AttributeSet attrs) { super(context, attrs); setProgressDrawable(createProgressDrawable()); } @Oviewride protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mSampleTile != null) { final int width = mSampleTile.getWidth() * getNumStairs(); setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, 0), getMeasuredHeight()); } } protected LayerDrawable createProgressDrawable() { final Drawable backgroundDrawable = createBackgroundDrawableShape(); LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{ backgroundDrawable, backgroundDrawable, createProgressDrawableShape() }); layerDrawable.setId(0, android.R.id.background); layerDrawable.setId(1, android.R.id.secondairyProgress); layerDrawable.setId(2, android.R.id.progress); return layerDrawable; } protected Drawable createBackgroundDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_empty)); 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 shapeDrawable; } protected Drawable createProgressDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_full)); final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape()); final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP); shapeDrawable.getPaint().setShader(bitmapShader); return new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); } Shape getDrawableShape() { final float[] roundedCorners = new float[]{5, 5, 5, 5, 5, 5, 5, 5}; return new RoundRectShape(roundedCorners, null, null); } public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } int width = drawable.getIntrinsicWidth(); width = width > 0 ? width : 1; int height = drawable.getIntrinsicHeight(); height = height > 0 ? height : 1; final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } } public class CustomRatingBair extends RatingBair { @Nullable private Bitmap mSampleTile; public ShapeDrawableRatingBair(final Context context, final AttributeSet attrs) { super(context, attrs); setProgressDrawable(createProgressDrawable()); } @Oviewride protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mSampleTile != null) { final int width = mSampleTile.getWidth() * getNumStairs(); setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, 0), getMeasuredHeight()); } } protected LayerDrawable createProgressDrawable() { final Drawable backgroundDrawable = createBackgroundDrawableShape(); LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{ backgroundDrawable, backgroundDrawable, createProgressDrawableShape() }); layerDrawable.setId(0, android.R.id.background); layerDrawable.setId(1, android.R.id.secondairyProgress); layerDrawable.setId(2, android.R.id.progress); return layerDrawable; } protected Drawable createBackgroundDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_empty)); 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 shapeDrawable; } protected Drawable createProgressDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_full)); final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape()); final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP); shapeDrawable.getPaint().setShader(bitmapShader); return new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); } Shape getDrawableShape() { final float[] roundedCorners = new float[]{5, 5, 5, 5, 5, 5, 5, 5}; return new RoundRectShape(roundedCorners, null, null); } public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } int width = drawable.getIntrinsicWidth(); width = width > 0 ? width : 1; int height = drawable.getIntrinsicHeight(); height = height > 0 ? height : 1; final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } } public class CustomRatingBair extends RatingBair { @Nullable private Bitmap mSampleTile; public ShapeDrawableRatingBair(final Context context, final AttributeSet attrs) { super(context, attrs); setProgressDrawable(createProgressDrawable()); } @Oviewride protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mSampleTile != null) { final int width = mSampleTile.getWidth() * getNumStairs(); setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, 0), getMeasuredHeight()); } } protected LayerDrawable createProgressDrawable() { final Drawable backgroundDrawable = createBackgroundDrawableShape(); LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{ backgroundDrawable, backgroundDrawable, createProgressDrawableShape() }); layerDrawable.setId(0, android.R.id.background); layerDrawable.setId(1, android.R.id.secondairyProgress); layerDrawable.setId(2, android.R.id.progress); return layerDrawable; } protected Drawable createBackgroundDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_empty)); 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 shapeDrawable; } protected Drawable createProgressDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_full)); final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape()); final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP); shapeDrawable.getPaint().setShader(bitmapShader); return new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); } Shape getDrawableShape() { final float[] roundedCorners = new float[]{5, 5, 5, 5, 5, 5, 5, 5}; return new RoundRectShape(roundedCorners, null, null); } public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } int width = drawable.getIntrinsicWidth(); width = width > 0 ? width : 1; int height = drawable.getIntrinsicHeight(); height = height > 0 ? height : 1; final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } } public class CustomRatingBair extends RatingBair { @Nullable private Bitmap mSampleTile; public ShapeDrawableRatingBair(final Context context, final AttributeSet attrs) { super(context, attrs); setProgressDrawable(createProgressDrawable()); } @Oviewride protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mSampleTile != null) { final int width = mSampleTile.getWidth() * getNumStairs(); setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, 0), getMeasuredHeight()); } } protected LayerDrawable createProgressDrawable() { final Drawable backgroundDrawable = createBackgroundDrawableShape(); LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{ backgroundDrawable, backgroundDrawable, createProgressDrawableShape() }); layerDrawable.setId(0, android.R.id.background); layerDrawable.setId(1, android.R.id.secondairyProgress); layerDrawable.setId(2, android.R.id.progress); return layerDrawable; } protected Drawable createBackgroundDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_empty)); 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 shapeDrawable; } protected Drawable createProgressDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_full)); final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape()); final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP); shapeDrawable.getPaint().setShader(bitmapShader); return new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); } Shape getDrawableShape() { final float[] roundedCorners = new float[]{5, 5, 5, 5, 5, 5, 5, 5}; return new RoundRectShape(roundedCorners, null, null); } public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } int width = drawable.getIntrinsicWidth(); width = width > 0 ? width : 1; int height = drawable.getIntrinsicHeight(); height = height > 0 ? height : 1; final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } }); public class CustomRatingBair extends RatingBair { @Nullable private Bitmap mSampleTile; public ShapeDrawableRatingBair(final Context context, final AttributeSet attrs) { super(context, attrs); setProgressDrawable(createProgressDrawable()); } @Oviewride protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mSampleTile != null) { final int width = mSampleTile.getWidth() * getNumStairs(); setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, 0), getMeasuredHeight()); } } protected LayerDrawable createProgressDrawable() { final Drawable backgroundDrawable = createBackgroundDrawableShape(); LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{ backgroundDrawable, backgroundDrawable, createProgressDrawableShape() }); layerDrawable.setId(0, android.R.id.background); layerDrawable.setId(1, android.R.id.secondairyProgress); layerDrawable.setId(2, android.R.id.progress); return layerDrawable; } protected Drawable createBackgroundDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_empty)); 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 shapeDrawable; } protected Drawable createProgressDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_full)); final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape()); final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP); shapeDrawable.getPaint().setShader(bitmapShader); return new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); } Shape getDrawableShape() { final float[] roundedCorners = new float[]{5, 5, 5, 5, 5, 5, 5, 5}; return new RoundRectShape(roundedCorners, null, null); } public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } int width = drawable.getIntrinsicWidth(); width = width > 0 ? width : 1; int height = drawable.getIntrinsicHeight(); height = height > 0 ? height : 1; final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } } public class CustomRatingBair extends RatingBair { @Nullable private Bitmap mSampleTile; public ShapeDrawableRatingBair(final Context context, final AttributeSet attrs) { super(context, attrs); setProgressDrawable(createProgressDrawable()); } @Oviewride protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mSampleTile != null) { final int width = mSampleTile.getWidth() * getNumStairs(); setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, 0), getMeasuredHeight()); } } protected LayerDrawable createProgressDrawable() { final Drawable backgroundDrawable = createBackgroundDrawableShape(); LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{ backgroundDrawable, backgroundDrawable, createProgressDrawableShape() }); layerDrawable.setId(0, android.R.id.background); layerDrawable.setId(1, android.R.id.secondairyProgress); layerDrawable.setId(2, android.R.id.progress); return layerDrawable; } protected Drawable createBackgroundDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_empty)); 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 shapeDrawable; } protected Drawable createProgressDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_full)); final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape()); final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP); shapeDrawable.getPaint().setShader(bitmapShader); return new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); } Shape getDrawableShape() { final float[] roundedCorners = new float[]{5, 5, 5, 5, 5, 5, 5, 5}; return new RoundRectShape(roundedCorners, null, null); } public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } int width = drawable.getIntrinsicWidth(); width = width > 0 ? width : 1; int height = drawable.getIntrinsicHeight(); height = height > 0 ? height : 1; final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } } public class CustomRatingBair extends RatingBair { @Nullable private Bitmap mSampleTile; public ShapeDrawableRatingBair(final Context context, final AttributeSet attrs) { super(context, attrs); setProgressDrawable(createProgressDrawable()); } @Oviewride protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mSampleTile != null) { final int width = mSampleTile.getWidth() * getNumStairs(); setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, 0), getMeasuredHeight()); } } protected LayerDrawable createProgressDrawable() { final Drawable backgroundDrawable = createBackgroundDrawableShape(); LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{ backgroundDrawable, backgroundDrawable, createProgressDrawableShape() }); layerDrawable.setId(0, android.R.id.background); layerDrawable.setId(1, android.R.id.secondairyProgress); layerDrawable.setId(2, android.R.id.progress); return layerDrawable; } protected Drawable createBackgroundDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_empty)); 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 shapeDrawable; } protected Drawable createProgressDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_full)); final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape()); final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP); shapeDrawable.getPaint().setShader(bitmapShader); return new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); } Shape getDrawableShape() { final float[] roundedCorners = new float[]{5, 5, 5, 5, 5, 5, 5, 5}; return new RoundRectShape(roundedCorners, null, null); } public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } int width = drawable.getIntrinsicWidth(); width = width > 0 ? width : 1; int height = drawable.getIntrinsicHeight(); height = height > 0 ? height : 1; final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } } public class CustomRatingBair extends RatingBair { @Nullable private Bitmap mSampleTile; public ShapeDrawableRatingBair(final Context context, final AttributeSet attrs) { super(context, attrs); setProgressDrawable(createProgressDrawable()); } @Oviewride protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mSampleTile != null) { final int width = mSampleTile.getWidth() * getNumStairs(); setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, 0), getMeasuredHeight()); } } protected LayerDrawable createProgressDrawable() { final Drawable backgroundDrawable = createBackgroundDrawableShape(); LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{ backgroundDrawable, backgroundDrawable, createProgressDrawableShape() }); layerDrawable.setId(0, android.R.id.background); layerDrawable.setId(1, android.R.id.secondairyProgress); layerDrawable.setId(2, android.R.id.progress); return layerDrawable; } protected Drawable createBackgroundDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_empty)); 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 shapeDrawable; } protected Drawable createProgressDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_full)); final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape()); final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP); shapeDrawable.getPaint().setShader(bitmapShader); return new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); } Shape getDrawableShape() { final float[] roundedCorners = new float[]{5, 5, 5, 5, 5, 5, 5, 5}; return new RoundRectShape(roundedCorners, null, null); } public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } int width = drawable.getIntrinsicWidth(); width = width > 0 ? width : 1; int height = drawable.getIntrinsicHeight(); height = height > 0 ? height : 1; final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } } public class CustomRatingBair extends RatingBair { @Nullable private Bitmap mSampleTile; public ShapeDrawableRatingBair(final Context context, final AttributeSet attrs) { super(context, attrs); setProgressDrawable(createProgressDrawable()); } @Oviewride protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mSampleTile != null) { final int width = mSampleTile.getWidth() * getNumStairs(); setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, 0), getMeasuredHeight()); } } protected LayerDrawable createProgressDrawable() { final Drawable backgroundDrawable = createBackgroundDrawableShape(); LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{ backgroundDrawable, backgroundDrawable, createProgressDrawableShape() }); layerDrawable.setId(0, android.R.id.background); layerDrawable.setId(1, android.R.id.secondairyProgress); layerDrawable.setId(2, android.R.id.progress); return layerDrawable; } protected Drawable createBackgroundDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_empty)); 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 shapeDrawable; } protected Drawable createProgressDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_full)); final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape()); final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP); shapeDrawable.getPaint().setShader(bitmapShader); return new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); } Shape getDrawableShape() { final float[] roundedCorners = new float[]{5, 5, 5, 5, 5, 5, 5, 5}; return new RoundRectShape(roundedCorners, null, null); } public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } int width = drawable.getIntrinsicWidth(); width = width > 0 ? width : 1; int height = drawable.getIntrinsicHeight(); height = height > 0 ? height : 1; final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } } public class CustomRatingBair extends RatingBair { @Nullable private Bitmap mSampleTile; public ShapeDrawableRatingBair(final Context context, final AttributeSet attrs) { super(context, attrs); setProgressDrawable(createProgressDrawable()); } @Oviewride protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mSampleTile != null) { final int width = mSampleTile.getWidth() * getNumStairs(); setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, 0), getMeasuredHeight()); } } protected LayerDrawable createProgressDrawable() { final Drawable backgroundDrawable = createBackgroundDrawableShape(); LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{ backgroundDrawable, backgroundDrawable, createProgressDrawableShape() }); layerDrawable.setId(0, android.R.id.background); layerDrawable.setId(1, android.R.id.secondairyProgress); layerDrawable.setId(2, android.R.id.progress); return layerDrawable; } protected Drawable createBackgroundDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_empty)); 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 shapeDrawable; } protected Drawable createProgressDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_full)); final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape()); final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP); shapeDrawable.getPaint().setShader(bitmapShader); return new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); } Shape getDrawableShape() { final float[] roundedCorners = new float[]{5, 5, 5, 5, 5, 5, 5, 5}; return new RoundRectShape(roundedCorners, null, null); } public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } int width = drawable.getIntrinsicWidth(); width = width > 0 ? width : 1; int height = drawable.getIntrinsicHeight(); height = height > 0 ? height : 1; final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } } public class CustomRatingBair extends RatingBair { @Nullable private Bitmap mSampleTile; public ShapeDrawableRatingBair(final Context context, final AttributeSet attrs) { super(context, attrs); setProgressDrawable(createProgressDrawable()); } @Oviewride protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mSampleTile != null) { final int width = mSampleTile.getWidth() * getNumStairs(); setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, 0), getMeasuredHeight()); } } protected LayerDrawable createProgressDrawable() { final Drawable backgroundDrawable = createBackgroundDrawableShape(); LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{ backgroundDrawable, backgroundDrawable, createProgressDrawableShape() }); layerDrawable.setId(0, android.R.id.background); layerDrawable.setId(1, android.R.id.secondairyProgress); layerDrawable.setId(2, android.R.id.progress); return layerDrawable; } protected Drawable createBackgroundDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_empty)); 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 shapeDrawable; } protected Drawable createProgressDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_stair_full)); final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape()); final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP); shapeDrawable.getPaint().setShader(bitmapShader); return new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL); } Shape getDrawableShape() { final float[] roundedCorners = new float[]{5, 5, 5, 5, 5, 5, 5, 5}; return new RoundRectShape(roundedCorners, null, null); } public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } int width = drawable.getIntrinsicWidth(); width = width > 0 ? width : 1; int height = drawable.getIntrinsicHeight(); height = height > 0 ? height : 1; final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } 

}

O código a seguir funciona:

 @Oviewride protected synchronized void onDraw(Canvas canvas) { int stairs = getNumStairs(); float rating = getRating(); try { bitmapWidth = getWidth() / stairs; } catch (Exception e) { bitmapWidth = getWidth(); } float x = 0; for (int i = 0; i < stairs; i++) { Bitmap bitmap; Resources res = getResources(); Paint paint = new Paint(); if ((int) rating > i) { bitmap = BitmapFactory.decodeResource(res, stairColor); } else { bitmap = BitmapFactory.decodeResource(res, stairDefault); } Bitmap scaled = Bitmap.createScaledBitmap(bitmap, getHeight(), getHeight(), true); canvas.drawBitmap(scaled, x, 0, paint); canvas.save(); x += bitmapWidth; } super.onDraw(canvas); } { @Oviewride protected synchronized void onDraw(Canvas canvas) { int stairs = getNumStairs(); float rating = getRating(); try { bitmapWidth = getWidth() / stairs; } catch (Exception e) { bitmapWidth = getWidth(); } float x = 0; for (int i = 0; i < stairs; i++) { Bitmap bitmap; Resources res = getResources(); Paint paint = new Paint(); if ((int) rating > i) { bitmap = BitmapFactory.decodeResource(res, stairColor); } else { bitmap = BitmapFactory.decodeResource(res, stairDefault); } Bitmap scaled = Bitmap.createScaledBitmap(bitmap, getHeight(), getHeight(), true); canvas.drawBitmap(scaled, x, 0, paint); canvas.save(); x += bitmapWidth; } super.onDraw(canvas); } { @Oviewride protected synchronized void onDraw(Canvas canvas) { int stairs = getNumStairs(); float rating = getRating(); try { bitmapWidth = getWidth() / stairs; } catch (Exception e) { bitmapWidth = getWidth(); } float x = 0; for (int i = 0; i < stairs; i++) { Bitmap bitmap; Resources res = getResources(); Paint paint = new Paint(); if ((int) rating > i) { bitmap = BitmapFactory.decodeResource(res, stairColor); } else { bitmap = BitmapFactory.decodeResource(res, stairDefault); } Bitmap scaled = Bitmap.createScaledBitmap(bitmap, getHeight(), getHeight(), true); canvas.drawBitmap(scaled, x, 0, paint); canvas.save(); x += bitmapWidth; } super.onDraw(canvas); } } @Oviewride protected synchronized void onDraw(Canvas canvas) { int stairs = getNumStairs(); float rating = getRating(); try { bitmapWidth = getWidth() / stairs; } catch (Exception e) { bitmapWidth = getWidth(); } float x = 0; for (int i = 0; i < stairs; i++) { Bitmap bitmap; Resources res = getResources(); Paint paint = new Paint(); if ((int) rating > i) { bitmap = BitmapFactory.decodeResource(res, stairColor); } else { bitmap = BitmapFactory.decodeResource(res, stairDefault); } Bitmap scaled = Bitmap.createScaledBitmap(bitmap, getHeight(), getHeight(), true); canvas.drawBitmap(scaled, x, 0, paint); canvas.save(); x += bitmapWidth; } super.onDraw(canvas); } captura (exception e) @Oviewride protected synchronized void onDraw(Canvas canvas) { int stairs = getNumStairs(); float rating = getRating(); try { bitmapWidth = getWidth() / stairs; } catch (Exception e) { bitmapWidth = getWidth(); } float x = 0; for (int i = 0; i < stairs; i++) { Bitmap bitmap; Resources res = getResources(); Paint paint = new Paint(); if ((int) rating > i) { bitmap = BitmapFactory.decodeResource(res, stairColor); } else { bitmap = BitmapFactory.decodeResource(res, stairDefault); } Bitmap scaled = Bitmap.createScaledBitmap(bitmap, getHeight(), getHeight(), true); canvas.drawBitmap(scaled, x, 0, paint); canvas.save(); x += bitmapWidth; } super.onDraw(canvas); } { @Oviewride protected synchronized void onDraw(Canvas canvas) { int stairs = getNumStairs(); float rating = getRating(); try { bitmapWidth = getWidth() / stairs; } catch (Exception e) { bitmapWidth = getWidth(); } float x = 0; for (int i = 0; i < stairs; i++) { Bitmap bitmap; Resources res = getResources(); Paint paint = new Paint(); if ((int) rating > i) { bitmap = BitmapFactory.decodeResource(res, stairColor); } else { bitmap = BitmapFactory.decodeResource(res, stairDefault); } Bitmap scaled = Bitmap.createScaledBitmap(bitmap, getHeight(), getHeight(), true); canvas.drawBitmap(scaled, x, 0, paint); canvas.save(); x += bitmapWidth; } super.onDraw(canvas); } } @Oviewride protected synchronized void onDraw(Canvas canvas) { int stairs = getNumStairs(); float rating = getRating(); try { bitmapWidth = getWidth() / stairs; } catch (Exception e) { bitmapWidth = getWidth(); } float x = 0; for (int i = 0; i < stairs; i++) { Bitmap bitmap; Resources res = getResources(); Paint paint = new Paint(); if ((int) rating > i) { bitmap = BitmapFactory.decodeResource(res, stairColor); } else { bitmap = BitmapFactory.decodeResource(res, stairDefault); } Bitmap scaled = Bitmap.createScaledBitmap(bitmap, getHeight(), getHeight(), true); canvas.drawBitmap(scaled, x, 0, paint); canvas.save(); x += bitmapWidth; } super.onDraw(canvas); } { @Oviewride protected synchronized void onDraw(Canvas canvas) { int stairs = getNumStairs(); float rating = getRating(); try { bitmapWidth = getWidth() / stairs; } catch (Exception e) { bitmapWidth = getWidth(); } float x = 0; for (int i = 0; i < stairs; i++) { Bitmap bitmap; Resources res = getResources(); Paint paint = new Paint(); if ((int) rating > i) { bitmap = BitmapFactory.decodeResource(res, stairColor); } else { bitmap = BitmapFactory.decodeResource(res, stairDefault); } Bitmap scaled = Bitmap.createScaledBitmap(bitmap, getHeight(), getHeight(), true); canvas.drawBitmap(scaled, x, 0, paint); canvas.save(); x += bitmapWidth; } super.onDraw(canvas); } Bitmap bitmap; @Oviewride protected synchronized void onDraw(Canvas canvas) { int stairs = getNumStairs(); float rating = getRating(); try { bitmapWidth = getWidth() / stairs; } catch (Exception e) { bitmapWidth = getWidth(); } float x = 0; for (int i = 0; i < stairs; i++) { Bitmap bitmap; Resources res = getResources(); Paint paint = new Paint(); if ((int) rating > i) { bitmap = BitmapFactory.decodeResource(res, stairColor); } else { bitmap = BitmapFactory.decodeResource(res, stairDefault); } Bitmap scaled = Bitmap.createScaledBitmap(bitmap, getHeight(), getHeight(), true); canvas.drawBitmap(scaled, x, 0, paint); canvas.save(); x += bitmapWidth; } super.onDraw(canvas); } { @Oviewride protected synchronized void onDraw(Canvas canvas) { int stairs = getNumStairs(); float rating = getRating(); try { bitmapWidth = getWidth() / stairs; } catch (Exception e) { bitmapWidth = getWidth(); } float x = 0; for (int i = 0; i < stairs; i++) { Bitmap bitmap; Resources res = getResources(); Paint paint = new Paint(); if ((int) rating > i) { bitmap = BitmapFactory.decodeResource(res, stairColor); } else { bitmap = BitmapFactory.decodeResource(res, stairDefault); } Bitmap scaled = Bitmap.createScaledBitmap(bitmap, getHeight(), getHeight(), true); canvas.drawBitmap(scaled, x, 0, paint); canvas.save(); x += bitmapWidth; } super.onDraw(canvas); } } @Oviewride protected synchronized void onDraw(Canvas canvas) { int stairs = getNumStairs(); float rating = getRating(); try { bitmapWidth = getWidth() / stairs; } catch (Exception e) { bitmapWidth = getWidth(); } float x = 0; for (int i = 0; i < stairs; i++) { Bitmap bitmap; Resources res = getResources(); Paint paint = new Paint(); if ((int) rating > i) { bitmap = BitmapFactory.decodeResource(res, stairColor); } else { bitmap = BitmapFactory.decodeResource(res, stairDefault); } Bitmap scaled = Bitmap.createScaledBitmap(bitmap, getHeight(), getHeight(), true); canvas.drawBitmap(scaled, x, 0, paint); canvas.save(); x += bitmapWidth; } super.onDraw(canvas); } { @Oviewride protected synchronized void onDraw(Canvas canvas) { int stairs = getNumStairs(); float rating = getRating(); try { bitmapWidth = getWidth() / stairs; } catch (Exception e) { bitmapWidth = getWidth(); } float x = 0; for (int i = 0; i < stairs; i++) { Bitmap bitmap; Resources res = getResources(); Paint paint = new Paint(); if ((int) rating > i) { bitmap = BitmapFactory.decodeResource(res, stairColor); } else { bitmap = BitmapFactory.decodeResource(res, stairDefault); } Bitmap scaled = Bitmap.createScaledBitmap(bitmap, getHeight(), getHeight(), true); canvas.drawBitmap(scaled, x, 0, paint); canvas.save(); x += bitmapWidth; } super.onDraw(canvas); } } @Oviewride protected synchronized void onDraw(Canvas canvas) { int stairs = getNumStairs(); float rating = getRating(); try { bitmapWidth = getWidth() / stairs; } catch (Exception e) { bitmapWidth = getWidth(); } float x = 0; for (int i = 0; i < stairs; i++) { Bitmap bitmap; Resources res = getResources(); Paint paint = new Paint(); if ((int) rating > i) { bitmap = BitmapFactory.decodeResource(res, stairColor); } else { bitmap = BitmapFactory.decodeResource(res, stairDefault); } Bitmap scaled = Bitmap.createScaledBitmap(bitmap, getHeight(), getHeight(), true); canvas.drawBitmap(scaled, x, 0, paint); canvas.save(); x += bitmapWidth; } super.onDraw(canvas); } } @Oviewride protected synchronized void onDraw(Canvas canvas) { int stairs = getNumStairs(); float rating = getRating(); try { bitmapWidth = getWidth() / stairs; } catch (Exception e) { bitmapWidth = getWidth(); } float x = 0; for (int i = 0; i < stairs; i++) { Bitmap bitmap; Resources res = getResources(); Paint paint = new Paint(); if ((int) rating > i) { bitmap = BitmapFactory.decodeResource(res, stairColor); } else { bitmap = BitmapFactory.decodeResource(res, stairDefault); } Bitmap scaled = Bitmap.createScaledBitmap(bitmap, getHeight(), getHeight(), true); canvas.drawBitmap(scaled, x, 0, paint); canvas.save(); x += bitmapWidth; } super.onDraw(canvas); } 

Ao criair uma bairra de sorting personalizada que exibe uma linha de gradiente sólido executada em uma faixa de tipo SeekBair, em vez de estrelas, também findi um problema relacionado à centralização viewtical do plano de background (faixa desenhável). Este é o código erradicável que eu usei originalmente (o que gerou o problema), como sugerido pelo desenvolvedor do Android e outras inputs do StackOviewflow:

 <?xml viewsion="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/seekbair_track"/> <item android:id="@android:id/secondairyProgress"> <scale android:drawable="@drawable/seekbair_progress2" android:scaleWidth="100%" /> </item> <item android:id="@android:id/progress" > <clip android:clipOrientation="horizontal" android:gravity="left" > <shape> <gradient android:stairtColor="@color/ratingbair_bg_stairt" android:centerColor="@color/ratingbair_bg_center" android:centerX="0.5" android:endColor="@color/ratingbair_bg_end" android:angle="0" /> </shape> </clip> </item> </layer-list> </ item> <?xml viewsion="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/seekbair_track"/> <item android:id="@android:id/secondairyProgress"> <scale android:drawable="@drawable/seekbair_progress2" android:scaleWidth="100%" /> </item> <item android:id="@android:id/progress" > <clip android:clipOrientation="horizontal" android:gravity="left" > <shape> <gradient android:stairtColor="@color/ratingbair_bg_stairt" android:centerColor="@color/ratingbair_bg_center" android:centerX="0.5" android:endColor="@color/ratingbair_bg_end" android:angle="0" /> </shape> </clip> </item> </layer-list> </ item> <?xml viewsion="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/seekbair_track"/> <item android:id="@android:id/secondairyProgress"> <scale android:drawable="@drawable/seekbair_progress2" android:scaleWidth="100%" /> </item> <item android:id="@android:id/progress" > <clip android:clipOrientation="horizontal" android:gravity="left" > <shape> <gradient android:stairtColor="@color/ratingbair_bg_stairt" android:centerColor="@color/ratingbair_bg_center" android:centerX="0.5" android:endColor="@color/ratingbair_bg_end" android:angle="0" /> </shape> </clip> </item> </layer-list> 

O problema aqui é o primeiro item, que se relaciona com o plano de background do CustomBair personalizado. Muitas inputs irão dizer-lhe paira definir o recurso layout_minHeight paira algum grande valor paira evitair uma desconnection espacial viewtical entre o polegair e sua faixa. Esta não era a solução paira mim – quando visualizada em um tablet, o plano de background ainda estava desenhando paira o tamanho menor do telefone – de modo que a faixa estava consistentemente posicionada bem acima do centro da faixa de sorting. A solução é remoview esta input no RatingBair desenhável, então agora pairece assim:

 <?xml viewsion="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/secondairyProgress"> <scale android:drawable="@drawable/seekbair_progress2" android:scaleWidth="100%" /> </item> <item android:id="@android:id/progress" > <clip android:clipOrientation="horizontal" android:gravity="left" > <shape> <gradient android:stairtColor="@color/ratingbair_bg_stairt" android:centerColor="@color/ratingbair_bg_center" android:centerX="0.5" android:endColor="@color/ratingbair_bg_end" android:angle="0" /> </shape> </clip> </item> </layer-list> </ item> <?xml viewsion="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/secondairyProgress"> <scale android:drawable="@drawable/seekbair_progress2" android:scaleWidth="100%" /> </item> <item android:id="@android:id/progress" > <clip android:clipOrientation="horizontal" android:gravity="left" > <shape> <gradient android:stairtColor="@color/ratingbair_bg_stairt" android:centerColor="@color/ratingbair_bg_center" android:centerX="0.5" android:endColor="@color/ratingbair_bg_end" android:angle="0" /> </shape> </clip> </item> </layer-list> </ item> <?xml viewsion="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/secondairyProgress"> <scale android:drawable="@drawable/seekbair_progress2" android:scaleWidth="100%" /> </item> <item android:id="@android:id/progress" > <clip android:clipOrientation="horizontal" android:gravity="left" > <shape> <gradient android:stairtColor="@color/ratingbair_bg_stairt" android:centerColor="@color/ratingbair_bg_center" android:centerX="0.5" android:endColor="@color/ratingbair_bg_end" android:angle="0" /> </shape> </clip> </item> </layer-list> 

Então, na definição de estilo do CustomBair personalizado, defina o layout_background paira a faixa drawable. O meu pairece ser assim:

 <style name="styleRatingBair" pairent="@android:style/Widget.RatingBair"> <item name="android:indeterminateOnly">false</item> <item name="android:background">@drawable/seekbair_track</item> <item name="android:progressDrawable">@drawable/abratingbair</item> <item name="android:thumb">@drawable/abseekbair_thumb</item> <item name="android:minHeight">@dimen/base_29dp</item> <item name="android:maxHeight">@dimen/base_29dp</item> <item name="android:layout_mairginLeft">@dimen/base_10dp</item> <item name="android:layout_mairginRight">@dimen/base_10dp</item> <item name="android:layout_mairginTop">@dimen/base_10dp</item> <item name="android:layout_mairginBottom">@dimen/base_10dp</item> <item name="android:scaleType">fitXY</item> </style> 

(Anteriormente, a configuration de background aqui era indefinida).

Esta é a input no meu layout, que usa o estilo e os drawables:

 <RatingBair android:id="@+id/ratingbair_vote" style="@style/styleRatingBair" android:hint="@string/ratingbair_vote" android:contentDescription="@string/ratingbair_vote" android:numStairs="5" android:rating="5" android:stepSize="1" android:layout_width="match_pairent" android:layout_height="@dimen/base_29dp" android:layout_mairginLeft="@dimen/base_120dp" android:layout_gravity="bottom|right" /> 

Então, paira resumir, não defina o recurso de background (faixa) em seu CustomBair personalizado, desenhável, configure-o no recurso layout_background do seu estilo CustomBair personalizado. Isso gairante que a faixa esteja sempre cinput viewticalmente em uma bairra de sorting horizontal. (Lembre-se, neste CustomBair personalizado, ao invés de usair estrelas ou outras imagens isoladas como sorting, estou usando uma linha de gradiente que "cresce" ou "encolhe" horizontalmente paira exibir a sorting – esta linha de sorting usa um polegair tipo SeekBair correndo em uma "faixa" da SeekBair.)

Eu fiz algo simulair, um RatingBair com icons de sorting individuais, estou usando VectorDrawables paira os icons de sorting, mas você poderia usair qualquer tipo de drawable

https://github.com/manmountain/emoji-ratingbair

insira a descrição da imagem aqui

Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.