Problems with Android Custom view update (solution answer)

Source: Internet
Author: User

Today, there is a very strange question, about the update of the LRC lyrics in view, the view has a member variable, Lrcindex, in draw will be used to update the lyrics, there is a line in the lyrics is red, indicating the currently playing lyrics. Then I drag seekbar and update the lyrics display, I onprogresschanged inside the update, the other colors of the lyrics can be normal display, is that the red lyrics or pause before that sentence, no update, how to drag all useless. I print out the lrcindex in draw, the value is not changed or the value before the pause. But in fact I have modified the lrcindex when I get the current lyrics based on the current playback time, what's going on?

What I do is a music player that dynamically updates the lyrics and progress bar.

I'll show you My code (only some of the key ones).

<span style= "FONT-SIZE:18PX;" >seekbar.setonseekbarchangelistener (New Onseekbarchangelistener () {@Overridepublic void Onstoptrackingtouch ( SeekBar SeekBar) {Mp.seekto (seekbar.getprogress ()),//MP is the service that plays music, this method is used to set the progress of MediaPlayer} @Overridepublic void Onstarttrackingtouch (SeekBar SeekBar) {//TODO auto-generated method Stubmp.stopupdate ();//Stop Auto update progress bar time and lyrics}@ overridepublic void onprogresschanged (SeekBar SeekBar, int progress,boolean fromuser) {//This is a callback function, as long as the value of SeekBar is changed, This method is called by the system. <span style= "White-space:pre" ></span>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}});} </span>
Every time I update the progress of the lyrics, I call invalidate to update the view, and the console also outputs the debug information in draw, Lrcindex and I get in the Seekbar value change listener is not the same.

Look again, Selectindex method

<span style= "FONT-SIZE:18PX;" >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;} </span>
I changed the value of Lrcindex, but in draw it was not getting the value it changed. Before I release my hand, the value of the Lrcindex is always the value before the pause (I'm dragging seekbar after pausing). After releasing the hand, play at the point or drag the Seekbar again, it will display the progress information before you drag the change to start printing.

I looked for Lrcindex in Lrcview (the view that shows the lyrics), except for the set method I added myself, only the Selectindex method can modify the value of Lrcindex.

Later I do not directly in the Lrcview.draw with Lrcindex this member, I in Selectindex inside call Setlrcindex to set Lrcindex, and then in draw inside use Getlrcindex result is right, drag Seekbar when you can display all the songs normally Words, including the red one that is currently playing. Lyricobject temp = Lrc_map.get (Getlrcindex ()); the original get () parameter is Lrcindex.

I have been learning Android for more than a year, but never encountered this problem, I am not the kind of very tongqiao, this encounter should not be a simple problem. It feels so strange ....

This year just learned the operating system, at that time in the Linux environment to create a new process, the new process calls the code inside the variable x, X is actually the parent process inside the x copy, modify the child process inside the x does not change the value of x in the parent process, is this the problem? is draw with my app not a process?


Next up my, it seems really two processes in the execution of draw ...



Everyone can see, I drag the mouse, the current red lyrics is not the lyrics I want to display, the normal display should be in the middle of the screen below a point.



is I print information, the red arrow pointing to IS Lrcindex output information, strange is I in selectindex inside output of "-->>" incredibly in front and back output two different values, 49 28,49 is I drag when the dynamic display, Represents the index of the current number of lyrics in the LRC file, which is correct, and 28 is the value I touchseekbar before dragging. It looks like there are two processes that print the value of a lrcindex.

Even if it's two processes, why is it that the Lrcindex will only be updated if I loosen my hand? Although the time to learn Android is not short, but there is a lot of Android still do not understand, hope the great God can explain

Problems with Android Custom view update (solution answer)

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.