自定义RatingBar
程序员文章站
2022-05-30 22:42:45
...
先看效果(下图里的用户的等级小星星):
这里本来可以修改ratingbar,使用自定义样式,即修改ratingbar图片来实现自定义ratingbar目的,但是这样图片的大小就不好控件,只能是图片给多大的显示多大,很无奈,我自己写了一个较灵活使用的ratingBar:
package com.gtuu.gzq.customview;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.gtuu.gzq.R;
/**
* 自定义rating bar
*
* @author chenwenbiao
* @date 2014-5-3
* @version
*/
@SuppressLint("NewApi")
public class MyRatingBar extends LinearLayout{
private float totalWidth;
private float totalHeight;
private int margin;
private int startNumber;
private int rating;//等级
private int ratingInResourceId;//相当于选中时的资源图
private int ratingOutResourceId;//相当于非选中时的资源图
private ImageView imageView;
private TypedArray mTypedArray;
private Context context = null;
public MyRatingBar(Context context) {
this(context, null);
}
public MyRatingBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyRatingBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
mTypedArray = context.obtainStyledAttributes(attrs,
R.styleable.MyRatingBar);
//获取自定义属性和默认值
totalWidth = mTypedArray.getDimension(R.styleable.MyRatingBar_total_width, 40);
totalHeight = mTypedArray.getDimension(R.styleable.MyRatingBar_total_height, 10);
margin = (int) mTypedArray.getDimension(R.styleable.MyRatingBar_margin, 2);
startNumber = mTypedArray.getInt(R.styleable.MyRatingBar_start_number, 0);
rating = mTypedArray.getInt(R.styleable.MyRatingBar_rating, 0);
ratingInResourceId = mTypedArray.getResourceId(R.styleable.MyRatingBar_rating_in_bg, -1);
ratingOutResourceId = mTypedArray.getResourceId(R.styleable.MyRatingBar_rating_out_bg, -1);
mTypedArray.recycle();
if(startNumber > 0){
initialsView();
}
}
private void initialsView(){
/**
* 生成startNumber个自定义图片
*/
for(int i = 0 ; i < startNumber ; i++){
ImageView imageView = new ImageView(context, null);//存放自定义图片,如五角星的图片控件
if(i < rating){
imageView.setImageResource(ratingInResourceId);//设置等级内的图片
}
else {
imageView.setImageResource(ratingOutResourceId);//等级外的图片
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int)(totalWidth / startNumber), (int)totalHeight);//设计每一个自定义图片的大小
layoutParams.setMargins(margin, 0, margin, 0);
addView(imageView,layoutParams);
}
}
//设置星星的个数
public void setRating(int level){
//防止level超出星星个数
if( level > startNumber){
level = startNumber;
}
for(int i = 0 ; i < startNumber ; i++){
ImageView subView = (ImageView) getChildAt(i);
if(i < level){
subView.setImageResource(ratingInResourceId);//设置等级内的图片
}else{
subView.setImageResource(ratingOutResourceId);//等级外的图片
}
}
invalidate();
}
}
使用简单:
<com.vvpinche.view.MyRatingBar android:layout_width="80dp" android:layout_height="20dp" ptr:total_width="160dp" ptr:total_height="20dp" ptr:margin="2dp" ptr:start_number="6" ptr:rating="3" ptr:rating_in_bg="@drawable/comment_star" ptr:rating_out_bg="@drawable/comment_star_no_select" />
value文件夹下加入attrs.xml:
<?xml version="1.0" encoding="UTF-8"?> <resources> <!-- 等级自定义等级控件属性 --> <declare-styleable name="MyRatingBar"> <attr name="total_width" format="dimension" /> <attr name="total_height" format="dimension" /> <attr name="margin" format="dimension" /> <attr name="start_number" format="integer" /> <!-- 等级数 --> <attr name="rating" format="integer"></attr> <!-- 等级内图片 --> <attr name="rating_in_bg" format="reference"></attr> <!-- 等级外图片 --> <attr name="rating_out_bg" format="reference"></attr> </declare-styleable> </resources>
这里给的星星图片是比较大的,而结过使用上面自定义的ratingbar后,图片也相应小,符合使用。