Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Android custom ViewPager create kaleidoscopic image transition effects     - HDFS Hadoop Distributed File System Works (Server)

- The mv command to move the directory two cases under Linux (Linux)

- Vim Getting Started Tutorial (Linux)

- Java NIO in the world (Programming)

- Analysis of MySQL Dockerfile 5.6 (Database)

- Java programmers talk about those advanced knowledge and direction (Programming)

- Use Oracle Data Guard to complete cross-platform database migration cases (Database)

- Zookeeper cluster deployment (Server)

- Jetty JNDI Development combat (Linux)

- Configuring a Linux operating system security management services Explain (Linux)

- Linux5 install MySQL 5.6 (RPM mode) (Database)

- Rman Oracle database backup and recovery plan (Database)

- IOS distributed management using GitHub project development (Linux)

- ASP.NET 5 (vNext) Linux deployment (Server)

- CentOS modify yum update source (Linux)

- Ubuntu 12.04 64-bit installation Redmine + Git + ReviewBoard (Linux)

- openSUSE 13.1 / 13.2 installation SoundConverter 2.1.2 (Linux)

- CentOS / Linux SELinux Close (Linux)

- ogg start being given libnnz11.so: can not open shared object file (Database)

- Android screen rotation processing and ProgressDialog the best AsyncTask (Programming)

 
         
  Android custom ViewPager create kaleidoscopic image transition effects
     
  Add Date : 2018-11-21      
         
         
         
  I remember the first time I saw this ViewPager controls, instant love, doing things all ViewPager main interface, and the image switching is also abandoned ImageSwitch like, started to make ViewPager do. A long time, transition effects ViewPager feel dull, fatigued formed ~~ we need to change, to teach you today how to change the effect ViewPager handover, personalized picture switch

1.Analysis of pre-production

Observed renderings, in fact, change is the animation when switching, so simple, and only requires the user to switch to get the current and the next View View, then add the animation is not on it. Well, the first step to get the user to switch when the current View and View to the handover destination.

We look, and if either the current View object View, animation for slow changes we need, it is best slide based on user gestures. For example, the above effect, when the user slides an object image appear slowly and gradually increases according to the user sliding distance. Well, the second step, the gradient design animation.

After analysis, we conclude that the two steps, let's start step by step to build ~~~

2. Get the user to switch the current View and switch to the purpose of View.

ViewPager also need to listen to the user's gesture, it certainly provides a method. So Throughout ViewPager way we found a known onPageScrolled (int position, float positionOffset, int positionOffsetPixels) method ~

Yes this is the method: when the page scrolls call ~

Under careful study of the following parameters:

Direct said the test results:

In the non-first page and the last page, slide to the next, position the current page position; slide to the previous page: position -1 to this page

positionOffset slide to the next, [0,1) interval change; slide to the previous page: (1,0] interval changes on

positionOffsetPixels and positionOffset like this: slide to the next, [0, width) on the interval change; slide to the previous page :( width, 0] on the interval change

When the first page: slide to the previous position = 0, the other is substantially 0; the last one slide to the next position for the current page location, the other two parameters 0

Suddenly found that the second step to solve the steps we need, positionOffset very suitable as, gradient, scaling control parameter; positionOffsetPixels you can control parameters such as panning.

So how do you get the current View and Objective View it:

I share a few astray:

1, [Error] I passed getChildAt (position), getChildAt (position + 1), getChildAt (position-1) obtained slid around two View; At first glance, it really felt good to write code ~~~ and then get out at first glance the effect ~ ~ Cause: we ignore a particularly big thing ViewPager mechanism, dynamic loading and deleting slides View, ViewPager fact, only last 2-3 View, while the position of the basic part of range Unlimited ~

2, [Error] I get the current position by getCurrentItem, then +1, -1 after obtaining a chuckle or previous ~ ~ is, the code quickly changed over, the effect is how not right, mess ~ ~ careful observation log, this getCurrentItem when the user finger from the screen when Page still perform animations, to change the ~ ~ ~ No wonder the entire sliding process is not fixed ~ ~ Oh, a broken heart ~

3, [error] position in the entire sliding process is not changed, and ViewPager saved two or three View; then I thought, if it is the first page, or last page then I take getChildAt (0) and getChildAt (1), if compared to other pages in getChildAt (0), getChildAt (2), and then through a series of changes - I think it would be the total of the bar, so I encountered the first problem, the first page time, regardless of left and right position is 0, Nima, which it left View, which is the right View ~~

Having said all this wrong, we can bypass these detours, but also from inside these detours to see something ~

Below that correct, in fact, when you add a View ViewPager or destroy a View, PageAdapter our own control, so we can maintain a HashMap inside the ViewPager

