Android learning --- SeekBar, RatingBar, and androidratingbar

Source: Internet
Author: User

Android learning --- SeekBar, RatingBar, and androidratingbar

SeekBar: The drag bar is similar to the scroll bar. You can drag the slider to change the progress.

RatingBar star rating bar: The star rating bar is similar to the drag bar


Xml attributes specific to SeekBar

Android: thumb specifies an Arawable object as its definition slider.



Xml attributes specific to RatingBar

Android: whether isIndicator allows user change (true indicates that modification is not allowed)

Android: How many stars does numStars have?

Android: default rating star rating

Android: stepSize: Minimum number of stars changed each time



To know the change progress, we need to add listeners.

Listening OnSeekBarChangeListener for SeekBar

Listener OnRatingBarChangeListener for RatingBar


Below we will familiarize ourselves with them through instances

(1) Compile the layout file activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/linearLayout"     android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">        <ImageView         android:id="@+id/imgView1"        android:layout_width="120dp"        android:layout_height="100dp"        android:src="@drawable/ic_launcher"/>    <SeekBar         android:id="@+id/seekBar"        android:layout_width="180dp"        android:layout_height="wrap_content"        android:thumb="@drawable/ic_launcher"        android:max="255"        android:progress="255"/>      <ImageView         android:id="@+id/imgView2"        android:layout_width="120dp"        android:layout_height="100dp"        android:src="@drawable/img01"/>      <RatingBar           android:id="@+id/reatingBar"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:max="255"          android:progress="255"          android:rating="5"          android:stepSize="0.5"          android:numStars="5"/>    </LinearLayout>

(2) Compile MainActivity. java

Package com. example. bar; import android. annotation. targetApi; import android. app. activity; import android. OS. build; import android. OS. bundle; import android. util. log; import android. view. menu; import android. widget. imageView; import android. widget. ratingBar; import android. widget. ratingBar. onRatingBarChangeListener; import android. widget. seekBar; import android. widget. seekBar. onSeekBarChangeListener; @ TargetApi (Build. extends) public class MainActivity extends Activity {private ImageView imgViewSB = null; // defines ImageViewprivate ImageView imgViewRB = null; private SeekBar seekBar = null; // defines SeekBarprivate RatingBar ratingBar = null; // define RatingBar @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // layout component this. imgViewSB = (ImageView) findViewById (R. id. imgView1); this. imgViewRB = (ImageView) findViewById (R. id. imgView2); this. seekBar = (SeekBar) findViewById (R. id. seekBar); this. ratingBar = (RatingBar) findViewById (R. id. reatingBar); // seekBar sets the listener to change the image transparency. this. seekBar. setOnSeekBarChangeListener (new OnSeekBarChangeListener () {@ Overridepublic void onStopTrackingTouch (SeekBar seekBar) {Log. I ("onStopTrackingTouch", "Method for triggering stop dragging");} @ Overridepublic void onStartTrackingTouch (SeekBar seekBar) {Log. I ("onStartTrackingTouch", "Method for triggering start dragging");}/*** seekBar: SeekBar object * progress: progress of the drag bar * fromUser: whether the user manually changes */@ Overridepublic void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {imgViewSB. setAlpha (progress); // sets the image transparency Log. I ("onProgressChanged", "method triggered when the slider position is changed by dragging") ;}}); // ratingBar sets the listener to change the image transparency this. ratingBar. listener (new OnRatingBarChangeListener () {/*** ratingBar: RatingBar object * rating: Star size * fromUser: whether the user manually changes */@ Overridepublic void onRatingChanged (RatingBar ratingBar, float rating, boolean fromUser) {imgViewRB. setAlpha (int) (rating * 255/5) ;}}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}

After the permission is granted, the effect is as follows:



Help: How to vertically display the seekbar in Android

After you repeat the SeekBar, do you use SlideBar or new in the layout file? Why don't I use it?
 
How to Use SeekBar in android to control the video playback progress

Int position = mediaPlayer. getCurrentPosition ();
Int total = mediaPlayer. getDuration ();
Int max = seekBar. getMax ();
If (position> = 0 & total! = 0 ){
SeekBar. setProgress (position * max/total );
}
This is when the progress bar is updated during playback, and the drag video is similar to this. Do not ask questions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.