码迷,mamicode.com
首页 > 其他好文 > 详细

justdoit --ViewPager_Annotation

时间:2014-11-15 23:18:23      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:android   viewpager   动效   

今天晚上电脑的转接口又出了问题,电脑一直联不了网;刚好手头的工作也告一段落了。好吧,择日不如撞日,今天就来写博客总结一下最近的学习经历吧。有些东西经常用却从未总结过,而有些东西是第一次接触,就通过一些列博客来做备忘吧!

一.android ViewPager滑动效果

首先,翻看developer文档,会发现常用的滑动效果有三种,分别是:default(默认效果),ZoomOut,以及Depth。而之所以要将这些呢,是因为LZ个人认为,对用户提供可选择的空间,让他选择一种自己喜欢的切换方式,会比较好。或者说,一种动效用太久了,来换另一种尝试一下,总比一成不变要来的好很多。而至于ViewPager切换动效使用也很简单。只需要在你的ViewPager实例调用setPageTransformer(true,DepthPageTransformer.getInstance())即可,第二个参数为你传入的工具类的静态工厂产生的实例,而默认效果即使第二个参数值为null。因为这个比较简单,所以直接贴上代码。

package wenyue.justdoit.util;

import android.support.v4.view.ViewPager;
import android.view.View;

public class DepthPageTransformer implements ViewPager.PageTransformer {
private static float MIN_SCALE = 0.75f;

private static DepthPageTransformer depthPageTransformer = new DepthPageTransformer();

private DepthPageTransformer() {

}

public  static DepthPageTransformer getInstance() {

    return depthPageTransformer;

}

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);
    }
}
}

接下来是ZoomOut效果的实现

package wenyue.justdoit.util;

import android.support.v4.view.ViewPager;
import android.view.View;

public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.85f;
private static final float MIN_ALPHA = 0.5f;


  private static ZoomOutPageTransformer zoomOutPageTransformer = new     ZoomOutPageTransformer();

private ZoomOutPageTransformer() {

}

public  static ZoomOutPageTransformer getInstance() {

    return zoomOutPageTransformer;

}

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);
    }
}
}

而一些具体的动效就可以通过在工具类里面设置相应的参数来实现。这里就不详细列举了。接下来,就来写一下关于自定义控件的一些事吧。好吧,我也是醉了,为了限制篇幅,重写一篇博客吧!

justdoit --ViewPager_Annotation

标签:android   viewpager   动效   

原文地址:http://blog.csdn.net/canglangwenyue/article/details/41153795

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!