<?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) }