The drag bar (SeekBar) is very similar to the progress bar, except that the progress bar uses a color fill to indicate how far the progress has been completed, while the drag bar identifies the value by the position of the slider----and the drag bar allows the user to drag the slider to change the values, so the drag bar is often used to adjust some of the system's values. such as volume adjustment.
Seekbar allows the user to change the slider appearance of the drag bar, changing the appearance of the slider to specify by the following properties
ANDROID:THUMB Specifies a drawable object that will act as the custom slider.
In order for the program to respond to changes in the position of the drag bar slider, the program can consider binding a Onseekbarchangerlistener listener for him.
Through an example: change the transparency of a picture by sliding the block to see Seekbar.
Layout/main.xml
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 android:orientation= "vertical"4 Android:layout_width= "Fill_parent"5 Android:layout_height= "Fill_parent"6 >7 <ImageView8 Android:id= "@+id/image"9 Android:layout_width= "Fill_parent" Ten Android:layout_height= "240px" One android:src= "@drawable/lijiang" A /> - <!--define a drag bar and change its slider appearance - - <SeekBar the Android:id= "@+id/seekbar" - Android:layout_width= "Fill_parent" - Android:layout_height= "Wrap_content" - Android:max= "255" + android:progress= "255" - Android:thumb= "@drawable/ic_launcher" + /> A </LinearLayout>
Two components are defined in the interface layout code above: Imageview,seekbar;seekbar thumb I used the app icon to make a slider, let's look at the main program inside the slider to add the event listener
Main program: Com.example.seekbar.SeekBarTest.java
1 PackageCom.example.seekbar;2 3 Importandroid.support.v7.app.ActionBarActivity;4 ImportAndroid.support.v7.app.ActionBar;5 Importandroid.support.v4.app.Fragment;6 ImportAndroid.os.Bundle;7 ImportAndroid.view.LayoutInflater;8 ImportAndroid.view.Menu;9 ImportAndroid.view.MenuItem;Ten ImportAndroid.view.View; One ImportAndroid.view.ViewGroup; A ImportAndroid.widget.ImageView; - ImportAndroid.widget.SeekBar; - ImportAndroid.widget.SeekBar.OnSeekBarChangeListener; the ImportAndroid.os.Build; - - Public classSeekbartestextendsactionbaractivity { - + @Override - protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.main); at - FinalImageView image =(ImageView) Findviewbyid (r.id.image); -SeekBar SeekBar =(SeekBar) Findviewbyid (R.id.seekbar); - -Seekbar.setonseekbarchangelistener (NewOnseekbarchangelistener () { - in @Override - Public voidOnstoptrackingtouch (SeekBar SeekBar) { to } + - @Override the Public voidOnstarttrackingtouch (SeekBar SeekBar) { * } $ Panax Notoginseng //This method is triggered when the slider position of the drag bar changes - @Override the Public voidOnprogresschanged (SeekBar arg0,intArg1,Booleanarg2) { + Image.setalpha (arg1); A } the }); + } -}
After the run:
Effect after pulling after sliding block: