android 3d翻转动画 viewpage,ViewPager3D旋转效果

AAffA0nNPuCLAAAAAElFTkSuQmCC这是一个3D旋转效果的viewPager:

ViewPager viewPager= (ViewPager) findViewById(R.id.viewPager);

PagerAdapter实现几个方法

getCount:返回数据源size

isViewFromObject()你pagerAdapter选中的内容是不是当成一个对象来处理

的意思吧

instantiateItem:instantiateItem将view添加,并且返回

destroyItem:移除一个view

3D旋转动画的实现

viewPager.setPageTransformer(true, transformer);

设置自定义旋转动画;

@Override

public void onTransform(View view, float position) {

float rotation = 180f * position;

view.setTranslationX(view.getWidth() * -position);

view.setAlpha(rotation > 90f || rotation < -90f ? 0 : 1);

view.setPivotX(view.getWidth() * 0.5f);

view.setPivotY(view.getHeight() * 0.5f);

view.setRotationY(rotation);

if (position > -0.5f && position < 0.5f) {

view.setVisibility(View.VISIBLE);

} else {

view.setVisibility(View.INVISIBLE);

}

}

postion代表旋转的,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一直都很喜欢Instagram的快拍(Story)功能,也很喜欢他们的翻转效果,是一种简单的3D翻转效果。大致效果如下:instagramstory.gif貌似最近微博也出了一个差不多的Story的功能,用的翻转动画也是和Instagram一样。思路看到这样的效果,很容易想到用ViewPager的Transformer动画来实现。当然这种翻转效果网上也有相应的实现,就是以View的左边或右边为旋转轴进行空间上Y轴的旋转。于是很容易我们可以写出下面的代码public class StereoPagerTransformer implements ViewPager.PageTransformer {   private static final float MAX_ROTATE_Y = 90;   private final float pageWidth;   public StereoPagerTransformer(float pageWidth) {     this.pageWidth = pageWidth;   }   public void transformPage(View view, float position) {     view.setPivotY(view.getHeight() / 2);     if (position < -1) { // [-Infinity,-1)       // This page is way off-screen to the left.       view.setPivotX(0);       view.setRotationY(90);     } else if (position <= 0) { // [-1,0]       view.setPivotX(pageWidth);       view.setRotationY(position * MAX_ROTATE_Y);     } else if (position <= 1) { // (0,1]       view.setPivotX(0);       view.setRotationY(position * MAX_ROTATE_Y);     } else { // (1, Infinity]       // This page is way off-screen to the right.       view.setPivotX(0);       view.setRotationY(90);     }   } }然后运行代码看一下我们实现的效果:badtransformer.gif额,总觉哪里不对,嗯,就是动画的速度不对,上面的写法就是一个匀速进行的动画效果非常不理想,这时我们就要重新写一个插值器(TimeInterpolator ),在尝试了系统自带的差值器后,发现效果仍然不是很理想。于是决定自己写一个插值器。关于插值器根据TimeInterpolator代码中的文档可以得知,插值器用于控制动画进行的速度,其输入值为0~1,输出值也为0~1。/**  * A time interpolator defines the rate of change of an animation. This allows animations  * to have non-linear motion, such as acceleration and deceleration.  */ public interface TimeInterpolator {     /**      * Maps a value representing the elapsed fraction of an animation to a value that represents      * the interpolated fraction. This interpolated value is then multiplied by the change in      * value of an animation to derive the animated value at the current elapsed animation time.      *      * @param input A value between 0 and 1.0 indicating our current point      *        in the animation where 0 represents the start and 1.0 represents      *        the end      * @return The interpolation value. This value can be more than 1.0 for      *         interpolators which overshoot their targets, or less than 0 for      *         interpolators that undershoot their targets.      */     float getInterpolation(float input); }经过简单的分析,这次我们的动画应该在动画前半段时进行缓慢一些(也就是输入值在0到某个值之间),在后半段时(也就是输入值在某个值到1之间)进行的快速一些。经过简单的调整,最终我写了如下的插值器private static final TimeInterpolator sInterpolator = new TimeInterpolator() {         @Override         public float getInterpolation(float input) {             if (input < 0.7) {                 return input * (float) pow(0.7, 3) * MAX_ROTATE_Y;             } else {                 return (float) pow(input, 4) * MAX_ROTATE_Y;             }         }     };再次运行代码,这次效果看上去好多了,哈哈,一个简单又好看的效果就完成了。goodtransformer.gif最后附上完整的代码:import android.animation.TimeInterpolator; import android.support.v4.view.ViewPager; import android.view.View; import static java.lang.Math.pow; /**  * @author wupanjie  */ public class StereoPagerTransformer implements ViewPager.PageTransformer {     private static final float MAX_ROTATE_Y = 90;     private static final TimeInterpolator sInterpolator = new TimeInterpolator() {         @Override         public float getInterpolation(float input) {             if (input < 0.7) {                 return input * (float) pow(0.7, 3) * MAX_ROTATE_Y;             } else {                 return (float) pow(input, 4) * MAX_ROTATE_Y;             }         }     };     private final float pageWidth;     public StereoPagerTransformer(float pageWidth) {         this.pageWidth = pageWidth;     }     public void transformPage(View view, float position) {         view.setPivotY(view.getHeight() / 2);         if (position < -1) { // [-Infinity,-1)             // This page is way off-screen to the left.             view.setPivotX(0);             view.setRotationY(90);         } else if (position <= 0) { // [-1,0]             view.setPivotX(pageWidth);             view.setRotationY(-sInterpolator.getInterpolation(-position));         } else if (position <= 1) { // (0,1]             view.setPivotX(0);             view.setRotationY(sInterpolator.getInterpolation(position));         } else { // (1, Infinity]             // This page is way off-screen to the right.             view.setPivotX(0);             view.setRotationY(90);         }     } }总结动画的灵魂在于它的插值器,插值器控制了动画进行的速度,这次我选择自己写了一个插值器作为练手,其实我这次写的插值器效果仍不是很平滑,动画的插值器也应该用贝塞尔曲线来制作,这样我们的动画就会进行的更平滑。具体大家可以参考自带的PathInterpolator,是在API 21以后引入的,当然它也有对应的兼容包。
好的,下面是使用ViewPager和TabLayout实现滑动效果的步骤: 1.在布局文件中添加ViewPager和TabLayout组件 ```xml <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" app:tabMode="scrollable" app:tabTextColor="@android:color/white" app:tabSelectedTextColor="@android:color/white" app:tabIndicatorColor="@android:color/white" app:tabIndicatorHeight="3dp" /> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 2.在Java代码中初始化ViewPager和TabLayout组件 ```java ViewPager viewPager = findViewById(R.id.viewPager); TabLayout tabLayout = findViewById(R.id.tabLayout); MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter); tabLayout.setupWithViewPager(viewPager); ``` 3.实现FragmentPagerAdapter类,用于管理ViewPager中的Fragment ```java public class MyFragmentPagerAdapter extends FragmentPagerAdapter { private String[] titles = {"Tab 1", "Tab 2", "Tab 3"}; public MyFragmentPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position) { case 0: return new Fragment1(); case 1: return new Fragment2(); case 2: return new Fragment3(); default: return null; } } @Override public int getCount() { return titles.length; } @Nullable @Override public CharSequence getPageTitle(int position) { return titles[position]; } } ``` 4.实现三个Fragment类,用于显示在ViewPager中的内容 ```java public class Fragment1 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment1_layout, container, false); return view; } } public class Fragment2 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment2_layout, container, false); return view; } } public class Fragment3 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment3_layout, container, false); return view; } } ``` 5.创建三个布局文件fragment1_layout.xml、fragment2_layout.xml、fragment3_layout.xml,用于显示在ViewPager中的内容 ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:text="Fragment 1" android:textSize="30sp" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> ``` ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:text="Fragment 2" android:textSize="30sp" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> ``` ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:text="Fragment 3" android:textSize="30sp" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> ``` 以上就是使用ViewPager和TabLayout实现滑动效果的步骤,希望对你有帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值