添加一个容器,以容纳具有默认图像的五个图像视图。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/start_ratting"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:scaleType="fitCenter"
android:src="@drawable/empty"
android:id="@+id/star_1"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:src="@drawable/empty"
android:scaleType="fitCenter"
android:id="@+id/star_2"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:scaleType="fitCenter"
android:src="@drawable/empty"
android:id="@+id/star_3"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:scaleType="fitCenter"
android:src="@drawable/empty"
android:id="@+id/star_4"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/empty"
android:background="@android:color/transparent"
android:scaleType="fitCenter"
android:id="@+id/star_5"/>
</LinearLayout>
然后再加上动画师
private fun expandHorizontal(v: ImageView, image: Drawable) {
val width: Int = v.width
v.visibility = View.VISIBLE
v.layoutParams.width = 0
v.setImageDrawable(image)
val valueAnimator = ValueAnimator.ofInt(0, width)
valueAnimator.addUpdateListener { animation ->
v.layoutParams.width = animation.animatedValue as Int
v.requestLayout()
}
valueAnimator.interpolator = DecelerateInterpolator()
valueAnimator.duration = VIEW_TRANSITION_TIME
valueAnimator.start()
}
在需要时调用设置星号值
private fun setStarValue(ratting: Double){
star1.visibility = View.INVISIBLE
star2.visibility = View.INVISIBLE
star3.visibility = View.INVISIBLE
star4.visibility = View.INVISIBLE
star5.visibility = View.INVISIBLE
var i = 0
for(i in 0..5){
when(i){
0->{
val res = ratting - i;
if(res >= 1){
Log.d(TAG, "start 1")
getDrawable(R.drawable.full)?.let { expandHorizontal(star1, it) }
}
else if(res > 0 && res < 1){
Log.d(TAG, "start half")
getDrawable(R.drawable.half)?.let { expandHorizontal(star1, it) }
}
else{
Log.d(TAG, "no start")
getDrawable(R.drawable.empty)?.let { expandHorizontal(star1, it) }
}
}
1 -> {
val res = ratting - i;
if(res >= 1){
Log.d(TAG, "start 2")
getDrawable(R.drawable.full)?.let { expandHorizontal(star2, it) }
}
else if(res > 0 && res < 1){
Log.d(TAG, "start two and half")
getDrawable(R.drawable.half)?.let { expandHorizontal(star2, it) }
}
else{
Log.d(TAG, "no start")
getDrawable(R.drawable.empty)?.let { expandHorizontal(star2, it) }
}
}
2->{
val res = ratting - i;
if(res >= 1){
Log.d(TAG, "start 3")
getDrawable(R.drawable.full)?.let { expandHorizontal(star3, it) }
}
else if(res > 0 && res < 1){
Log.d(TAG, "start three and half")
getDrawable(R.drawable.half)?.let { expandHorizontal(star3, it) }
}
else{
Log.d(TAG, "no start")
getDrawable(R.drawable.empty)?.let { expandHorizontal(star3, it) }
}
}
3 -> {
val res = ratting - i;
if(res >= 1){
Log.d(TAG, "start 4")
getDrawable(R.drawable.full)?.let { expandHorizontal(star4, it) }
}
else if(res > 0 && res < 1){
Log.d(TAG, "start 4 and half")
getDrawable(R.drawable.half)?.let { expandHorizontal(star4, it) }
}
else{
Log.d(TAG, "no start")
getDrawable(R.drawable.empty)?.let { expandHorizontal(star4, it) }
}
}
4 ->{
val res = ratting - i;
if(res >= 1){
Log.d(TAG, "start 5")
getDrawable(R.drawable.full)?.let { expandHorizontal(star5, it) }
}
else if(res > 0 && res < 1){
Log.d(TAG, "start three and half")
getDrawable(R.drawable.half)?.let { expandHorizontal(star5, it) }
}
else{
Log.d(TAG, "no start")
getDrawable(R.drawable.empty)?.let { expandHorizontal(star5, it) }
}
}
}
}
}
可以选择水平方向的动画