大家好我需要在我的应用程序中执行评级...所以我需要创建自定义评级栏...任何人都可以帮助我吗?
编辑
看看摩托罗拉的自定义评级http://community.developer.motorola.com/t5/Android-App-Development-for/custom-rating-bar-style-using-android-s-ratingBar-small-style/ TD-p/10462
更新
styles.xml
它必须位于您的values文件夹中
food_rating_bar_full.xml
此文件必须位于Drawable文件夹中.
food_ratingbar_full_empty.xml
此文件必须位于Drawable文件夹中.
food_ratingbar_full_filled.xml
此文件必须位于Drawable文件夹中.
main.xml文件应如下所示:
MainActivity.class应如下所示:
import android.app.Activity; import android.os.Bundle; import android.widget.RatingBar; import android.widget.RatingBar.OnRatingBarChangeListener; import android.widget.Toast; public class MainActivity extends Activity { /** Called when the activity is first created. */ RatingBar rb; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); rb=(RatingBar)findViewById(R.id.ratingBar1); rb.setOnRatingBarChangeListener(new OnRatingBarChangeListener(){ @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show(); } }); } }
我用过两张图片:
cookie.jpg
cookiee.jpg
这两个图像大小相同,用于识别选定的评级栏,其他图像用于识别未选择的RatingBar
我需要添加我的解决方案,而不是上面的解决方案.我们甚至不需要使用样式.
在drawable文件夹中创建一个选择器文件:
custom_ratingbar_selector.xml
在布局中将选择器文件设置为progressDrawable:
这就是我们所需要的一切.
首先将图像添加到drawable:
第一张图片"ratingbar_staroff.png"和第二张"ratingbar_staron.png"
之后,在res/drawable上创建"ratingbar.xml"
res/drawable上的下一个xml相同
"ratingbar_empty.xml"
"ratingbar_filled"
接下来要在res/values/styles上添加这些代码行
现在,已经可以为ratingbar资源添加样式了
最后你的活动才宣布:
RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingbar); ratingbar.setRating(3.67f);
使用图层列表和选择器创建自定义等级栏很复杂,最好覆盖RatingBar类并创建自定义RatingBar。createBackgroundDrawableShape()是应该将空状态png放置在其中的函数,createProgressDrawableShape()是应该将填充状态png放置在其中的函数。
注意:该代码目前不适用于svg。
public class CustomRatingBar extends RatingBar { @Nullable private Bitmap mSampleTile; public ShapeDrawableRatingBar(final Context context, final AttributeSet attrs) { super(context, attrs); setProgressDrawable(createProgressDrawable()); } @Override protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mSampleTile != null) { final int width = mSampleTile.getWidth() * getNumStars(); 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.secondaryProgress); layerDrawable.setId(2, android.R.id.progress); return layerDrawable; } protected Drawable createBackgroundDrawableShape() { final Bitmap tileBitmap = drawableToBitmap(getResources().getDrawable(R.drawable.ic_star_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_star_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; } }
您可以使用更好的动画来尝试此评级栏
SmileyRating