标签:
前言:
Launcher主体框架实现:
这个是通过获取ViewPager的Scroller,对Scroller重新设置一些参数就可以了,具体实现的代码如下:
1
2
3
4
5
|
Field Scroller = ViewPager. class .getDeclaredField( "mScroller" ); Scroller.setAccessible( true ); Interpolator interpolator = new LinearInterpolator(); //设置加速器 ViewPagerScroller scroller = new ViewPagerScroller(context,interpolator); //重新设置ViewPager的Scroller Scroller.set( this , scroller); |
在上面的代码中用到了ViewPagerScroller.java,在ViewPagerScroller中的设置如下,其中的mDuration变量是自定义的动画时间,这个你可以根据自己的动画效果来设置时间长度,我这定义的是500毫秒。
1
2
3
4
5
6
7
8
9
10
11
|
@Override public void startScroll( int startX, int startY, int dx, int dy) { // Ignore received duration, use fixed one instead super .startScroll(startX, startY, dx, dy, mDuration); } @Override public void startScroll( int startX, int startY, int dx, int dy, int duration) { // Ignore received duration, use fixed one instead super .startScroll(startX, startY, dx, dy, mDuration); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
class LauncherPageTransformer implements ViewPager.PageTransformer { private static float DEFAULT_SCALE = 1 .0f; private static float SCALE_FACTOR = 0 .30f; // 缩放因子 0.50f private static float ROTATION_FACTOR = 20f; // 旋转因子 private static float ALPHA_FACTOR = 0 .8f; @Override public void transformPage(View view, float position) { if (position <= 1 ) { // [-1,1] // Modify the default slide transition to shrink the page as well if (position < 0 ) { // view.setRotationY(position * ROTATION_FACTOR); view.setScaleX(SCALE_FACTOR * position + DEFAULT_SCALE); view.setScaleY(SCALE_FACTOR * position + DEFAULT_SCALE); // view.setAlpha(ALPHA_FACTOR * position + 1.0f); } else { // view.setRotationY(position * ROTATION_FACTOR); view.setScaleX(SCALE_FACTOR * -position + DEFAULT_SCALE); view.setScaleY(SCALE_FACTOR * -position + DEFAULT_SCALE); // view.setAlpha(ALPHA_FACTOR * -position + 1.0f); } } } } |
具体效果可以通过修改旋转和缩放实现。
主屏幕page两侧page部分显示可以通过设置ViewPager的setPageMargin(int margin)方法实现,具体代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
public class LauncherViewPager extends ViewPager { public static final int PAGE_LIMIT = 3 ; public LauncherViewPager(Context context) { this (context, null ); } public LauncherViewPager(Context context, AttributeSet attrs) { super (context, attrs); init(context); } private void init(Context context) { this .setPageMargin(-getResources().getInteger(R.integer.portal_viewpager_margin)); this .setOffscreenPageLimit(PAGE_LIMIT); this .setPageTransformer( true , new LauncherPageTransformer()); try { Field Scroller = ViewPager. class .getDeclaredField( "mScroller" ); Scroller.setAccessible( true ); Interpolator interpolator = new LinearInterpolator(); ViewPagerScroller scroller = new ViewPagerScroller(context, interpolator); Scroller.set( this , scroller); } catch (NoSuchFieldException e) { } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } } } |
Android Metro风格的Launcher开发系列第二篇
标签:
原文地址:http://www.cnblogs.com/donghuizaixian/p/4381604.html