标签:metro android android开发 launcher google
Launcher主体框架实现:
这个是通过获取ViewPager的Scroller,对Scroller重新设置一些参数就可以了,具体实现的代码如下:
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);
@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); }
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两侧page部分显示可以通过设置ViewPager的setPageMargin(int margin)方法实现,具体代码如下:
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) { } } }
欢迎关注个人微信公众平台:程序员互动联盟(coder_online),扫一扫下方二维码或搜索微信号coder_online即可关注,我们可以在线交流。
Android Metro风格的Launcher开发系列第二篇
标签:metro android android开发 launcher google
原文地址:http://blog.csdn.net/lihui130135/article/details/44782329