7

strisciare su e giù per l'effetto, come quelle applicazioni notizie inshorts, escursione notizie, mormorio.Android: il layout di animazione come Inshorts News App

intero layout uniformemente su/giù.
controllare l'app su questo collegamento inshorts e murmur.

ho provato questo codice ...

public class VerticalViewPager extends ViewPager { 
    public VerticalViewPager(Context context) { 
     super(context); 
     init(); 
    } 

    public VerticalViewPager(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 
    private void init() { 
     // The majority of the magic happens here 
     setPageTransformer(true, new VerticalPageTransformer()); 
     // The easiest way to get rid of the overscroll drawing that happens on the left and right 
     setOverScrollMode(OVER_SCROLL_NEVER); 
    } 
    private class VerticalPageTransformer implements PageTransformer { 

     @SuppressLint("NewApi") 
     @Override 
     public void transformPage(View view, float position) { 

      if (position < -1) { // [-Infinity,-1) 
       // This page is way off-screen to the left. 
       view.setAlpha(0); 

      } else if (position <= 1) { // [-1,1] 
       view.setAlpha(1); 

       // Counteract the default slide transition 
       view.setTranslationX(view.getWidth() * -position); 

       //set Y position to swipe in from top 
       float yPosition = position * view.getHeight(); 
       view.setTranslationY(yPosition); 

      } else { // (1,+Infinity] 
       // This page is way off-screen to the right. 
       view.setAlpha(0); 
      } 
     } 
    } 

    /** 
    * Swaps the X and Y coordinates of your touch event. 
    */ 
    private MotionEvent swapXY(MotionEvent ev) { 
     float width = getWidth(); 
     float height = getHeight(); 

     float newX = (ev.getY()/height) * width; 
     float newY = (ev.getX()/width) * height; 

     ev.setLocation(newX, newY); 

     return ev; 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev){ 
     boolean intercepted = super.onInterceptTouchEvent(swapXY(ev)); 
     swapXY(ev); // return touch coordinates to original reference frame for any child views 
     return intercepted; 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 
     return super.onTouchEvent(swapXY(ev)); 
    } 

} 

In MainActivity.java

VerticalViewPager Pager2; 
PagerAdapter adapter; 
String[] articleTitle; 
String[] articleName; 
String[] articleDiscription; 

OnCreate() 

Pager2=(VerticalViewPager)findViewById(R.id.pager); 


// Pass results to ViewPagerAdapter Class 
adapter = new ViewPagerAdapter(getActivity(), articleTitle, articleName,  articleDiscription, btnBack,articleImage); 

// Binds the Adapter to the ViewPager 
Pager2.setAdapter(adapter); 

activity_main.xml

<com.example.flipnews.VerticalViewPager 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/pager" 
    /> 

Nel mio codice semplice tocco up-down fatto, come questo link. ma voglio creare un migliore effetto di animazione come le app sopra menzionate. o telefono incorporato effetto galleria fotografica.

Grazie in anticipo.

+0

Ciao, Viveka ... non possiamo aiutarti a sviluppare le cose ... possiamo solo aiutarti a risolvere questo problema. Devi provarlo da solo e pubblicare il tuo codice con i problemi in seguito. –

+1

@Viveka se hai trovato lib per questa animazione, fammelo sapere. – Dev

+1

Ho già provato questo codice ma non ho ottenuto l'output corretto ... controlla il mio codice. - @MarianoZorrilla –

risposta

13

Ho trovato la soluzione dopo molte ricerche spero sia utile per gli altri.

Suggerimento: impostare il colore di sfondo per visualizzare il cercapersone in nero per un migliore effetto di scorrimento.

private static class VerticalPageTransformer implements PageTransformer { 
     private static float MIN_SCALE = 0.75f; 

     public void transformPage(View view, float position) { 
      int pageWidth = view.getWidth(); 

      if (position < -1) { // [-Infinity,-1) 
       // This page is way off-screen to the left. 
       view.setAlpha(0); 



      } else if (position <= 0) { // [-1,0] 
       // Use the default slide transition when moving to the left page 
       view.setAlpha(1); 
       //view.setTranslationX(1); 
       view.setScaleX(1); 
       view.setScaleY(1); 
       float yPosition = position * view.getHeight(); 
       view.setTranslationY(yPosition); 
       view.setTranslationX(-1 * view.getWidth() * position); 

      } else if (position <= 1) { // (0,1] 
       // Fade the page out. 
       view.setAlpha(1 - position); 

       view.setTranslationX(-1 * view.getWidth() * position); 

       // Scale the page down (between MIN_SCALE and 1) 
       float scaleFactor = MIN_SCALE 
         + (1 - MIN_SCALE) * (1 - Math.abs(position)); 
       view.setScaleX(scaleFactor); 
       view.setScaleY(scaleFactor); 

      } else { // (1,+Infinity] 
       // This page is way off-screen to the right. 
       view.setAlpha(0); 
      } 

     } 
    } 

    private MotionEvent swapXY(MotionEvent ev) { 
     float width = getWidth(); 
     float height = getHeight(); 

     float newX = (ev.getY()/height) * width; 
     float newY = (ev.getX()/width) * height; 

     ev.setLocation(newX, newY); 

     return ev; 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev){ 
     boolean intercepted = super.onInterceptTouchEvent(swapXY(ev)); 
     swapXY(ev); // return touch coordinates to original reference frame for any child views 
     return intercepted; 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 
     return super.onTouchEvent(swapXY(ev)); 
    } 

} 
+0

Funzionante Awesome hai salvato la mia giornata !!!!! – user3069590

+0

@Viveka Ho un problema con il mio cercapersone vista al suo interno. quando si scorre la funzione di scorrimento della griglia non funziona – Praneeth

+0

Ciao, ho provato la tua risposta e funziona benissimo. Sono un principiante in questo. Puoi darmi una breve descrizione su cosa stai facendo esattamente qui, ad esempio 'view.setTranslationX (-1 * view.getWidth() * position);' questo? – Vishnu