Java tutorial
/* * Copyright (c) 2012 Wireless Designs, LLC * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.paic.zhifu.wallet.activity.modules.redpacket; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import android.content.Context; import android.graphics.Paint; import android.graphics.Point; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.FrameLayout; /** * PagerContainer: A layout that displays a ViewPager with its children that are outside * the typical pager bounds. */ public class PagerContainer extends FrameLayout implements ViewPager.OnPageChangeListener { private ViewPager mPager; boolean mNeedsRedraw = false; public PagerContainer(Context context) { super(context); init(); } public PagerContainer(Context context, AttributeSet attrs) { super(context, attrs); init(); } public PagerContainer(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { //Disable clipping of children so non-selected pages are visible setClipChildren(false); //Child clipping doesn't work with hardware acceleration in Android 3.x/4.x //You need to set this value here if using hardware acceleration in an // application targeted at these releases. // setLayerType(View.LAYER_TYPE_SOFTWARE, null); try { Method setLayerTypeMethod = this.getClass().getMethod("setLayerType", new Class[] { int.class, Paint.class }); setLayerTypeMethod.invoke(this, new Object[] { LAYER_TYPE_SOFTWARE, null }); } catch (NoSuchMethodException e) { // Older OS, no HW acceleration anyway } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } @Override protected void onFinishInflate() { try { mPager = (ViewPager) getChildAt(0); mPager.setOnPageChangeListener(this); } catch (Exception e) { throw new IllegalStateException("The root child of PagerContainer must be a ViewPager"); } } public ViewPager getViewPager() { return mPager; } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { //Force the container to redraw on scrolling. //Without this the outer pages render initially and then stay static if (mNeedsRedraw) invalidate(); } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { mNeedsRedraw = (state != ViewPager.SCROLL_STATE_IDLE); } }