The method of realizing sliding effect of novel reader by Android programming _android

Source: Internet
Author: User
Tags visibility

This paper illustrates the method of Android programming to realize the sliding effect of novel reader. Share to everyone for your reference, specific as follows:

Read the novel all know that the novel Reader page has a variety of effects, such as simulation page, sliding page, and so on. For some reason, suddenly want to write a simple point of sliding page effect. There is no intention to write here, I hope you can according to this effect extrapolate, write other results. The picture is not on.

Here's the code: you understand the Ontouch event.

Package Com.example.testscroll.view; 
Import Android.content.Context; 
Import Android.util.AttributeSet; 
Import android.view.MotionEvent; 
Import Android.view.VelocityTracker; 
Import Android.view.View; 
Import android.view.ViewConfiguration; 
Import Android.view.ViewGroup; 
Import Android.widget.Scroller; 
 public class Flipperlayout extends ViewGroup {private scroller mscroller; 
 Private Velocitytracker Mvelocitytracker; 
 private int mvelocityvalue = 0; 
 /** agreed that this slide is valid distance */private int limitdistance = 0; 
 private int screenwidth = 0; 
 /** finger moving Direction/private static final int move_to_left = 0; 
 private static final int move_to_right = 1; 
 private static final int move_no_result = 2; 
 /** Final Touch Result direction * * Private int mtouchresult = Move_no_result; 
 /** first direction/private int mdirection = Move_no_result; 
 /** Touch Mode * * private static final int mode_none = 0; 
 private static final int mode_move = 1; 
 private int mmode = Mode_none; /** Sliding View */private View MscroLlerview = null; 
 /** view (on the edge, invisible) * * Private view Currenttopview = null; 
 /** display view, displayed on the screen/private View currentshowview = null; 
 /** the bottom view (invisible)/private view Currentbottomview = null; 
  Public Flipperlayout {Super (context); 
 Init (context); 
  Public Flipperlayout (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); 
 Init (context); 
  Public Flipperlayout (context, AttributeSet attrs) {Super (context, attrs); 
 Init (context); 
  private void init (context context) {Mscroller = new scroller (context); 
  ScreenWidth = Context.getresources (). Getdisplaymetrics (). Widthpixels; 
 Limitdistance = SCREENWIDTH/3;  /*** * * @param listener * @param currentbottomview * Lowest view, initial status not visible * @param currentshowview * The view that is being displayed @param currenttopview * Top view, slide out of the screen/public void initflipperviews (Touchlistener listener, View Currentbottomview, viewCurrentshowview, View currenttopview) {this.currentbottomview = Currentbottomview; 
  This.currentshowview = Currentshowview; 
  This.currenttopview = Currenttopview; 
  Settouchresultlistener (listener); 
  AddView (Currentbottomview); 
  AddView (Currentshowview); 
  AddView (Currenttopview); 
 /** defaults to the topmost view sliding edge (used to view the previous page)/Currenttopview.scrollto (-screenwidth, 0); @Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) {for (int i = 0; i < GETCHILDC Ount (); 
   i++) {View child = Getchildat (i); 
   int height = child.getmeasuredheight (); 
   int width = child.getmeasuredwidth (); 
  Child.layout (0, 0, width, height); } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasur 
  Espec, Heightmeasurespec); 
  int width = measurespec.getsize (widthmeasurespec); 
  int height = measurespec.getsize (heightmeasurespec); 
  Setmeasureddimension (width, height); for (int i = 0; i < Getchildcount (); 
  i++) {Getchildat (i). Measure (Widthmeasurespec, heightmeasurespec); 
 } private int startx = 0; @Override public boolean dispatchtouchevent (motionevent ev) {switch (ev.getaction ()) {case Motionevent.action_do 
   Wn:if (!mscroller.isfinished ()) {break; 
   } startx = (int) ev.getx (); 
  Break 
 return super.dispatchtouchevent (EV); @SuppressWarnings ("deprecation") @Override public boolean ontouchevent (Motionevent event) {Obtainvelocitytrack 
  ER (event); Switch (event.getaction ()) {Case MotionEvent.ACTION_MOVE:if (!mscroller.isfinished ()) {return Super.ontouch 
   Event (event); 
   } if (startx = = 0) {startx = (int) event.getx (); 
   final int distance = startx-(int) event.getx ();  if (mdirection = = Move_no_result) {if (Mlistener.whetherhasnextpage () && distance > 0) {mdirection 
    = Move_to_left; else if (mlistener.whetherhaspreviouspage () && DistancE < 0) {mdirection = Move_to_right; } if (Mmode = Mode_none && (mdirection = = Move_to_left && mlistener.whetherhasnextpage () ) || (mdirection = = Move_to_right && mlistener whetherhaspreviouspage ()))) 
   {mmode = Mode_move; } if (Mmode = = Mode_move) {if (mdirection = = Move_to_left && distance <= 0) | | (mdirection = = move_to_right && distance >= 0)) 
    {mmode = Mode_none; } if (Mdirection!= move_no_result) {if (mdirection = = Move_to_left) {if (Mscrollerview!= currents 
     Howview) {Mscrollerview = Currentshowview; 
     } else {if (Mscrollerview!= currenttopview) {mscrollerview = Currenttopview; } if (Mmode = = Mode_move) {mvelocitytracker.computecurrentvelocity (1000, VIEWCONFIGURATION.GETMAXIMUMFL 
     Ingvelocity ()); 
     if (mdirection = = Move_to_left) {mscrollerview.scrollto (distance, 0); } else {Mscrollerview.scrollto (screenwidth + distance, 0); 
     } else {final int scrollx = MSCROLLERVIEW.GETSCROLLX (); if (mdirection = = Move_to_left && scrollx!= 0 && mlistener.whetherhasnextpage ()) {MSCROLLERVIEW.S 
     Crollto (0, 0); else if (mdirection = Move_to_right && mlistener.whetherhaspreviouspage () && screenwidth!= Math.Abs (s 
     CROLLX)) {Mscrollerview.scrollto (-screenwidth, 0); 
  }} break; 
   Case MotionEvent.ACTION_UP:if (Mscrollerview = = null) {return super.ontouchevent (event); 
   Final int scrollx = MSCROLLERVIEW.GETSCROLLX (); 
   Mvelocityvalue = (int) mvelocitytracker.getxvelocity (); 
    Scroll Zo Zheng, right negative (), (startx + dx) value if 0, reset/* android.widget.Scroller.startScroll (int startx, int starty, int 
   * DX, int dy, int duration) */int time = 500; if (Mmode = = Mode_move && mdirection = = move_to_left) {if (Scrollx >limitdistance | | 
     Mvelocityvalue <-time) {//fingers moved to the left, you can turn the screen mtouchresult = Move_to_left; 
     if (Mvelocityvalue <-time) {time = 200; 
    } mscroller.startscroll (SCROLLX, 0, SCREENWIDTH-SCROLLX, 0, time); 
     else {mtouchresult = Move_no_result; 
    Mscroller.startscroll (scrollx, 0,-SCROLLX, 0, time); } else if (Mmode = mode_move && mdirection = = move_to_right) {if (SCREENWIDTH-SCROLLX) > LIMITD istance | | 
     Mvelocityvalue > Time) {//fingers to the right to move, you can turn the screen mtouchresult = Move_to_right; 
     if (Mvelocityvalue > Time) {time = 250; 
    } mscroller.startscroll (SCROLLX, 0,-SCROLLX, 0, time); 
     else {mtouchresult = Move_no_result; 
    Mscroller.startscroll (scrollx, 0, SCREENWIDTH-SCROLLX, 0, time); 
   } resetvariables (); 
   Postinvalidate (); 
  Break 
 return true; 
  private void Resetvariables () {mdirection = Move_no_result; Mmode = MODE_none; 
  startx = 0; 
 Releasevelocitytracker (); 
 Private Touchlistener Mlistener; 
 private void Settouchresultlistener (Touchlistener listener) {This.mlistener = listener; 
  @Override public void Computescroll () {super.computescroll ();
   if (Mscroller.computescrolloffset ()) {Mscrollerview.scrollto (Mscroller.getcurrx (), Mscroller.getcurry ()); 
  Postinvalidate (); else if (mscroller.isfinished () && mlistener!= null && mtouchresult!= move_no_result) {if (Mtouch 
    result = = Move_to_left) {if (Currenttopview!= null) {Removeview (Currenttopview); 
    } Currenttopview = Mscrollerview; 
    Currentshowview = Currentbottomview; 
     if (Mlistener.currentislastpage ()) {final View Newview = Mlistener.createview (Mtouchresult); 
     Currentbottomview = Newview; 
    AddView (Newview, 0); 
     else {Currentbottomview = new View (GetContext ()); 
     Currentbottomview.setvisibility (View.gone); AddView (CurrenTbottomview, 0); 
    } else {if (Currentbottomview!= null) {Removeview (Currentbottomview); 
    } Currentbottomview = Currentshowview; 
    Currentshowview = Mscrollerview; 
     if (Mlistener.currentisfirstpage ()) {final View Newview = Mlistener.createview (Mtouchresult); 
     Currenttopview = Newview; 
     Currenttopview.scrollto (-screenwidth, 0); 
    AddView (Currenttopview); 
     else {Currenttopview = new View (GetContext ()); 
     Currenttopview.scrollto (-screenwidth, 0); 
     Currenttopview.setvisibility (View.gone); 
    AddView (Currenttopview); 
  } Mtouchresult = Move_no_result; }} private void Obtainvelocitytracker (Motionevent event) {if (Mvelocitytracker = null) {Mvelocitytracker = 
  Velocitytracker.obtain (); 
 } mvelocitytracker.addmovement (event); 
   private void Releasevelocitytracker () {if (Mvelocitytracker!= null) {mvelocitytracker.recycle (); 
  Mvelocitytracker = null; 
 } 
 }/*** * For real-time callback Touch event callback * * @author Freeson/public interface Touchlistener {/** Fingers slide to the left, view next chapter/FINA 
  l int move_to_left = 0; 
  /** finger slide to the right, that is, view the previous chapter */FINAL int move_to_right = 1; 
  /** * Create a view * * @param direction * {@link move_to_left,move_to_right} * * @return * * 
  Public View CreateView (final int direction); 
  /*** * Whether the current page is the first page * * @return * * public boolean currentisfirstpage (); 
  /*** * Whether the current page is the last page * * @return * * public boolean currentislastpage (); 
  /** * Whether the current page has a previous page (used to determine the sliding) * * @return/public boolean whetherhaspreviouspage (); 
 /*** * Whether the current page has a next page (used to determine the sliding) * * @return/public boolean whetherhasnextpage ();

 } 
}

Activity test File:

Package com.example.testscroll; 
Import Java.io.ByteArrayOutputStream; 
Import java.io.IOException; 
Import Java.io.InputStream; 
Import android.app.Activity; 
Import Android.content.res.AssetManager; 
Import Android.os.Bundle; 
Import Android.os.Handler; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.widget.TextView; 
Import Com.example.testscroll.view.FlipperLayout; 
Import Com.example.testscroll.view.FlipperLayout.TouchListener; 
Import COM.EXAMPLE.TESTSCROLLACTIVITY.R; 
 public class Mainactivity extends activity implements Onclicklistener, Touchlistener {private String text = ""; 
 private int textlenght = 0; 
 private static final int COUNT = 400; 
 private int currenttopendindex = 0; 
 private int currentshowendindex = 0; 
 private int currentbottomendindex = 0; Private Handler Handler = new Handler () {public void Handlemessage (Android.os.Message msg) {flipperlayout Rootlay out = (flipperlayOut) Findviewbyid (R.id.container); 
   View Recoverview = Layoutinflater.from (mainactivity.this). Inflate (r.layout.view_new, NULL); 
   View view1 = Layoutinflater.from (mainactivity.this). Inflate (r.layout.view_new, NULL); 
   View view2 = Layoutinflater.from (mainactivity.this). Inflate (r.layout.view_new, NULL); 
   Rootlayout.initflipperviews (Mainactivity.this, View2, View1, Recoverview); 
   Textlenght = Text.length (); 
   SYSTEM.OUT.PRINTLN ("----textlenght----->" + textlenght); 
   TextView TextView = (TextView) View1.findviewbyid (R.id.textview); 
    if (Textlenght > Count) {textview.settext (text.subsequence (0, Count)); 
    TextView = (TextView) View2.findviewbyid (R.id.textview); 
     if (Textlenght > (Count << 1)) {Textview.settext (Text.subsequence (count, Count * 2)); 
     Currentshowendindex = COUNT; 
    Currentbottomendindex = COUNT << 1; 
     else {textview.settext (text.subsequence (COUNT, textlenght)); Currentshowendindex = TEXTLEnght; 
    Currentbottomendindex = Textlenght; 
    } else {Textview.settext (text.subsequence (0, textlenght)); 
    Currentshowendindex = Textlenght; 
   Currentbottomendindex = Textlenght; 
 } 
  }; 
 }; 
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  Setcontentview (R.layout.activity_main); 
 New Readingthread (). Start (); @Override public void OnClick (View v) {} @Override public View createview (final int direction) {String tx 
  t = ""; 
   if (direction = = Touchlistener.move_to_left) {currenttopendindex = Currentshowendindex; 
   Final int nextindex = Currentbottomendindex + COUNT; 
   Currentshowendindex = Currentbottomendindex; 
    if (Textlenght > Nextindex) {txt = text.substring (Currentbottomendindex, Nextindex); 
   Currentbottomendindex = Nextindex; 
    else {txt = text.substring (Currentbottomendindex, textlenght); 
   Currentbottomendindex = Textlenght; 
  }else {currentbottomendindex = Currentshowendindex; 
   Currentshowendindex = Currenttopendindex; 
   Currenttopendindex = Currenttopendindex-count; 
  txt = text.substring (Currenttopendindex-count, Currenttopendindex); 
  View view = Layoutinflater.from (this). Inflate (r.layout.view_new, NULL); 
  TextView TextView = (TextView) View.findviewbyid (R.id.textview); 
  Textview.settext (TXT); System.out.println ("-top->" + Currenttopendindex + "-show->" + Currentshowendindex + "--bottom-->" + 
  Currentbottomendindex); 
 return view; 
 @Override public boolean whetherhaspreviouspage () {return currentshowendindex > COUNT; 
 @Override public boolean whetherhasnextpage () {return Currentshowendindex < textlenght; 
  @Override public boolean Currentisfirstpage () {Boolean should = Currenttopendindex > COUNT; 
   if (!should) {currentbottomendindex = Currentshowendindex; 
   Currentshowendindex = Currenttopendindex; CurrenttopendIndex = Currenttopendindex-count; 
 return should; 
  @Override public boolean Currentislastpage () {Boolean should = Currentbottomendindex < Textlenght; 
   if (!should) {currenttopendindex = Currentshowendindex; 
   Final int nextindex = Currentbottomendindex + COUNT; 
   Currentshowendindex = Currentbottomendindex; 
   if (Textlenght > Nextindex) {currentbottomendindex = Nextindex; 
   else {currentbottomendindex = Textlenght; 
 } return should; 
   Private class Readingthread extends Thread {public void run () {Assetmanager am = getassets (); 
   InputStream response; 
    try {response = Am.open ("Text.txt"); 
     if (response!= null) {Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); 
     int i =-1; 
     while ((i = Response.read ())!=-1) {baos.write (i); 
     } Text = new String (Baos.tobytearray (), "UTF-8"); 
     Baos.close (); 
     Response.close (); Handler.sendemptymessage (0); 
   } catch (IOException e) {e.printstacktrace ();

 } 
  } 
 } 
}

XML layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
 android:layout_width=" match_parent " 
 android:layout_height=" match_parent " 
 android:o" rientation= "Horizontal" > 
 <textview 
  android:id= "@+id/textview" 
  android:layout_width= "0DP" 
  android:layout_height= "match_parent" 
  android:layout_weight= "1.0" 
  android:background= "#666666" 
  android:gravity= "center" 
  android:text= "New View" 
  android:textcolor= "@android: Color/white" 
  Android:textsize= "16SP" 
  android:visibility= "visible"/> 
 <view 
  android:layout_width= "5DP" 
  android:layout_height= "match_parent" 
  android:background= "#FFFF00" 
  android:gravity= "center" 
  android:textsize= "25SP" 
  android:visibility= "visible"/>
</LinearLayout>

Activity layout File:

<com.example.testscroll.view.flipperlayout xmlns:android= "Http://schemas.android.com/apk/res/android" 
 xmlns:tools= "Http://schemas.android.com/tools" " 
 android:id=" @+id/container " 
 android:layout_width=" match _parent " 
 android:layout_height=" match_parent "> 
</com.example.testscroll.view.FlipperLayout>

Note: The above why add a rate calculator, in fact, just to identify the action is not a quick move, even if the sliding distance of less than 1/3 of the screen, but as long as the rate of satisfaction can be determined to change the sliding is a page-flipping action.

Note: This is only one of the effects of sliding, ah, not including the logic of the chapters of the novel Oh. Although some rough, but there is still a place to learn, if you have any good solutions, you can discuss together.

Add demo download Address click Download Demo.

I hope this article will help you with the Android program.

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.