In Android, the seekbar drags the progress bar to adjust the transparency and the ratingbar star rating

In the previous issue, I briefly explained the loading case of the Android advanced UI component progressbar. Today, I will briefly introduce the seekbar to drag the progress bar to adjust the transparency of the picture and the common star-striped comment effect.

The adjustment of transparency and the vocabulary of five-star praise are common in our daily life, so how to realize it in the software, today Xiong Xiaobai will take you to understand it briefly.

For the code I posted below, I implemented the progress bar to drag the progress bar to adjust the transparency of the picture, and click different numbers of stars to display the score at the specified position on the right of the star

Drag the progress bar to adjust the transparency to inherit the progressbar, to achieve transparency is to use the rewritten onProgressChanged in the seekbar monitoring method to add setimageviewalpha (progress) because there are three rewritten methods of the seekbar, and the other two onStartTrackingTouch and onStopTrackingTouch are for start and onStopTrackingTouch The behavior result of stopping the touch, this function does not need to add any code, but in the later stage of the music player, sliding the progress bar to change the progress of the music needs to write the relevant Java code in onstoptrackingtouch

The Stars and Stripes comment introduces the rating control. You have already obtained the number of stars in the listening event of the rating bar. You only need to place the obtained stars. In addition, I have posted some related attributes of the rating in the xml below for your reference. refer to

android:isIndicator : whether it is used as an indicator, the user cannot change it, the default is false
android:numStars : how many stars to display, must be an integer
android:rating : the default rating value, must be a floating point number
android:stepSize:  the value that the rating increases each time, Must be a floating point number

code:

public class MainActivity extends AppCompatActivity {
private SeekBar seekBar;
private ImageView imageView;
private RatingBar ratingBar;
private TextView txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView=findViewById(R.id.image);
        seekBar=findViewById(R.id.seekbar);
        ratingBar=findViewById(R.id.ratingbar);
        txt=findViewById(R.id.txt);
       seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                imageView.setImageAlpha(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
//        String rating= String.valueOf(ratingBar.getRating());
//        txt.setText(rating);
        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                //Toast.makeText(MainActivity.this, "rating:" + String.valueOf(rating), Toast.LENGTH_SHORT).show();
                txt.setText(String.valueOf(rating));
            }
        });

    }
}

 

Guess you like

Origin blog.csdn.net/Abtxr/article/details/125847051