ShapeDrawable作为progressDrawable为的RatingBar Android中? [英] ShapeDrawable as progressDrawable for RatingBar in Android?
问题描述
看来我无法设置ShapeDrawable作为progressDrawable为的RatingBar。我尝试以下,但失败:
<的RatingBar
机器人:ID =@ + ID /的RatingBar
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:numStars =5
机器人:stepSize的=1.0
机器人:等级=3.0
风格=@风格/ myRatingBar
/>
myRatingbar风格:
<样式名称=myRatingBar父=@安卓风格/ Widget.RatingBar>
<项目名称=机器人:progressDrawable> @绘制/ ratingbar_full< /项目>
<项目名称=机器人:indeterminateDrawable> @绘制/ ratingbar_full< /项目>
<项目名称=安卓了minHeight> 48dip< /项目>
<项目名称=安卓了maxHeight> 48dip< /项目>
<项目名称=机器人:scaleType>中心< /项目>
< /风格>
ratingbar_full.xml:
<形状
机器人:造型=环
机器人:innerRadius =10dip
机器人:厚度=1dip>
[固体机器人:色=@色/红/>
<大小
机器人:宽=48dip
机器人:身高=48dip
/>
< /形状>
没有显示在屏幕上。
编辑:使用png格式,而不是形状适用于这一行:
@绘制/ ratingbar_full
块引用>解决方案形状可绘制XML +的RatingBar一塌糊涂。
这是接近我能得到一个解决方案,而无需编写一个全新的类。
我的扩展类建立正确的进度绘制的
进度
夹紧它作为必需的。与我prepopulated的那些替换你的空和满的状态。此刻不是很灵活,你可以很容易地抽象的空/满星状态的设置。
/ **
*创建者克里斯·上28/08/2014。
*对于悠悠,Android系统。
* /
公共类ShapeDrawableRatingBar扩展的RatingBar {
/ **
* TileBitmap立足宽客。
* /
@Nullable
私人位图mSampleTile; 公共ShapeDrawableRatingBar(最终上下文的背景下,最终的AttributeSet ATTRS){
超(背景下,ATTRS);
setProgressDrawable(createProgressDrawable());
} @覆盖
保护同步无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
super.onMeasure(widthMeasureSpec,heightMeasureSpec); 如果(mSampleTile!= NULL){
最终诠释宽度= mSampleTile.getWidth()* getNumStars();
setMeasuredDimension(resolveSizeAndState(宽度,widthMeasureSpec,0),
getMeasuredHeight());
}
} 保护LayerDrawable createProgressDrawable(){
最终绘制对象backgroundDrawable = createBackgroundDrawableShape();
LayerDrawable layerDrawable =新LayerDrawable(新绘制对象[] {
backgroundDrawable,
backgroundDrawable,
createProgressDrawableShape()
});
layerDrawable.setId(0,android.R.id.background);
layerDrawable.setId(1,android.R.id.secondaryProgress);
layerDrawable.setId(2,android.R.id.progress);
返回layerDrawable;
} 保护可绘制createBackgroundDrawableShape(){
最终位图tileBitmap = drawableToBitmap(getResources()getDrawable(R.drawable.ic_stamp_circle_empty));
如果(mSampleTile == NULL){
mSampleTile = tileBitmap;
}
最后ShapeDrawable shapeDrawable =新ShapeDrawable(getDrawableShape());
最后BitmapShader bitmapShader =新BitmapShader(tileBitmap,Shader.TileMode.REPEAT,Shader.TileMode.CLAMP);
shapeDrawable.getPaint()setShader(bitmapShader)。
返回shapeDrawable;
} 保护可绘制createProgressDrawableShape(){
最终位图tileBitmap = drawableToBitmap(getResources()getDrawable(R.drawable.ic_stamp_circle_full));
最后ShapeDrawable shapeDrawable =新ShapeDrawable(getDrawableShape());
最后BitmapShader bitmapShader =新BitmapShader(tileBitmap,Shader.TileMode.REPEAT,Shader.TileMode.CLAMP);
shapeDrawable.getPaint()setShader(bitmapShader)。
返回新ClipDrawable(shapeDrawable,Gravity.LEFT,ClipDrawable.HORIZONTAL);
} 形状getDrawableShape(){
最终浮动[] = roundedCorners新的浮动[] {5,5,5,5,5,5,5,5};
返回新RoundRectShape(roundedCorners,NULL,NULL);
} 公共静态位图drawableToBitmap(绘制对象绘制){
如果(绘制的instanceof BitmapDrawable){
返回((BitmapDrawable)绘制).getBitmap();
} INT宽度= drawable.getIntrinsicWidth();
宽度=宽度GT; 0?宽度:1;
INT高度= drawable.getIntrinsicHeight();
高度=身高> 0?身高:1; 最终位图的位图= Bitmap.createBitmap(宽度,高度,Bitmap.Config.ARGB_8888);
最后的画布油画=新的Canvas(位图);
drawable.setBounds(0,0,canvas.getWidth(),canvas.getHeight());
drawable.draw(画布); 返回位图;
}}调用
SETMAX
setMaxStars
通话requestLayout
这样你就将正确测量宽度。无需制定出安卓了minWidth
,只需设置的android:layout_width =WRAP_CONTENT
请记住,您将需要一个比特填充添加到您的
ShapeDrawables
,因为他们得到反复边缘到边缘。It seems I cannot set ShapeDrawable as progressDrawable for Ratingbar. I tried the following but failed:
<RatingBar android:id="@+id/ratingbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:numStars="5" android:stepSize="1.0" android:rating="3.0" style="@style/myRatingBar" />
myRatingbar style:
<style name="myRatingBar" parent="@android:style/Widget.RatingBar"> <item name="android:progressDrawable">@drawable/ratingbar_full</item> <item name="android:indeterminateDrawable">@drawable/ratingbar_full</item> <item name="android:minHeight">48dip</item> <item name="android:maxHeight">48dip</item> <item name="android:scaleType">center</item> </style>
ratingbar_full.xml:
<shape android:shape="ring" android:innerRadius="10dip" android:thickness="1dip"> <solid android:color="@color/red" /> <size android:width="48dip" android:height="48dip" /> </shape>
Nothing shows on screen.
EDIT: use .png instead of shape works for this line:
@drawable/ratingbar_full
解决方案Shape XML Drawables + RatingBar a complete mess.
This is as close as I could get to a solution without writing a whole new class.
My extended class builds the progress drawable correctly for the
ProgressBar
clamping it as required.Replace your empty and full states with the ones I've prepopulated. Not very flexible at the moment, you could easily abstract the setting of empty/full star states.
/** * Created by chris on 28/08/2014. * For Yoyo-Android. */ public class ShapeDrawableRatingBar extends RatingBar { /** * TileBitmap to base the width off of. */ @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_stamp_circle_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_stamp_circle_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; } }
Calling
setMax
setMaxStars
callsrequestLayout
so you it will measure the width correctly. No need to work out theandroid:minWidth
, Just setandroid:layout_width="wrap_content"
.Just remember you will need to add a bit of padding to your
ShapeDrawables
as they get repeated edge to edge.这篇关于ShapeDrawable作为progressDrawable为的RatingBar Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!