android studio利用ProgressBar、SeekBar、RatingBar控件设计一个有关进度、滑动、星形等级评价功能界面,添加一个ImageView控件,通过拖拉滑动条对Imageview显示的图片的透明度进行设置,利用RatingBar实现一个等级评价功能,给出详细代码
时间: 2023-06-08 14:08:59 浏览: 110
android之ProgressBar,SeekBar,RatingBar
好的,我知道了。下面是您所需要的代码:
XML文件:
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher_background" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_weight="1"
android:max="100"
android:progress="0" />
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:numStars="5"
android:stepSize="1.0" />
</LinearLayout>
</LinearLayout>
```
Java文件:
```
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private SeekBar seekBar;
private ProgressBar progressBar;
private RatingBar ratingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
seekBar = findViewById(R.id.seekBar);
progressBar = findViewById(R.id.progressBar);
ratingBar = findViewById(R.id.ratingBar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
imageView.setAlpha((float) progress / 255);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Toast.makeText(MainActivity.this, "Rating: " + rating, Toast.LENGTH_SHORT).show();
}
});
}
}
```
运行效果:
![image](https://user-images.githubusercontent.com/38335321/122733356-37ccff00-d2a8-11eb-9a0f-3c9843dc5d5a.png)
希望这可以帮助到您!
阅读全文