And then when the slide by get (position) out, such as the above effect is always the right of the View change, either from small to large, or big to small

Then slip Next: left View: map.get (position), the right of View: map.get (position + 1).

Then slipping Previous: left View: map.get (position), the right of View: map.get (position + 1), the same as previous slide, position the current page -1

Well, so far, we have analyzed and solved all the steps.

3, the code

MainActivity

package com.example.zhy_jazzyviewpager;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class MainActivity extends Activity
{
 protected static final String TAG = "MainActivity";
 private int [] mImgIds;
 private MyJazzyViewPager mViewPager;

 @Override
 protected void onCreate (Bundle savedInstanceState)
 {
  super.onCreate (savedInstanceState);
  setContentView (R.layout.activity_main);
  mImgIds = new int [] {R.drawable.a, R.drawable.b, R.drawable.c,
    R.drawable.d};
  mViewPager = (MyJazzyViewPager) findViewById (R.id.id_viewPager);
  mViewPager.setAdapter (new PagerAdapter ()
  {

   @Override
   public boolean isViewFromObject (View arg0, Object arg1)
   {
    return arg0 == arg1;
   }

   @Override
   public void destroyItem (ViewGroup container, int position,
     Object object)
   {
    container.removeView ((View) object);
   }

   @Override
   public Object instantiateItem (ViewGroup container, int position)
   {
    ImageView imageView = new ImageView (MainActivity.this);
    imageView.setImageResource (mImgIds [position]);
    imageView.setScaleType (ScaleType.CENTER_CROP);
    container.addView (imageView);
    mViewPager.setObjectForPosition (imageView, position);
    return imageView;
   }

   @Override
   public int getCount ()
   {
    return mImgIds.length;
   }
  });

 }

}

This is very common code is to initialize ViewPager ~~ on nothing more to say ~ ~ One thing to note: in instantiateItem method, we call a multi mViewPager.setObjectForPosition (imageView, position); in fact, is to give us Map stored value

Mainly to see ViewPager custom

package com.example.zhy_jazzyviewpager;

import java.util.HashMap;
import java.util.LinkedHashMap;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.nineoldandroids.view.ViewHelper;

public class MyJazzyViewPager extends ViewPager
{
 private float mTrans;
 private float mScale;
 / **
  * Maximum reduction ratio
  * /
 private static final float SCALE_MAX = 0.5f;
 private static final String TAG = "MyJazzyViewPager";
 / **
  * Save position with respect to the View
  * /
 private HashMap < Integer, View> mChildrenViews = new LinkedHashMap < Integer, View> ();
 / **
  * When the left sliding element
  * /
 private View mLeft;
 / **
  * The right of the sliding element
  * /
 private View mRight;

 public MyJazzyViewPager (Context context, AttributeSet attrs)
 {
  super (context, attrs);
 }

 @Override
 public void onPageScrolled (int position, float positionOffset,
   int positionOffsetPixels)
 {

// Log.e (TAG, "position =" + position + ", positionOffset =" + positionOffset + ", positionOffsetPixels =" + positionOffsetPixels + ", currentPos =" + getCurrentItem ());
  
  // Sliding very small distance, we see no movement, dispensable judgment
  float effectOffset = isSmall (positionOffset) 0: positionOffset;?
  
  // Get the left View
  mLeft = findViewFromObject (position);
  // Get the right side of the View
  mRight = findViewFromObject (position + 1);
  
  // Add Switching animation effects
  animateStack (mLeft, mRight, effectOffset, positionOffsetPixels);
  super.onPageScrolled (position, positionOffset, positionOffsetPixels);
 }

 public void setObjectForPosition (View view, int position)
 {
  mChildrenViews.put (position, view);
 }

 / **
  * Obtain the corresponding position by over-View
  *
  * @param Position
  * @return
  * /
 public View findViewFromObject (int position)
 {
  return mChildrenViews.get (position);
 }

 private boolean isSmall (float positionOffset)
 {
  return Math.abs (positionOffset) < 0.0001;
 }

 protected void animateStack (View left, View right, float effectOffset,
   int positionOffsetPixels)
 {
  if (right! = null)
  {
   / **
    * If the reduction ratio slide your finger from right to left (after switching to a): 0.0 to 1.0, from half to a maximum
    * If you slide your finger from left to right (before switching to a): 1.0 to 0, that is, from the largest to half
    * /
   mScale = (1 - SCALE_MAX) * effectOffset + SCALE_MAX;
   / **
    * X offset: If you slide your finger from right to left (after switching to a): 0-720 If you slide your finger from left to right (before switching to a): 720-0
    * /
   mTrans = -getWidth () - getPageMargin () + positionOffsetPixels;
   ViewHelper.setScaleX (right, mScale);
   ViewHelper.setScaleY (right, mScale);
   ViewHelper.setTranslationX (right, mTrans);
  }
  if (left! = null)
  {
   left.bringToFront ();
  }
 }
}

