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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| /**
* Based on: https://gist.github.com/devunwired/8cbe094bb7a783e37ad1
*/
package trucmuche;
import les_imports_qui_vont_bien;
public class SupportGallery extends FrameLayout implements OnPageChangeListener, ViewPager.OnTouchListener {
private ViewPager mPager;
boolean mNeedsRedraw = false;
public SupportGallery(Context context) {
super(context);
init();
}
public SupportGallery(Context context, AttributeSet attrs) {
super(context, attrs);
init();
parseAttrs(context, attrs);
}
public SupportGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
parseAttrs(context, attrs);
}
private void init() {
mPager = new ViewPager(getContext());
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT, Gravity.CENTER);
mPager.setLayoutParams(layoutParams);
mPager.setBackgroundResource(R.drawable.drawable_gallery_selected_page);
addView(mPager);
// Disable clipping of children so non-selected pages are visible
setClipChildren(false);
disableHardwareAcceleration();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void disableHardwareAcceleration() {
// 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.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
}
private void parseAttrs(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SupportGallery);
float width = typedArray.getDimension(R.styleable.SupportGallery_widthElement, -1);
if (width != -1) {
mPager.getLayoutParams().width = (int) width;
}
float height = typedArray.getDimension(R.styleable.SupportGallery_heightElement, -1);
if (height != -1) {
mPager.getLayoutParams().height = (int) height;
}
typedArray.recycle();
}
@Override
protected void onFinishInflate() {
try {
mPager.setOnPageChangeListener(this);
mPager.setOnTouchListener(this);
} catch (Exception e) {
throw new IllegalStateException("The root child of PagerContainer must be a ViewPager");
}
}
public ViewPager getViewPager() {
return mPager;
}
private Point mCenter = new Point();
private Point mInitialTouch = new Point();
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mCenter.x = w / 2;
mCenter.y = h / 2;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// We capture any touches not already handled by the ViewPager
// to implement scrolling from a touch outside the pager bounds.
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mInitialTouch.x = (int) ev.getX();
mInitialTouch.y = (int) ev.getY();
case MotionEvent.ACTION_UP :
mInitialTouch.x = (int) ev.getX();
mInitialTouch.y = (int) ev.getY();
default:
ev.offsetLocation(mCenter.x - mInitialTouch.x, mCenter.y - mInitialTouch.y);
break;
}
return mPager.dispatchTouchEvent(ev);
}
@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) {
invalidate();
}
@Override
public void onPageScrollStateChanged(int state) {
mNeedsRedraw = (state != ViewPager.SCROLL_STATE_IDLE);
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
invalidate();
return super.onTouchEvent(arg1);
}
} |
Partager