Android imitation every day beautiful songs automatic scrolling view_android

Source: Internet
Author: User
Tags abs drawtext gety time interval touch

Recent projects to do a similar song every day the effect of automatic scrolling line number. The first thing you want to do with Android is the Scroller class or Scrollto, Scrollby, or the View.layout () method, or use animation. But to loop around, it looks like these to the last line scrolling to the first row has the effect of rolling back, is not a good solution. How can you forget the canvas of all the things that could be plotted? Well, now that we've found it, let's use this option! But every day beautiful song also has a manual sliding effect, seemingly this article did not write. If so, write it yourself! Before the realization or first look at the beautiful effect of every day:

Body

idea 1: get the slide distance, then calculate how many rows have been slid, and then update the data. To achieve the seemingly ineffective effect.
Idea 2: we can see that he scrolling is a line of scrolling, but based on the speed of the scrolling to determine the speed of the number of rolling lines. In this case, as long as the scroll, a certain amount of time to the line of the scroll, and then based on the speed of scrolling to determine the update interval.

Well, think about how to do it, now write code.

First to write a class, inherit TextView

Verticalscrolltextview.class

public class Verticalscrolltextview extends TextView implements runnable{//Draw lyrics Brush private Paint mcontentpaint;
 Draw a baseline brush private Paint mlinepaint;
 Draw sliding progress background brush private Paint mrectpaint;
 Lyrics data private list<sentence> mdatalist;
 Number of rows private int index = 0;
 The wide private float MX for the current view;
 The high private float of the current view I;
 Current View vertical centerline private float Middley;
 Spacing between rows and rows private final static int DY = 80;
 Lyric text size private int mtextsize = 35;
 Lyrics middle font size private int mbigtextsize = 45;
 Whether the private Boolean Istouch = False is currently pressed;
 The y-coordinate of the last touch view private float mlasty;
 Whether the private Boolean ismoving is being slid;
 Record the time of the last slide private long lastmovetime;
 Sliding speed tracking class private velocitytracker mvelocitytracker;
 Sliding maximum speed private int mmaximumvelocity;

