Android custom view updates)

Source: Internet
Author: User

Android custom view updates)

Today, I encountered a very strange problem. For the LRC lyrics updated in the view, the view contains a member variable, lrcindex, which will be used in the draw to update the lyrics, there is a red line in the lyrics, indicating the currently playing lyrics. Then I dragged seekbar and updated the lyrics display. I updated the lyrics in onProgressChanged. The lyrics of other colors can be normally displayed, that is, the red lyrics or the sentence before the pause, but not updated, it's useless. I printed the lrcindex in draw. The value is not changed or the previous value is paused. But in fact I have modified lrcindex when another function obtains the current lyrics based on the current playback time. What is the problem?

I am working on a music player that dynamically updates the lyrics and progress bars.

Let's take a look at my code (only some of the key ones are extracted)

SeekBar. setOnSeekBarChangeListener (new OnSeekBarChangeListener () {@ Overridepublic void onStopTrackingTouch (SeekBar seekBar) {mp. seekTo (seekBar. getProgress (); // mp is the service for playing music. This method is used to set the mediaplayer progress} @ Overridepublic void onStartTrackingTouch (SeekBar seekBar) {// TODO Auto-generated method stubmp. stopUpdate (); // stop the automatic update progress bar time and lyrics} @ Overridepublic void onProgressChanged (SeekBar seekBar, int progress, boolean fro MUser) {// This is a callback function. The system will call this method as long as the value of seekbar changes. System. out. println (220-lyricView. selectIndex (seekBar. getProgress () * (lyricView. getSIZEWORD () + lyricView. getINTERVAL ()-1); System. out. println (lyricView. selectIndex (progress) + "---" + lyricView. getSIZEWORD () + "--" + (lyricView. getINTERVAL ()-1); lyricView. setOffsetY (220-lyricView. selectIndex (progress) * (lyricView. getSIZEWORD () + lyricView. getINTERVAL ()-1); lyricView. selectIndex (mp. getplayer (). getCurrentPosition (); lyricView. setLrcIndex (lyricView. selectIndex (progress); lyricView. invalidate (); // update view }}});}
Every time I update the progress of the lyrics, I will call invalidate to update the view, and the console also outputs the debugging information in draw. lrcindex is different from what I obtain in the seekbar value change listener.

Let's take a look at the selectIndex method.

public int SelectIndex(int time) {if (!blLrc) {return 0;}int index = 0;for (int i = 0; i < lrc_map.size(); i++) {LyricObject temp = lrc_map.get(i);if (temp.begintime <= time) {++index;}}lrcIndex = index - 1;if (lrcIndex < 0) {lrcIndex = 0;}System.out.println("lrcindex-->>" + lrcIndex);return lrcIndex;}
I obviously changed the lrcindex value, but in draw, the changed value cannot be obtained. Before I release the device, the lrcindex value is always the value before the pause (I am pausing the device and dragging the seekbar ). After you release the player, you can play the video at a certain point or drag the seekbar to display the progress information after dragging and changing it.

I searched for lrcindex in lrcview (the view showing the lyrics). Except for the set method I added myself, only the selectindex method can modify the lrcindex value.

Later I went to lrcview. the lrcindex member is not directly used in the draw. In selectindex, I call setlrcindex to set lrcindex, and then use getlrcindex in the draw to display all the lyrics normally when dragging the seekbar, this includes the red sentence that is currently being played. LyricObject temp = lrc_map.get (getLrcIndex (); the parameter in the original get () is lrcindex.

I have been learning android for more than a year, but I have never encountered this problem. I am not that uncomfortable. This time, it should not be a simple problem. It feels strange ....

I just learned the operating system this year. When I created a new process in the Linux environment and then called the variable x in the code, x is actually a copy of x in the parent process. Modifying x in the child process does not change the x value in the parent process. Is this also the problem? Is draw not in the same process as my application?


Next, let's take a look at mine. It seems that two processes are actually executing draw...



As you can see, when I move the mouse, the current red lyrics are no longer the ones I want to display. Normally, they are displayed at the bottom of the screen.


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20141218/2014121808480050.png" alt = "\">

I printed the information, and the red arrow points to the output information of lrcindex. The strange thing is that I output "-->" in selectindex, two different values are output, 49 28, 49 is dynamically displayed when I drag, indicating that the number of current lyrics is indexed in the LRC file. This is correct, and 28 is the value before I drag the touchseekbar. It looks like there are two processes that print the lrcindex value.

Even two processes, why is lrcindex updated only when I release my hands? Although it is not a short time to learn android, I still don't know much about android. I hope you can explain it.

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.