Android Self-study drag strip Seekbar

Source: Internet
Author: User

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:

 

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.