 Whether the lyrics are empty private Boolean isempty;
 Public Verticalscrolltextview {This (context,null);
 Public Verticalscrolltextview (context, AttributeSet attrs) {This (context, attrs,0); } public VerticalscrollTextView (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);
 Gets the maximum sliding speed value mmaximumvelocity = viewconfiguration.get (context). Getscaledmaximumflingvelocity ();
 Init ();
 private void Init () {setfocusable (true);
 Setclickable (TRUE);
  The lyrics are empty set the default value if (mdatalist==null) {mdatalist = new arraylist<> ();
  Sentence sentence = new sentence (0, "not getting lyrics", 0);
  Mdatalist.add (sentence);
 IsEmpty = true;
 }//Initialize the lyrics brush mcontentpaint = new Paint ();
 Mcontentpaint.settextsize (mtextsize);
 Mcontentpaint.setantialias (TRUE);
 Mcontentpaint.setcolor (Color.parsecolor ("#e5e2e2"));
 Set to SERIF font mcontentpaint.settypeface (Typeface.serif);
 Sets the font to be centered mcontentpaint.settextalign (Paint.Align.CENTER);
 Initializes the baseline brush mlinepaint = new Paint ();
 Mlinepaint.setantialias (TRUE);
 Mlinepaint.setstrokewidth (1);
 Mlinepaint.setcolor (Color.White);
 Progress background color Brush mrectpaint = new Paint ();
 Mlinepaint.setantialias (TRUE); Mrectpaint.setcolor (Color.parsecolor ("#66666666"));
 } @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
 If the current progress is-1, direct return, do not draw if (index = = 1) returns;
 Sentence sentence = mdatalist.get (index);
 Draw the lyrics of the middle line, set to highlight white, large font mcontentpaint.setcolor (color.white);
 Mcontentpaint.settextsize (mbigtextsize);
 Canvas.drawtext (Sentence.getname (), MX/2, Middley, Mcontentpaint); If the lyrics are currently not empty and pressed, draw baselines and progress if (!isempty&&istouch) {//Get the highest position in the middle line font float baseLine = middley-math.abs (mcontentp
  Aint.ascent ());
  Draw Progress Background canvas.drawrect (10.0f,baseline-70,150.0f,baseline,mrectpaint);
  Draw Baseline Canvas.drawline (10.0f,baseline,mx-10,baseline,mlinepaint);
  Set the progress font size mcontentpaint.settextsize (mtextsize);
 Draw Progress Font Canvas.drawtext (string.valueof (index), 85,baseline-35,mcontentpaint);
 }//Initialize IsEmpty IsEmpty = false;
 Initializes the lyric content brush Mcontentpaint.setcolor (Color.parsecolor ("#e5e2e2"));
 Mcontentpaint.settextsize (mtextsize);
 Temporarily save the middle line position to draw the number of lines above the middle line the font float tempy = Middley; Draw the lyrics above the middle line for (int i = index-1; I >= 0;
  i--) {tempy = Tempy-dy;
  if (Tempy < 0) {break;
  } Sentence presentence = Mdatalist.get (i);
 Canvas.drawtext (Presentence.getname (), MX/2, Tempy, Mcontentpaint);
 } tempy = Middley;
  Draw the lyrics below the middle line for (int i = index + 1; i < mdatalist.size (); i++) {tempy = Tempy + DY;
  if (Tempy > My) {break;
  } Sentence nexesentence = Mdatalist.get (i);
 Canvas.drawtext (Nexesentence.getname (), MX/2, Tempy, Mcontentpaint);
 }//Initialize ismoving, here to indicate sliding end ismoving = false;
 } protected void onsizechanged (int w, int h, int ow, int oh) {super.onsizechanged (W, H, Ow, OH);
 Gets the width and height MX = W of the view;
 my = h;
 Middley = h * 0.5f;
 public long Updateindex (int index) {if (index = = 1) return-1;
 This.index=index;
 return index;
 Public list<sentence> getdatalist () {return mdatalist;
 public void Setdatalist (list<sentence> mdatalist) {this.mdatalist = mdatalist;
 public void UpdateUI () {new Thread (this). Start (); } @Override Public BooleaN Ontouchevent (motionevent event) {int action = event.getaction ();
  Switch (action) {case MotionEvent.ACTION_DOWN:isTouch =true;
  Mlasty = Event.gety ();
  Break
  Case Motionevent.action_move://Create Speed tracker initvelocitytrackerifnotexists ();
  Mvelocitytracker.addmovement (event);
  Mvelocitytracker.computecurrentvelocity (1000, mmaximumvelocity); Gets the current speed.
  The default is float velocity = mvelocitytracker.getyvelocity () ==0?100:mvelocitytracker.getyvelocity ();
  Long currenttime = System.currenttimemillis (); Set a fixed value and velocity combination to determine the speed of sliding update if (!ismoving&&currenttime-lastmovetime>20000/math.abs (velocity)) {ismoving =
   true;
   Lastmovetime = System.currenttimemillis ();
   float currenty = event.gety ();
   float Mmovey = currenty-mlasty;
   Slide Up-1 slide down +1 int newindex = mmovey>0?index-1:index+1;
   Cyclic scrolling newindex=newindex<0?mdatalist.size () -1:newindex>=mdatalist.size () 0:newindex;
   Updateindex (NewIndex);
   Invalidate ();
  Mlasty = CurrentY;
 } break; Case MotionEvent.ACTION_UP:isTouch = false;
  Recyclevelocitytracker ();
 Break
 Return Super.ontouchevent (event);
 @Override public void Run () {//automatic scrolling refresh interval long time = 1000;
 control progress int i=0;
  while (true) {////is not currently pressed automatically scrolling if (!istouch) {//sets the current progress value long Sleeptime = Updateindex (i);
  Refreshes the UI Mhandler.post (mupdateresults) using handle;
  if (sleeptime = = 1) return;
   try {thread.sleep (time);
   i++;
  Automatically jumps to the first line when the last line is I==getdatalist (). Size ()) i=0;
  catch (Interruptedexception e) {e.printstacktrace ();
 }}} Handler Mhandler = new Handler ();
 Runnable mupdateresults = new Runnable () {public void run () {invalidate ();
 }
 }; Create a speed tracker private void initvelocitytrackerifnotexists () {if (Mvelocitytracker = = null) {Mvelocitytracker = Velocityt
 Racker.obtain ();
  }//Free private void Recyclevelocitytracker () {if (Mvelocitytracker!= null) {mvelocitytracker.recycle ();
 Mvelocitytracker = null;
 }
 }
}

Custom view basically this is the case, we can put some of the attributes to be defined in the attrs inside, here is lazy to write. The idea is to draw the lyrics of the specified index line first, and then draw the lyrics of the line above the index, and then draw the lyrics below the index line. Then create a new thread that will refresh the number of lines of the lyrics at timed intervals through handle. Then the ontouchevent handles the number of touch scrolling rows to get to the current scrolling speed to determine the time interval for an update. In order to achieve the touch of scrolling refresh speed. That's basically it. Look at the other notes.

Then look at the activity that initializes the data test:

Verticalscrolltextactivity.class

public class Verticalscrolltextactivity extends activity {Verticalscrolltextview Msampleview; string[] str = {"You are snowing in the Southern Sun", I am in the cold night in the north of the Four Seasons like spring, "if you come before dark and", "I want to forget your eyes," "The poor life can not finish a dream", "he is not talking to who to talk about the island of Reunion", "because the heart In the long deserted "," his heart is not fit a home "," be a mute who only lies to himself "," he said you are any praised beauty "," not as he first met you "," Time to Survive "," if all the land together "," the life only to embrace
  You, "Drunk His Dream Goodnight", "You are snowing in the Southern Sun", I am in the cold night in the north of the Four Seasons like spring, "if come before dark and", "I want to forget your eyes," "a poor life can not finish a dream", "he is not talking to who to talk about the island of Reunion", "Because the heart is already deserted," his heart is unable to install a home "," to do a lie to their own mute, "he said you any praised the beautiful," "not as he first met you," "Time to Live helpless", "if all the land together", "to embark on a
 Live just to hug you "," Drunk His Dream Goodnight "};
 @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.activity_main);
 Msampleview = (Verticalscrolltextview) Findviewbyid (R.ID.SAMPLEVIEW1);
 List lst=new arraylist<> ();
  for (int i=0;i<str.length;i++) {sentence sen=new sentence (i,str[i],i+1202034);
 Lst.add (i, SEN);
 } msampleview.setdatalist (LST); MsAmpleview.updateui ();

 } 
}

Simulates a lyric data, and then setdatalist the call to UpdateUI () on the line.

Finally look at the layout file

Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
 android:layout_width=" match_parent "
 android:layout_height=" match_parent ">

 < Com.goach.lib.VerticalScrollTextView
 android:id= "@+id/sampleview1"
 android:layout_width= "Match_parent"
 android:layout_height= "match_parent"
 android:background= "@drawable/bg"
 />
</ Relativelayout>

Test, we can see the effect:

SOURCE Download: Android Imitation every day beautiful songs automatically scrolling

This is the entire content of this article, I hope to learn more about Android software programming help.

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.