출처 : http://blog.naver.com/ldb86/130176257039
1. SlideActivity (Fragment 형식)
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SlideActivity extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//SlidingView 인스턴스화 (Activity의 Context가 필요, getActvity로 Context값을 가져옴)
SlidingView sv = new SlidingView(getActivity());
//2개의 Layout xml파일이 필요함. xml을 inflate시켜 붙임.
View v1 = View.inflate(getActivity(), R.layout.activity_slide1, null);
View v2 = View.inflate(getActivity(), R.layout.activity_slide2, null);
//SlidingView에 xml을 add함.
sv.addView(v1);
sv.addView(v2);
//Activity를 상속받지 않고 Fragment를 상속받으면 onCreate에서 화면을 보여주지않고 onCreateView로 화면을 보여주어야 하므로 리턴시킴.
return sv;
}
}
2. SlidingView
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewGroup;
import android.widget.Scroller;
import android.widget.Toast;
public class SlidingView extends ViewGroup {
private VelocityTracker mVelocityTracker = null;
private static final int SNAP_VELOCITY = 100;
private int mTouchStop = 10;
private Bitmap mWallPaper = null;
private Paint mPaint = null;
private Scroller mScroller = null;
private PointF mLastPoint = null;
private int mCurPage = 0;
private int mCurTouchState;
private static final int TOUCH_STATE_SCROLLING = 0;
private static final int TOUCH_STATE_NORMAL = 1;
private Toast mToast;
public SlidingView(Context context)
{
super(context);
init();
}
public SlidingView(Context context, AttributeSet attrs){
super(context,attrs);
init();
}
public SlidingView(Context context, AttributeSet attrs, int defStyle)
{
super(context,attrs,defStyle);
init();
}
private void init()
{
mWallPaper = BitmapFactory.decodeResource(getResources(),R.drawable.portrait);
mPaint = new Paint();
mScroller = new Scroller(getContext());
mLastPoint = new PointF();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
for(int i=0;i<getChildCount();i++)
{
getChildAt(i).measure(widthMeasureSpec,heightMeasureSpec);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
for(int i=0;i<getChildCount();i++)
{
int w = getChildAt(i).getMeasuredWidth() * i;
getChildAt(i).layout(w, t, w+getChildAt(i).getMeasuredWidth(),getChildAt(i).getMeasuredHeight());
}
}
@Override
protected void dispatchDraw(Canvas canvas) {
canvas.drawBitmap(mWallPaper, 0, 0, mPaint);
for(int i=0; i<getChildCount();i++)
{
drawChild(canvas,getChildAt(i),100);
}
super.dispatchDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(mVelocityTracker == null)
mVelocityTracker = VelocityTracker.obtain();
mVelocityTracker.addMovement(event);
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
if(mScroller.isFinished())
{
mScroller.abortAnimation();
}
mLastPoint.set(event.getX(),event.getY());
break;
case MotionEvent.ACTION_MOVE:
int x = (int)(event.getX() - mLastPoint.x);
scrollBy(-x,0);
invalidate();
mLastPoint.set(event.getX(),event.getY());
break;
case MotionEvent.ACTION_UP:
mVelocityTracker.computeCurrentVelocity(1000);
int v = (int) mVelocityTracker.getXVelocity();
int gap = getScrollX() - mCurPage * getWidth();
int nextPage = mCurPage;
if((v > SNAP_VELOCITY || gap < -getWidth() /2) && mCurPage > 0){
nextPage--;
}
else if((v < -SNAP_VELOCITY || gap > getWidth() / 2) && mCurPage < getChildCount() - 1){
nextPage++;
}
int move = 0;
if(mCurPage != nextPage){
move = getChildAt(0).getWidth() * nextPage - getScrollX();
} else{
move = getWidth() * mCurPage - getScrollX();
}
mScroller.startScroll(getScrollX(),0,move,0,Math.abs(move));
invalidate();
mCurPage = nextPage;
mCurTouchState = TOUCH_STATE_NORMAL;
mVelocityTracker.recycle();
mVelocityTracker = null;
break;
}
return true;
}
public void computeScroll(){
super.computeScroll();
if(mScroller.computeScrollOffset()){
scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
invalidate();
}
}
@Override
public boolean onInterceptHoverEvent(MotionEvent event) {
int action = event.getAction();
int x = (int) event.getX();
int y = (int) event.getY();
switch (action)
{
case MotionEvent.ACTION_DOWN:
mCurTouchState = mScroller.isFinished() ? TOUCH_STATE_NORMAL : TOUCH_STATE_SCROLLING;
mLastPoint.set(x,y);
break;
case MotionEvent.ACTION_MOVE:
int move_x = Math.abs(x-(int)mLastPoint.x);
if(move_x > mTouchStop){
mCurTouchState = TOUCH_STATE_SCROLLING;
mLastPoint.set(x,y);
}
break;
}
return mCurTouchState == TOUCH_STATE_SCROLLING;
}
}