Android项目实战(四):ViewPager切换动画(3.0版本以上有效果)

时间:2022-06-06 15:18:32

学习内容来自“慕课网”

一般APP进去之后都会有几张图片来导航,这里就学习怎么在这张图片切换的时候添加切换动画效果

先看布局文件

activity_main.layout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/frameLayout1"
android:orientation="vertical"> </FrameLayout>

然后是java文件

mainactivity.java

 package com.example.viewpage;

 import java.util.ArrayList;
import java.util.List; import android.os.Bundle;
import android.app.Activity;
import android.graphics.DashPathEffect;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.style.SuperscriptSpan;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
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);
//为ViewPage切换添加动画效果(3.0以上版本才可以有效果)
mviewpager.setPageTransformer(true, new ZoomOutPageTransformer()); mviewpager.setAdapter(new PagerAdapter() { @Override
public Object instantiateItem(View container, int position) {
ImageView imageview = new ImageView(MainActivity.this);
imageview.setImageResource(mImgIds[position]);
imageview.setScaleType(ScaleType.CENTER_CROP); //设置图片不变形
((ViewGroup) container).addView(imageview);
mImages.add(imageview);
return imageview;
};
@Override
public void destroyItem(View container, int position, Object object) {
// TODO Auto-generated method stub ((ViewPager) container).removeView(mImages.get(position));
}
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == object;
} @Override
public int getCount() { //返回图片个数
// TODO Auto-generated method stub
return mImgIds.length;
}
});
} }

这里代码很简单,主要就是一行代码控制切换效果 第36行mviewpager.setPageTransformer(true, new ZoomOutPageTransformer());

第二个参数就是切换效果,当然不是随便的参数

这些参数可以在http://wear.techbrood.com/training/animation/screen-slide.html网上查阅到

代码是给定的,只要复制代码,点击包粘贴就生成了相应的java文件

这里展示下几种效果:

1、ZoomOutPageTransformer()

 public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.85f;
private static final float MIN_ALPHA = 0.5f; public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
int pageHeight = view.getHeight(); if (position < -) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(); } else if (position <= ) { // [-1,1]
// Modify the default slide transition to shrink the page as well
float scaleFactor = Math.max(MIN_SCALE, - Math.abs(position));
float vertMargin = pageHeight * ( - scaleFactor) / ;
float horzMargin = pageWidth * ( - scaleFactor) / ;
if (position < ) {
view.setTranslationX(horzMargin - vertMargin / );
} else {
view.setTranslationX(-horzMargin + vertMargin / );
} // 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) /
( - MIN_SCALE) * ( - MIN_ALPHA)); } else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha();
}
}
}

效果图:

Android项目实战(四):ViewPager切换动画(3.0版本以上有效果)     Android项目实战(四):ViewPager切换动画(3.0版本以上有效果)

红色的为第一张,蓝色的为第二张,向左滑动显示蓝色。效果为两张图并行缩小滑动

2、DepthPageTransformer()

 public class DepthPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.75f; public void transformPage(View view, float position) {
int pageWidth = view.getWidth(); if (position < -) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(); } else if (position <= ) { // [-1,0]
// Use the default slide transition when moving to the left page
view.setAlpha();
view.setTranslationX();
view.setScaleX();
view.setScaleY(); } else if (position <= ) { // (0,1]
// Fade the page out.
view.setAlpha( - position); // Counteract the default slide transition
view.setTranslationX(pageWidth * -position); // Scale the page down (between MIN_SCALE and 1)
float scaleFactor = MIN_SCALE
+ ( - MIN_SCALE) * ( - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor); } else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha();
}
}
}

效果图:

Android项目实战(四):ViewPager切换动画(3.0版本以上有效果)   Android项目实战(四):ViewPager切换动画(3.0版本以上有效果)  Android项目实战(四):ViewPager切换动画(3.0版本以上有效果)

红色的为第一张,蓝色的为第二张,向左滑动显示蓝色。效果为红色无变化向左滑动,蓝色由淡到深,由小到大占据屏幕

注意:添加DepthPageTransformer()和ZoomOutPageTransformer()java文件的时候 有两个错误 很容易 ctrl+1都能解决

更多动画类:

下载地址:

http://yunpan.cn/cFyjVgaeIFZ4e (提取码:ff5f)