男女RatingBar

来张效果图
下创建一个帮助类AnimUtils

public class AnimUtils {
    /**
     * 假设总人数为417,男性为360
     *
     * @param ratingBar
     * @param percentTxt
     * @param numTxt
     */
    private static float count;
    public static void progressAnim(final RatingBar ratingBar[], final TextView percentTxt[], final TextView numTxt[]) {
        ValueAnimator valueAnimator = new ValueAnimator();
        count = 318000;
        //设置 value 的变化范围
        valueAnimator.setObjectValues(new PointF(0,0), new PointF(219000, 100000));
        //设置变化状态,线性变化
        valueAnimator.setInterpolator(new LinearInterpolator());
        //设置估值器,需要根据需求来实现
        valueAnimator.setEvaluator(new TypeEvaluator<PointF>() {
            @Override
            public PointF evaluate(float fraction, PointF startValue, PointF endValue) {

                float x =  (startValue.x + fraction * (endValue.x - startValue.x));
                float y =  (startValue.x + fraction * (endValue.y - startValue.y));
                return new PointF(x, y);
            }
        });
//        监听值的变化,从而设置值
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF value = (PointF) animation.getAnimatedValue();

                float percentX = (value.x * 100f / count);
                float percentY = (value.y * 100f / count);
                ratingBar[0].setProgress((int) (percentX+.5));
                percentTxt[0].setText((int) (percentX+.5) + "%");
                numTxt[0].setText(String.format("男%s人", (int)value.x));
                ratingBar[1].setProgress((int) (percentY+.5));
                percentTxt[1].setText((int) (percentY+.5) + "%");
                numTxt[1].setText(String.format("女%s人", (int)value.y));
            }
        });
        valueAnimator.setDuration(5000);
        valueAnimator.start();
    }
}

Activity代码

public class MainActivity extends Activity {
    private RatingBar mRatingBar[] = new RatingBar[2];
    private TextView mPercentTxt[] = new TextView[2];
    private TextView mNumTxt[] = new TextView[2];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRatingBar[0] = (RatingBar) findViewById(R.id.id_rating);
        mRatingBar[0].setRating(0.1f);  //默认的评分,必须是浮点类型,像“1.2”。
        mRatingBar[0].setNumStars(10);  //显示的星型数量,必须是一个整形值,像“100”。
        mRatingBar[0].setStepSize(0.1f);    //评分的步长,必须是浮点类型,像“1.2”。
        mPercentTxt[0] = (TextView) findViewById(R.id.id_text_percent);
        mNumTxt[0] = (TextView) findViewById(R.id.id_text_num);
        mRatingBar[1] = (RatingBar) findViewById(R.id.id_rating_nv);
        mRatingBar[1].setRating(0.1f);
        mRatingBar[1].setNumStars(10);
        mRatingBar[1].setStepSize(0.1f);
        mPercentTxt[1] = (TextView) findViewById(R.id.id_text_percent_nv);
        mNumTxt[1] = (TextView) findViewById(R.id.id_text_num_nv);
        /*开始执行动画*/
        AnimUtils.progressAnim(mRatingBar, mPercentTxt, mNumTxt);
    }

}

activity_main.xml代码

<RatingBar
        android:layout_marginTop="80dp"
        android:id="@+id/id_rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:progressDrawable="@drawable/five_rating_bar"
        />
    <TextView
        android:id="@+id/id_text_percent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:layout_marginLeft="30dp"
        android:textColor="@color/text_light_black4"/>
    <TextView
        android:id="@+id/id_text_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:layout_marginLeft="30dp"
        android:textColor="@color/text_light_black4"/>
    <RatingBar
        android:id="@+id/id_rating_nv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:progressDrawable="@drawable/five_rating_bar" />
    <TextView
        android:id="@+id/id_text_percent_nv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:layout_marginLeft="30dp"
        android:textColor="@color/text_light_black4"
        />
    <TextView
        android:id="@+id/id_text_num_nv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:layout_marginLeft="30dp"
        android:textColor="@color/text_light_black4"/>

five_rating_bar.xml代码

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@android:id/background"
        android:drawable="@mipmap/img2" />
    <item
        android:id="@android:id/secondaryProgress"
        android:drawable="@mipmap/img2" />
    <item
        android:id="@android:id/progress"
        android:drawable="@mipmap/img1" />

</layer-list>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值