博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
viewPager的切换动画
阅读量:6392 次
发布时间:2019-06-23

本文共 8051 字,大约阅读时间需要 26 分钟。

今天在看苏州通的代码,里面有个引导的代码,涉及到viewPager的切换动画:

DepthPageTransformer :
1 package com.soyoungboy.guide; 2 import android.annotation.TargetApi; 3 import android.os.Build; 4 import android.support.v4.view.ViewPager; 5 import android.view.View; 6 /** 7 * 8 * @ClassName: DepthPageTransformer 9 * @Description: TODO(viewPager切换动画)10 *11 */12 @TargetApi(Build.VERSION_CODES.HONEYCOMB)13 publicclassDepthPageTransformerimplementsViewPager.PageTransformer{14 privatestaticfloat MIN_SCALE =0.75f;15 /**16 * @Name transformPage17 * @Description TODO(这里用一句话描述这个方法的作用)18 * @param view19 * 滑动中的那个view20 * @param position21 * position这里是float类型,不是平时理解的int位置,而是当前滑动状态的一个表示,比如当滑动到正全屏时,22 * position是023 * ,而向左滑动,使得右边刚好有一部被进入屏幕时,position是1,如果前一页和下一页基本各在屏幕占一半时24 * ,前一页的position是25 * -0.5,后一页的posiotn是0.5,所以根据position的值我们就可以自行设置需要的alpha,x/y信息26 * @see android.support.v4.view.ViewPager.PageTransformer#transformPage(android.view.View,27 * float)28 * @Date 2015-5-25 下午3:13:1829 **/30 @Override31 publicvoid transformPage(View view,float position){32 // TODO Auto-generated method stub33 int pageWidth = view.getWidth();34 if(position <-1){35 //前一页36 view.setAlpha(0);37 }elseif(position <=0){38 view.setAlpha(1);39 view.setTranslationX(0);40 view.setScaleX(1);41 view.setScaleY(1);42 }elseif(position <=1){43 //后一页44 view.setAlpha(1- position);45 view.setTranslationX(pageWidth *-position);46 float scaleFactor = MIN_SCALE +(1- MIN_SCALE)47 *(1-Math.abs(position));48 view.setScaleX(scaleFactor);49 view.setScaleY(scaleFactor);50 }else{51 view.setAlpha(0);52 }53 }54 }

 

 
ZoomOutPageTransformer :
1 package com.soyoungboy.guide; 2 import android.annotation.TargetApi; 3 import android.os.Build; 4 import android.support.v4.view.ViewPager.PageTransformer; 5 import android.view.View; 6 @TargetApi(Build.VERSION_CODES.HONEYCOMB) 7 publicclassZoomOutPageTransformerimplementsPageTransformer{ 8 privatestaticfloat MIN_SCALE =0.85f; 9 privatestaticfloat MIN_ALPHA =0.5f;10 @Override11 publicvoid transformPage(View view,float position){12 int pageWidth = view.getWidth();13 int pageHeight = view.getHeight();14 if(position <-1){
// [-Infinity,-1)15 // This page is way off-screen to the left.16 view.setAlpha(0);17 }elseif(position <=1){
// [-1,1]18 // Modify the default slide transition to19 // shrink the page as well20 float scaleFactor =Math.max(MIN_SCALE,1-Math.abs(position));21 float vertMargin = pageHeight *(1- scaleFactor)/2;22 float horzMargin = pageWidth *(1- scaleFactor)/2;23 if(position <0){24 view.setTranslationX(horzMargin - vertMargin /2);25 }else{26 view.setTranslationX(-horzMargin + vertMargin /2);27 }28 // Scale the page down (between MIN_SCALE and 1)29 view.setScaleX(scaleFactor);30 view.setScaleY(scaleFactor);31 // Fade the page relative to its size.32 view.setAlpha(MIN_ALPHA +(scaleFactor - MIN_SCALE)33 /(1- MIN_SCALE)*(1- MIN_ALPHA));34 }else{
// (1,+Infinity]35 // This page is way off-screen to the right.36 view.setAlpha(0);37 }38 }39 }

 

activity里面使用动画:
1 package com.soyoungboy.guide;  2 import java.util.ArrayList;  3 import java.util.List;  4 import android.app.Activity;  5 import android.content.Context;  6 import android.content.SharedPreferences;  7 import android.content.SharedPreferences.Editor;  8 import android.os.Bundle;  9 import android.support.v4.view.PagerAdapter; 10 import android.support.v4.view.ViewPager; 11 import android.support.v4.view.ViewPager.OnPageChangeListener; 12 import android.view.LayoutInflater; 13 import android.view.View; 14 import android.view.View.OnClickListener; 15 import android.view.animation.Animation; 16 import android.view.animation.AnimationSet; 17 import android.view.animation.AnimationUtils; 18 import android.widget.Button; 19 import android.widget.ImageView; 20 import android.widget.TextView; 21 publicclassMainActivityextendsActivityimplementsOnPageChangeListener{ 22 privateTextView pageNum; 23 privateViewPager vp; 24 privateList
views; 25 privateViewPagerAdapter vpAdapter; 26 privateLayoutInflater inflater; 27 @Override 28 protectedvoid onCreate(Bundle savedInstanceState){ 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.activity_main); 31 inflater =LayoutInflater.from(this); 32 initViews(); 33 initPageNum(); 34 } 35 privatevoid initPageNum(){ 36 pageNum =(TextView) findViewById(R.id.page_num); 37 pageNum.setText(""); 38 } 39 privatevoid initViews(){ 40 views =newArrayList
(); 41 /** 42 * 要加载的引导图片 43 */ 44 views.add(inflater.inflate(R.layout.views_one,null)); 45 views.add(inflater.inflate(R.layout.views_two,null)); 46 views.add(inflater.inflate(R.layout.views_three,null)); 47 views.add(inflater.inflate(R.layout.views_four,null)); 48 vpAdapter =newViewPagerAdapter(views,this); 49 vp =(ViewPager) findViewById(R.id.viewpager); 50 vp.setPageTransformer(true,newDepthPageTransformer()); 51 // vp.setPageTransformer(true, new ZoomOutPageTransformer()); 52 vp.setAdapter(vpAdapter); 53 vp.setOnPageChangeListener(this); 54 } 55 publicclassViewPagerAdapterextendsPagerAdapter{ 56 privateList
views; 57 privateActivity activity; 58 publicViewPagerAdapter(List
views,Activity activity){ 59 this.views = views; 60 this.activity = activity; 61 } 62 @Override 63 publicvoid destroyItem(View arg0,int arg1,Object arg2){ 64 ((ViewPager) arg0).removeView(views.get(arg1)); 65 } 66 @Override 67 publicint getCount(){ 68 if(views !=null){ 69 return views.size(); 70 } 71 return0; 72 } 73 @Override 74 publicObject instantiateItem(View arg0,int arg1){ 75 ((ViewPager) arg0).addView(views.get(arg1),0); 76 if(arg1 ==0){ 77 AnimationSet animationSet =newAnimationSet(true); 78 Animation alphaAnimation =AnimationUtils.loadAnimation( 79 MainActivity.this, R.anim.alpha); 80 Animation tAnimation =AnimationUtils.loadAnimation( 81 MainActivity.this, R.anim.trans); 82 animationSet.addAnimation(alphaAnimation); 83 animationSet.addAnimation(tAnimation); 84 ImageView imageView =(ImageView) arg0 85 .findViewById(R.id.first_image); 86 imageView.startAnimation(animationSet); 87 } 88 if(arg1 == views.size()-1){ 89 Button mStart =(Button) arg0.findViewById(R.id.mstart); 90 mStart.setOnClickListener(newOnClickListener(){ 91 @Override 92 publicvoid onClick(View v){ 93 setGuided(); 94 goHome(); 95 } 96 }); 97 } 98 return views.get(arg1); 99 }100 @Override101 publicboolean isViewFromObject(View arg0,Object arg1){102 return(arg0 == arg1);103 }104 publicvoid goHome(){105 // Intent intent = new Intent(activity, ContentActivity.class);106 // activity.startActivity(intent);107 // overridePendingTransition(android.R.anim.fade_in,108 // android.R.anim.fade_out);109 // activity.finish();110 }111 publicvoid setGuided(){112 SharedPreferences preferences = activity.getSharedPreferences(113 "first_pref",Context.MODE_PRIVATE);114 Editor editor = preferences.edit();115 editor.putBoolean("isFirst",false);116 editor.commit();117 }118 }119 @Override120 publicvoid onPageScrollStateChanged(int arg0){121 // TODO Auto-generated method stub122 }123 @Override124 publicvoid onPageScrolled(int arg0,float arg1,int arg2){125 // TODO Auto-generated method stub126 }127 @Override128 publicvoid onPageSelected(int arg0){129 if(arg0 ==0){130 pageNum.setText("");131 }else{132 pageNum.setTextColor(0xFF767676);133 pageNum.setText(arg0 +1+" - 4");134 }135 }136 publicvoid onResume(){137 super.onResume();138 }139 publicvoid onPause(){140 super.onPause();141 }142 }

 

activity_main.xml
 
  1. 1 
    6
    10
    20 21 alpha.xml22
    23
    25
    29 30

     

trans.xml
1 
2
4
10
11

 

views_one.xml
  1. 1 
    2
    7
    14

     

views_two.xml
  1. 1 
    2
    8
    14

     

views_three.xml
  1.  

views_four.xml
  1. 1 
    2
    7
    13
    23

源码demo下载地址:

https://github.com/soyoungboy/Guide

对应引用:

https://github.com/soyoungboy/appcompat_v7

转载地址:http://krpha.baihongyu.com/

你可能感兴趣的文章
Proxmox VE 部署维护
查看>>
Linux软件包安装与卸载
查看>>
centos5.x安装sphinx
查看>>
3分钟搭建Ant Design Pro前端开发环境( MyClouds的前端选型)
查看>>
Scala各种用法
查看>>
Linux系统常用命令(二)
查看>>
简单的工厂模式学习
查看>>
温习如何画E-R图
查看>>
eclispe注释模板
查看>>
Thymeleaf教程 (三) 创建一个多语言的首页
查看>>
OSChina 周六乱弹 ——你们猜狗的舌头有多长
查看>>
OSChina 周日乱弹 —— 爱丽丝爱吃京酱肉丝
查看>>
2018.11月微信小程序优质开源项目
查看>>
IOS 未来几年的认知
查看>>
解决中文乱码--加密
查看>>
浅析全民社交创业梦
查看>>
Java操纵MongoDB_1(环境设置)
查看>>
C#字符串操作--获取字符或字符串的位置、数量
查看>>
php - 字符串处理
查看>>
bulk collect 以及ref cursor使用
查看>>