本文主要是介绍利用ClipDrawable显示评分的view,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
ScoreView
利用ClipDrawable显示评分的view.同样类似的可以用在动态显示麦克风音量,只需要修改背景前景图,clipOrientation和gravity。
新建Drawable的xml:
<?xml version="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/score_normal1" /><item android:id="@id/score_clip"><clip
android:clipOrientation="horizontal"android:drawable="@drawable/score_select"android:gravity="left" /></item>
</layer-list>
新建attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="ScoreView"><attr name="text_color" format="color"></attr><attr name="text_size" format="float"></attr><attr name="max_score" format="float"></attr><attr name="src_drawable" format="reference"></attr></declare-styleable>
</resources>
public class ScoreView extends LinearLayout implements Level {private Context mContext;private ImageView scoreClip;private TextView tvScore;private float maxScore = 5.0f;private float textSize;//單位spprivate int textColor;private int resourceId;public ScoreView(Context context) {this(context, null);}public ScoreView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public ScoreView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mContext = context;setHorizontalGravity(HORIZONTAL);TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScoreView);maxScore = typedArray.getFloat(R.styleable.ScoreView_max_score, 5.0f);textColor = typedArray.getColor(R.styleable.ScoreView_text_color, Color.BLACK);textSize = typedArray.getFloat(R.styleable.ScoreView_text_size, 14);resourceId = typedArray.getResourceId(R.styleable.ScoreView_src_drawable, R.drawable.clip_drawable2);typedArray.recycle();LinearLayout scoreView = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.score_view, null);scoreClip = (ImageView) scoreView.findViewById(R.id.score_clip);tvScore = (TextView) scoreView.findViewById(R.id.tv_score);scoreClip.setImageResource(resourceId);tvScore.setTextSize(textSize);tvScore.setTextColor(textColor);addView(scoreView);}@Overridepublic void setPercent(float percent) {percent = percent > 1.0f ? 1.0f : percent;percent = percent < 0.0f ? 0.0f : percent;String result = String.format("%.1f", maxScore * percent);tvScore.setText(result);scoreClip.getDrawable().setLevel((int) (percent * 10000));}
}
使用:
<comulez.github.scoreview.ScoreView
android:id="@+id/sv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/test_clip"app:max_score="5"app:src_drawable="@drawable/clip_drawable3"app:text_color="@color/colorAccent"app:text_size="12" />
源码地址:https://github.com/Ulez/ScoreView
这篇关于利用ClipDrawable显示评分的view的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!