android ratingbar 高度,ShapeDrawable作为Android中RatingBar的progressDrawab...

该博客介绍了如何通过自定义@ChrisJenkins方法来创建一个完全可定制的RatingBar,允许设置图标大小、色调颜色以及在XML中设置参数。通过创建LayerDrawable和使用BitmapShader,实现了RatingBar的背景和进度条的自定义样式。
摘要由CSDN通过智能技术生成

我自定义了@ChrisJenkins方法,因此您可以设置RatingBar图标的大小和评级图像的自定义色调颜色.您可以在RatingBar上设置填充或设置自定义宽度和高度. @ChrisJenkins解决方案帮助我最终想出了一个完全可定制的RatingBar.您也可以在XML中设置参数.

public class DrawableRatingBar extends RatingBar

{

// TileBitmap to base the width and hight off of.

@Nullable

private Bitmap iconTile;

private float scaleIconFactor;

private @ColorInt int iconBackgroundColor;

private @ColorInt int iconForegroundColor;

private @DrawableRes int iconDrawable;

public DrawableRatingBar(Context context)

{

super(context);

init();

}

public DrawableRatingBar(Context context, AttributeSet attrs)

{

super(context, attrs);

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DrawableRatingBarItem);

scaleIconFactor = a.getFloat(R.styleable.DrawableRatingBarItem_scaleIconFactor, 1);

iconBackgroundColor = a.getColor(R.styleable.DrawableRatingBarItem_iconBackgroundColor, Color.BLACK);

iconForegroundColor = a.getColor(R.styleable.DrawableRatingBarItem_iconForegroundColor, Color.WHITE);

iconDrawable = a.getResourceId(R.styleable.DrawableRatingBarItem_iconDrawable, -1);

a.recycle();

init();

}

public DrawableRatingBar(Context context, AttributeSet attrs, int defStyleAttr)

{

super(context, attrs, defStyleAttr);

init();

}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

public DrawableRatingBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

{

super(context, attrs, defStyleAttr, defStyleRes);

init();

}

private void init()

{

setProgressDrawable(createProgressDrawable());

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

{

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

if (iconTile != null)

{

final int width = iconTile.getWidth() * getNumStars();

final int height = iconTile.getHeight();

setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, 0),

resolveSizeAndState(height, heightMeasureSpec, 0));

}

}

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()

{

Drawable drawable = ContextCompat.getDrawable(getContext(), iconDrawable);

final Bitmap tileBitmap = scaleImageWithColor(drawable, scaleIconFactor, iconBackgroundColor);

if (iconTile == null)

{

iconTile = 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;

}

private void setRatingStarColor(Drawable drawable, @ColorInt int color)

{

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)

{

DrawableCompat.setTint(drawable, color);

}

else

{

drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);

}

}

protected Drawable createProgressDrawableShape()

{

Drawable drawable = ContextCompat.getDrawable(getContext(), iconDrawable);

final Bitmap tileBitmap = scaleImageWithColor(drawable, scaleIconFactor, iconForegroundColor);

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()

{

return new RectShape();

}

public Bitmap scaleImageWithColor(Drawable drawable, float scaleFactor, @ColorInt int color)

{

Bitmap b = ((BitmapDrawable) drawable).getBitmap();

int sizeX = Math.round(drawable.getIntrinsicWidth() * scaleFactor);

int sizeY = Math.round(drawable.getIntrinsicHeight() * scaleFactor);

Bitmap bitmapResized = Bitmap.createScaledBitmap(b, sizeX, sizeY, true);

Canvas canvas = new Canvas(bitmapResized);

Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);

paint.setAntiAlias(true);

ColorFilter filter = new LightingColorFilter(color, 1);

paint.setColorFilter(filter);

canvas.drawBitmap(bitmapResized, 0, 0, paint);

return bitmapResized;

}

protected Bitmap fromDrawable(final Drawable drawable, final int height, final int width) {

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 float getScaleIconFactor() {

return scaleIconFactor;

}

public void setScaleIconFactor(float scaleIconFactor) {

this.scaleIconFactor = scaleIconFactor;

}

public int getIconForegroundColor() {

return iconForegroundColor;

}

public void setIconForegroundColor(int iconForegroundColor) {

this.iconForegroundColor = iconForegroundColor;

}

public int getIconBackgroundColor() {

return iconBackgroundColor;

}

public void setIconBackgroundColor(int iconBackgroundColor) {

this.iconBackgroundColor = iconBackgroundColor;

}

public int getIconDrawable() {

return iconDrawable;

}

public void setIconDrawable(int iconDrawable) {

this.iconDrawable = iconDrawable;

}

}

自定义属性:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值