首页 > 代码库 > justdoit --ViewPager_Annotation
justdoit --ViewPager_Annotation
今天晚上电脑的转接口又出了问题,电脑一直联不了网;刚好手头的工作也告一段落了。好吧,择日不如撞日,今天就来写博客总结一下最近的学习经历吧。有些东西经常用却从未总结过,而有些东西是第一次接触,就通过一些列博客来做备忘吧!
一.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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。