Home >Java >javaTutorial >How to implement ViewPager multi-page sliding switching and animation effects in Android

How to implement ViewPager multi-page sliding switching and animation effects in Android

高洛峰
高洛峰Original
2017-01-13 11:57:001224browse

The example of this article describes the method of Android programming to implement ViewPager multi-page sliding switching and animation effects. Share it with everyone for your reference, the details are as follows:

1. First, let’s take a look at the rendering, which is the Tab sliding effect of Sina Weibo. We can slide with gestures or click on the header above to switch. In the same way, the

white horizontal bar will move to the corresponding page card header. This is an animation effect, and the white bar slides past slowly. Okay, let's implement it next.

How to implement ViewPager multi-page sliding switching and animation effects in Android

#2. Before we start, we must first get to know a control, ViewPager. It is a class in an additional package that comes with Google SDk and can be used to switch between screens.

This additional package is android-support-v4.jar, which will be provided to everyone in the final source code, in the libs folder. Of course, you can also search for the latest version yourself from the Internet.

After finding it, we need to add it to the project

How to implement ViewPager multi-page sliding switching and animation effects in Android

3. Let’s do the interface first

The interface design is very simple, first There are three headers in one row, animated images in the second row, and page card content display in the third row.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:umadsdk="http://schemas.android.com/apk/res/com.LoveBus"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >
 <LinearLayout
  android:id="@+id/linearLayout1"
  android:layout_width="fill_parent"
  android:layout_height="100.0dip"
  android:background="#FFFFFF" >
  <TextView
   android:id="@+id/text1"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_weight="1.0"
   android:gravity="center"
   android:text="页卡1"
   android:textColor="#000000"
   android:textSize="22.0dip" />
  <TextView
   android:id="@+id/text2"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_weight="1.0"
   android:gravity="center"
   android:text="页卡2"
   android:textColor="#000000"
   android:textSize="22.0dip" />
  <TextView
   android:id="@+id/text3"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_weight="1.0"
   android:gravity="center"
   android:text="页卡3"
   android:textColor="#000000"
   android:textSize="22.0dip" />
 </LinearLayout>
 <ImageView
  android:id="@+id/cursor"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:scaleType="matrix"
  android:src="@drawable/a" />
 <android.support.v4.view.ViewPager
  android:id="@+id/vPager"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:layout_weight="1.0"
  android:background="#000000"
  android:flipInterval="30"
  android:persistentDrawingCache="animation" />
</LinearLayout>

We want to display three page cards, so we also need the interface design of the content of the three page cards. Here we only set the background color, which can make a difference.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical"
 android:background="#158684" >
</LinearLayout>

4. Initialization work is required in the code part

(1) Let’s first define the variables

private ViewPager mPager;//页卡内容
private List<View> listViews; // Tab页面列表
private ImageView cursor;// 动画图片
private TextView t1, t2, t3;// 页卡头标
private int offset = 0;// 动画图片偏移量
private int currIndex = 0;// 当前页卡编号
private int bmpW;// 动画图片宽度

(2) Initialize the header

/**
  * 初始化头标
*/
 private void InitTextView() {
  t1 = (TextView) findViewById(R.id.text1);
  t2 = (TextView) findViewById(R.id.text2);
  t3 = (TextView) findViewById(R.id.text3);
  t1.setOnClickListener(new MyOnClickListener(0));
  t2.setOnClickListener(new MyOnClickListener(1));
  t3.setOnClickListener(new MyOnClickListener(2));
 }
/**
  * 头标点击监听
*/
public class MyOnClickListener implements View.OnClickListener {
  private int index = 0;
  public MyOnClickListener(int i) {
   index = i;
  }
  @Override
  public void onClick(View v) {
   mPager.setCurrentItem(index);
  }
};

I believe everyone will have no problem after reading it. Click on which page the content of the card will be displayed.

(3) Initialize the page card content area

/**
  * 初始化ViewPager
*/
private void InitViewPager() {
  mPager = (ViewPager) findViewById(R.id.vPager);
  listViews = new ArrayList<View>();
  LayoutInflater mInflater = getLayoutInflater();
  listViews.add(mInflater.inflate(R.layout.lay1, null));
  listViews.add(mInflater.inflate(R.layout.lay2, null));
  listViews.add(mInflater.inflate(R.layout.lay3, null));
  mPager.setAdapter(new MyPagerAdapter(listViews));
  mPager.setCurrentItem(0);
  mPager.setOnPageChangeListener(new MyOnPageChangeListener());
}

We will load three page card interfaces into it, and the first page card will be displayed by default. Here we also need to implement an adapter.

/**
* ViewPager Adapter
*/
public class MyPagerAdapter extends PagerAdapter {
public List mListViews;
public MyPagerAdapter(List mListViews) {
this.mListViews = mListViews;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView(mListViews.get(arg1));
}
@Override
public void finishUpdate(View arg0) {
}
@Override
public int getCount() {
return mListViews.size();
}
@Override
public Object instantiateItem(View arg0, int arg1) {
((ViewPager) arg0).addView(mListViews.get(arg1), 0);
return mListViews.get( arg1);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == (arg1);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate( View arg0) {
}
}

Here we implement the loading and unloading of each page card

(3) Initialization animation

/**
  * 初始化动画
*/
private void InitImageView() {
  cursor = (ImageView) findViewById(R.id.cursor);
  bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a)
    .getWidth();// 获取图片宽度
  DisplayMetrics dm = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(dm);
  int screenW = dm.widthPixels;// 获取分辨率宽度
  offset = (screenW / 3 - bmpW) / 2;// 计算偏移量
  Matrix matrix = new Matrix();
  matrix.postTranslate(offset, 0);
  cursor.setImageMatrix(matrix);// 设置动画初始位置
}

According to the screen resolution Calculate the offset of animation movement with the width of the picture

How to implement ViewPager multi-page sliding switching and animation effects in Android

Realize page card switching monitoring

/**
  * 页卡切换监听
*/
public class MyOnPageChangeListener implements OnPageChangeListener {
  int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
  int two = one * 2;// 页卡1 -> 页卡3 偏移量
  @Override
  public void onPageSelected(int arg0) {
   Animation animation = null;
   switch (arg0) {
   case 0:
    if (currIndex == 1) {
     animation = new TranslateAnimation(one, 0, 0, 0);
    } else if (currIndex == 2) {
     animation = new TranslateAnimation(two, 0, 0, 0);
    }
    break;
   case 1:
    if (currIndex == 0) {
     animation = new TranslateAnimation(offset, one, 0, 0);
    } else if (currIndex == 2) {
     animation = new TranslateAnimation(two, one, 0, 0);
    }
    break;
   case 2:
    if (currIndex == 0) {
     animation = new TranslateAnimation(offset, two, 0, 0);
    } else if (currIndex == 1) {
     animation = new TranslateAnimation(one, two, 0, 0);
    }
    break;
   }
   currIndex = arg0;
   animation.setFillAfter(true);// True:图片停在动画结束位置
   animation.setDuration(300);
   cursor.startAnimation(animation);
  }
  @Override
  public void onPageScrolled(int arg0, float arg1, int arg2) {
  }
  @Override
  public void onPageScrollStateChanged(int arg0) {
  }
}

5. After finishing the work, come and take a look at your own The fruits of your labor

How to implement ViewPager multi-page sliding switching and animation effects in Android

#I hope this article will be helpful to everyone in Android programming.

For more related articles on how Android implements ViewPager multi-page sliding switching and animation effects, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn