ratingBar 星级评分条

时间:2024-11-15 14:34:25

ratingBar 星级评分条

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="240dp"
android:src="@drawable/lijiang"/>
<!-- 定义一个星级评分条 -->
<RatingBar
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:max="255"
android:progress="255"
android:stepSize="0.5"/>
</LinearLayout>

主界面

 val image = findViewById<ImageView>(R.id.image)
val ratingBar = findViewById<RatingBar>(R.id.rating)
ratingBar.onRatingBarChangeListener = RatingBar.OnRatingBarChangeListener { ratingBar, rating, fromUser ->
// 当星级评分条的评分发生改变时触发该方法
// 动态改变图片的透明度,其中255是星级评分条的最大值
// 5个星星就代表最大值255
image.imageAlpha = (rating * 255 / 5).toInt()
print(rating)
}

主程序