You can see, the core code is onPageScrolled, we findViewFromObject (position); findViewFromObject (position + 1); the left and right sides, respectively, to obtain the View, and then animate; the present case adds two animations, one is from 0.5 zoom down to 1.0 or 1.0 to 0.5, right by our positionOffset gradient ~ ~ there is a translation of the movie: Next to move directly to the current screen (the default is on the right, you can comment this effect, run to see how see), and then continue through the original default positionOffsetPixels offset displacement when moving, allowing users to feel it in place zoom ~~

Okay, so to achieve ~ ~ you can easily write their favorite animation effects, such as the above plus a default fade or God horse, just is not very random ~ ~ ~

Our layout file:

< RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
    xmlns: tools = "http://schemas.android.com/tools"
    android: layout_width = "match_parent"
    android: layout_height = "match_parent"
  >

    < Com.example.zhy_jazzyviewpager.MyJazzyViewPager
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: id = "@ + id / id_viewPager" />

< / RelativeLayout>

4, JazzyViewPager use

In fact, the above implementation is on github JazzyViewPager source, usage, needless to say, is our MainActivity, it is built to about 10 kinds of effects, we can animate our example above, the effect of ~ by code or layout above, it is called Stack;

Use JazzViewPager code: In fact, almost the same ~ ~ would eventually labeled JazzyViewPager source download

MainActivity

package com.jfeinstein.jazzyviewpager;

import com.jfeinstein.jazzyviewpager.JazzyViewPager.TransitionEffect;

import Android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class MainActivity extends Activity
{
 protected static final String TAG = "MainActivity";
 private int [] mImgIds;
 private JazzyViewPager mViewPager;

 @Override
 protected void onCreate (Bundle savedInstanceState)
 {
  super.onCreate (savedInstanceState);
  setContentView (R.layout.activity_main);
  mImgIds = new int [] {R.drawable.a, R.drawable.b, R.drawable.c,
    R.drawable.d};
  mViewPager = (JazzyViewPager) findViewById (R.id.id_viewPager);
  // Set the transition effects
  mViewPager.setTransitionEffect (TransitionEffect.Stack);
  
  
  mViewPager.setAdapter (new PagerAdapter ()
  {

   @Override
   public boolean isViewFromObject (View arg0, Object arg1)
   {
    return arg0 == arg1;
   }

   @Override
   public void destroyItem (ViewGroup container, int position,
     Object object)
   {
    container.removeView ((View) object);
   }

   @Override
   public Object instantiateItem (ViewGroup container, int position)
   {
    ImageView imageView = new ImageView (MainActivity.this);
    imageView.setImageResource (mImgIds [position]);
    imageView.setScaleType (ScaleType.CENTER_CROP);
    container.addView (imageView);
    mViewPager.setObjectForPosition (imageView, position);
    return imageView;
   }

   @Override
   public int getCount ()
   {
    return mImgIds.length;
   }
  });

 }

}

The only difference is that our code:

// Set the transition effects

mViewPager.setTransitionEffect (TransitionEffect.Stack);

It has 12 selectable transition effects, in fact, wrote a 12 handover animation ~~~
     
         
         
         
  More:      
 
- How to use the command line to obtain Freely RSS source on Linux (Linux)
- RocketMQ Message Queuing simple deployment (Linux)
- Python substring format (Programming)
- VMware ghost Linux card error (Linux)
- Linux common network tools: batch scanning of nmap hosting service (Linux)
- Based shell: using read, the command-line script to achieve mass participation and input two integer calculation (Programming)
- Oracle 11g new features and associated SQL TUNING (Database)
- Java code JIT compiler-friendly Mody (Programming)
- Install Linux Mint 17: 20 things to do (Linux)
- ORA-01839 error caused by incorrect system date setting (Database)
- Linux group account deletion, and other related operations (Linux)
- 5 fast Node.js application performance tips (Programming)
- Extended VMware Ubuntu root partition size (Linux)
- Linux find command detailing (Linux)
- Hackers is how the invasion and control of Things devices? (Linux)
- Shell commonly commands (Linux)
- linux system optimization and security configuration (Linux)
- Introduction and use of the Raspberry Pi (Linux)
- MySQL5.7 JSON type using presentation (Database)
- Ubuntu Linux installation GAMIT10.6 (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.