Android 17 progress bar and Hanlder

Video lesson: https://edu.csdn.net/course/play/7621

Build progress bar

Use ProgressBar label layout view in XML layout file

<ProgressBar

        android:id="@+id/progressbar"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        style="@android:style/Widget.ProgressBar.Horizontal"

        />

Use android.widget.ProgressBar class to manipulate view in Java code

ProgressBar  bar = (ProgressBar)findViewById(R.id.progressbar);

//Set the value range of the progress bar 0~n

bar.setMax(10);


Use SeekBar label layout view in XML layout file

<SeekBar

        android:id="@+id/seekbar"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        />


Use android.widget.SeekBar class to manipulate view in Java code

SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar);

seekBar.setMax(10);

seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()  

        {

                @Override

                public void onProgressChanged(SeekBar seekBar, int progress,

boolean fromUser) {

                        //Fill in the code to handle the event here

                }

                @Override

                public void onStartTrackingTouch(SeekBar seekBar) {}

                @Override

                public void onStopTrackingTouch(SeekBar seekBar) {}

});

Build a rating view

Use RatingBar tag in XML layout file

<RatingBar

        android:id="@+id/ratingbar"

        android:layout_width=“wrap_content"

        android:layout_height="wrap_content” />


The event monitoring interface is android.widget.RatingBar.OnRatingBarChangeListener, and the android.widget.RatingBar class is used in the Java code

.RatingBar  bar = (RatingBar)findViewById(R.id.ratingbar);

//Set the number of stars displayed in the rating view

bar.setNumStars (5);

bar.setMax(10);



Introduction to Handler

Handler related knowledge introduces
the main thread of the Android application. When the application starts, Android will start a main thread responsible for interface event processing. When the interface event needs to be processed for a long time, the task should be placed in a child thread to run the
child thread The interface should not be modified, because the main thread is in charge. If it is modified, the thread is not safe. Handler is set up to solve the communication between the child thread and the main thread. The Handler object created when the main thread is running
will automatically be bound to the main thread. The thread will continue to process the data processing requirements in the Handler during the message loop. The Handler provides a variety of methods for providing data transfer methods to the child threads:
post(), postAtTime(), postDelay()
sendMessage(), sendMessageAtTime(), sendMessageDelay()


realize Taobao automatically play animation steps:
1. Copy the animation to be played to the drawable directory
2. Add an ImageView to the layout file to display the picture
3. Create a Handler in the main thread to handle the picture replacement action
4 . Create a Timer (Timer itself will start a child thread) to send messages to the

main view Activity regularly . The picture resource code is as follows:

//ImageView object, used to display pictures

private ImageView image;

//Define an array to store image resources

private int[] images = {

R.drawable.image1,

R.drawable.image2,

R.drawable.image3,

R.drawable.image4,

R.drawable.image5,

};

int index = 0;//Image resource index


Define the Handler code as follows:

Handler handler = new Handler () {

//Processing the message

@Override

public void handleMessage(Message msg) {

//If it is a message sent by this thread

if(msg.what==1)

{

//Modify the picture dynamically

image.setImageResource(images[index++%images.length]);

}

super.handleMessage(msg);

}

};

 


The main view Activity, onCreate() method code is as follows:

protected void onCreate(android.os.Bundle savedInstanceState) {

super.onCreate (savedInstanceState);

setContentView(R.layout.handlertest);

image = (ImageView) findViewById(R.id.image);

//Create a timer with a period of 2 seconds and create an execution task

new Timer().schedule(new TimerTask() {

@Override

public void run() {

//Send an empty message with what is 1

handler.sendEmptyMessage (1);

}

},0,2000);

}

 


to sum up

In the above code, the Timer is used to periodically execute the specified task, and the Timer object can schedule the TimerTask object. The essence of the TimerTask object is to start a new thread. Because Android does not allow other threads to modify the UI interface in the Activity (because it is not safe), The new thread can only send one message to notify the main thread to update the interface.
The role of the Handler is to establish the bond between the main thread and the child thread. The child thread sends a message through the Handler object and returns it to the Handler, so that the main thread can process the message through the Handler.

Handler mechanism principle analysis

The following components need to be used in the Handler work:
Message: Message object received and processed by the Handler
Looper: Read the Message object in the MessageQueue continuously (infinite loop), and if the Message is read, the Message will be passed to the Handler that sent the message for processing. There is only one Looper object
MessageQueue per thread : Message queue, which manages Message objects in a first-in, first-out manner. When the program creates the Looper object, it creates the MessageQueue object

Handler in its constructor. There are two main functions:
1. In the newly started Send messages
in threads (sub-threads) 2. Obtain and process messages in the main thread.
Sub-threads use Handler to send messages (Message). The sent messages must be sent to the specified message queue (MessageQueue, managed by Looper, and Looper is managed by sub-threads) Create by yourself); the main thread uses an infinite loop to continuously take out the messages in the MessageQueue through the looper() method of the Looper object created by the system, and hand them over to the Handler for processing, thereby realizing the change of the view in the main thread

Modify the text information in Activity through the Handler message passing mechanism

//Create Handler

Handler handler = new Handler () {

//Processing the message

public void handleMessage(android.os.Message msg) {

if(msg.what==1)

{

txt.setText("update handler");

}

};

};

/Start a new thread to send a message
new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Thread.sleep(3000);//Thread sleeps for 3 seconds
            handler.sendEmptyMessage(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}).start();

 


Guess you like

Origin blog.51cto.com/2096101/2588789