标签:千变万化的viewpager切换动画-仅 viewpager
(1)创建一个项目Viewpager_anim 添加图片资源guide_image1.png、guide_image2.png、guide_image3.png
编写activity_main.xml
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <!-- ctrl+shitf+t 可以找到viewpager的包名 --> <android.support.v4.view.ViewPager android:id="@+id/id_viewpager" android:layout_width="fill_parent" android:layout_height="fill_parent" > </android.support.v4.view.ViewPager> </RelativeLayout>编写MainActivity.java
package com.lc.viewpager_anim; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.ImageView; import android.widget.ImageView.ScaleType; public class MainActivity extends Activity { private ViewPager mViewPager; private int[] mImgIds = new int[] { R.drawable.guide_image1, R.drawable.guide_image2, R.drawable.guide_image3 }; private List<ImageView> mImages = new ArrayList<ImageView>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mViewPager = (ViewPager) findViewById(R.id.id_viewpager); /* * 为viewpager添加动画效果 */ // 1.第一种动画效果 // mViewPager.setPageTransformer(true, new DepthPageTransformer()); // 2.第二种动画效果 mViewPager.setPageTransformer(true, new ZoomOutPageTransformer()); // 添加适配器 mViewPager.setAdapter(new PagerAdapter() { /** * 初始化代码 */ @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); mImages.add(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(mImages.get(position)); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public int getCount() { return mImgIds.length; } }); } }
ZoomOutPageTransformer.java(从文档中粘贴过来的时候可能有一些错误,只需要修改一下即可)
package com.lc.viewpager_anim; import android.annotation.SuppressLint; import android.support.v4.view.ViewPager; import android.view.View; public class ZoomOutPageTransformer implements ViewPager.PageTransformer { private static float MIN_SCALE = 0.85f; private static float MIN_ALPHA = 0.5f; @SuppressLint("NewApi") public void transformPage(View view, float position) { int pageWidth = view.getWidth(); int pageHeight = view.getHeight(); if (position < -1) { // [-Infinity,-1) // This page is way off-screen to the left. view.setAlpha(0); } else if (position <= 1) { // [-1,1] // Modify the default slide transition to shrink the page as well float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position)); float vertMargin = pageHeight * (1 - scaleFactor) / 2; float horzMargin = pageWidth * (1 - scaleFactor) / 2; if (position < 0) { view.setTranslationX(horzMargin - vertMargin / 2); } else { view.setTranslationX(-horzMargin + vertMargin / 2); } // Scale the page down (between MIN_SCALE and 1) view.setScaleX(scaleFactor); view.setScaleY(scaleFactor); // Fade the page relative to its size. view.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA)); } else { // (1,+Infinity] // This page is way off-screen to the right. view.setAlpha(0); } } }
package com.lc.viewpager_anim; import android.annotation.SuppressLint; import android.support.v4.view.ViewPager; import android.view.View; /* * 这里的动画效果只有在3.0以上有效果 */ public class DepthPageTransformer implements ViewPager.PageTransformer { private static float MIN_SCALE = 0.75f; @SuppressLint("NewApi") 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(0); view.setScaleX(1); view.setScaleY(1); } else if (position <= 1) { // (0,1] // Fade the page out. view.setAlpha(1 - position); // Counteract the default slide transition view.setTranslationX(pageWidth * -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); } } }
千变万化的ViewPager切换动画(1)--仅支持3.0以上版本的官方方法
标签:千变万化的viewpager切换动画-仅 viewpager
原文地址:http://blog.csdn.net/xlgen157387/article/details/44033975