利用ClipDrawable显示评分的view

本文介绍了一种基于 ClipDrawable 的 ScoreView 实现方案,可用于动态显示评分或音量等场景。通过自定义 View 和 Drawable,结合 XML 属性设置,实现了灵活且易于定制的评分组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;//單位sp
    private 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);
    }

    @Override
    public 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值