下创建一个帮助类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>