@@ -67,10 +67,11 @@ public class SplitLayout extends ViewGroup {
private Paint mPaint = new Paint ( ) ;
private int dragForgroundColor = Color . argb ( 0 . 88f , 1f , 1f , 1f ) ; // 默认的前景色
private int splitDragTouchCount = 2 ; // 默认拖动切换左右两个子View时的点击个数
private boolean splitIsShowSnapshotView = false ; // 拖动过程中是否显示快照图片
private long mChildLongPressTime ; // 记录用户长按拖动子View的初始时间
private View switchLongPressChildView = null ; // 记录用户长按拖动的子View
private boolean hasSwitchChild = false ; // 子View是否通过手势切换完成
private long LONG_PRESS_TIME = 20 00 ;
private long LONG_PRESS_TIME = 1 200;
// private Vibrator vibrator;
public SplitLayout ( Context context ) {
@@ -112,6 +113,7 @@ public class SplitLayout extends ViewGroup {
dragForgroundColor = a . getColor ( R . styleable . SplitLayout_splitDragForgroundColor , dragForgroundColor ) ;
// 切换左右分割子View时手指的点击个数
splitDragTouchCount = a . getInteger ( R . styleable . SplitLayout_splitDragTouchCount , splitDragTouchCount ) ;
splitIsShowSnapshotView = a . getBoolean ( R . styleable . SplitLayout_splitIsShowSnapshotView , splitIsShowSnapshotView ) ;
a . recycle ( ) ;
mPaint . setColor ( dragForgroundColor ) ;
@@ -126,6 +128,12 @@ public class SplitLayout extends ViewGroup {
checkChildren ( ) ;
int widthSize = MeasureSpec . getSize ( widthMeasureSpec ) ;
int heightSize = MeasureSpec . getSize ( heightMeasureSpec ) ;
// 重置子View的最小值
if ( mOrientation = = VERTICAL ) {
mChildMinSize = heightSize / 3 ;
} else {
mChildMinSize = widthSize / 3 ;
}
if ( widthSize > 0 & & heightSize > 0 ) {
mWidth = widthSize ;
mHeight = heightSize ;
@@ -146,8 +154,12 @@ public class SplitLayout extends ViewGroup {
}
// 初始化双缓冲对应的Bitmap和canvas
cachedBitmap = Bitmap . createBitmap ( mWidth , mHeight , Bitmap . Config . ARGB_8888 ) ;
cachedCanvas = new Canvas ( cachedBitmap ) ;
if ( cachedBitmap = = null ) {
cachedBitmap = Bitmap . createBitmap ( mWidth , mHeight , Bitmap . Config . ARGB_8888 ) ;
}
if ( cachedCanvas = = null ) {
cachedCanvas = new Canvas ( cachedBitmap ) ;
}
} else {
throw new IllegalStateException ( " SplitLayout with or height must not be MeasureSpec.UNSPECIFIED " ) ;
}
@@ -198,7 +210,7 @@ public class SplitLayout extends ViewGroup {
}
mHandleDrawable . setState ( PRESSED_STATE_SET ) ;
mIsDragging = true ;
// getParent(). requestDisallowInterceptTouchEvent(true) ;
getParent( ) . requestDisallowInterceptTouchEvent( true ) ;
getBitmapFromChildView ( ) ; // 截屏当前两个子View的图片, 用于在拖动过程中的
invalidate ( ) ;
} else {
@@ -224,7 +236,7 @@ public class SplitLayout extends ViewGroup {
Log . d ( TAG , " ACTION_MOVE " ) ;
if ( ev . getPointerCount ( ) = = 1 ) {
if ( mIsDragging ) {
// getParent(). requestDisallowInterceptTouchEvent(true) ;
getParent( ) . requestDisallowInterceptTouchEvent( true ) ;
if ( mOrientation = = VERTICAL ) {
float deltaY = y - mLastMotionY ;
onlyUpdateSplitPosition ( deltaY ) ;
@@ -246,11 +258,12 @@ public class SplitLayout extends ViewGroup {
// 判断所有手指是否都到了另外一个子View内
View tmpView = ( switchLongPressChildView = = mChild0 ) ? mChild1 : mChild0 ;
if ( isEventAllInChildView ( tmpView , ev ) ) {
// 震动提示用户,已实现换位
// 多个触控位置全部移动到另外的View中, 并且此前状态不是已切换状态, 开始切换快照图片
if ( ! hasSwitchChild ) {
// 震动提示用户
performHapticFeedback ( HapticFeedbackConstants . VIRTUAL_KEY ) ;
hasSwitchChild = true ;
Toast. makeText( getContext( ) , " 实现切换 " , Toast . LENGTH_SHORT ) . show ( ) ;
// Toast. makeText( getContext(), "实现切换", Toast.LENGTH_SHORT).show() ;
postInvalidate ( ) ;
}
} else {
@@ -258,7 +271,7 @@ public class SplitLayout extends ViewGroup {
if ( hasSwitchChild ) {
performHapticFeedback ( HapticFeedbackConstants . VIRTUAL_KEY ) ;
hasSwitchChild = false ;
Toast. makeText( getContext( ) , " 恢复状态 " , Toast . LENGTH_SHORT ) . show ( ) ;
// Toast. makeText( getContext(), "恢复状态", Toast.LENGTH_SHORT).show() ;
postInvalidate ( ) ;
}
}
@@ -270,6 +283,7 @@ public class SplitLayout extends ViewGroup {
performHapticFeedback ( HapticFeedbackConstants . DRAG_START ) ;
mIsSwitching = true ;
mIsSwitchStart = false ;
postInvalidate ( ) ; // 开始进入切换左右位置的状态
} else { // 划出当前View, 则重置长按初始时间为0, 且初始点按的childView也置为null
mChildLongPressTime = 0 ;
switchLongPressChildView = null ;
@@ -279,9 +293,7 @@ public class SplitLayout extends ViewGroup {
}
break ;
case MotionEvent . ACTION_POINTER_UP :
if ( hasSwitchChild ) {
switchChildViewPosition ( ) ;
}
switchChildViewPosition ( hasSwitchChild ) ;
hasSwitchChild = false ;
mIsSwitching = false ;
mIsSwitchStart = false ;
@@ -302,6 +314,7 @@ public class SplitLayout extends ViewGroup {
mLastMotionX = x ;
mLastMotionY = y ;
mIsDragging = false ;
postInvalidate ( ) ;
}
break ;
}
@@ -330,7 +343,7 @@ public class SplitLayout extends ViewGroup {
if ( mOrientation = = VERTICAL ) {
return y > = ( mSplitPosition - ( mHandleSize * 2 ) ) & & y < = ( mSplitPosition + ( mHandleSize * 2 ) ) ;
} else {
return x > = ( mSplitPosition - ( mHandleSize * 2 ) ) & & x < = ( mSplitPosition + ( mHandleSize * 2 ) ) ;
return x > = ( mSplitPosition - ( mHandleSize * 20 ) ) & & x < = ( mSplitPosition + ( mHandleSize * 20 ) ) ;
}
}
@@ -373,12 +386,16 @@ public class SplitLayout extends ViewGroup {
child0Rect . set ( 0 , 0 , ( int ) ( mSplitPosition - mHandleSize / 2 ) , getHeight ( ) ) ;
child1Rect . set ( ( int ) ( mSplitPosition + mHandleSize / 2 ) , 0 , getWidth ( ) , getHeight ( ) ) ;
}
cachedCanvas . drawBitmap ( cachedBitmapArray [ 0 ] , null , child0Rect , null ) ;
if ( splitIsShowSnapshotView ) {
cachedCanvas . drawBitmap ( cachedBitmapArray [ 0 ] , null , child0Rect , null ) ;
}
cachedCanvas . drawRect ( child0Rect , mPaint ) ;
// cachedCanvas.drawBitmap(((BitmapDrawable)getContext().getDrawable(R.drawable.icon_app)).getBitmap(),
// child0Rect.centerX() - (getContext().getDrawable(R.drawable.icon_app).getBounds().width()/2),
// child0Rect.centerY() - (getContext().getDrawable(R.drawable.icon_app).getBounds().height()/2), null);
cachedCanvas . drawBitmap ( cachedBitmapArray [ 1 ] , null , child1Rect , null ) ;
if ( splitIsShowSnapshotView ) {
cachedCanvas . drawBitmap ( cachedBitmapArray [ 1 ] , null , child1Rect , null ) ;
}
cachedCanvas . drawRect ( child1Rect , mPaint ) ;
// cachedCanvas.drawBitmap(((BitmapDrawable)getContext().getDrawable(R.drawable.icon_app)).getBitmap(),
// child1Rect.centerX() - (getContext().getDrawable(R.drawable.icon_app).getBounds().width()/2),
@@ -502,14 +519,19 @@ public class SplitLayout extends ViewGroup {
}
// 切换两个子View的位置
public void switchChildViewPosition ( ) {
public void switchChildViewPosition ( boolean isReverse ) {
checkChildren ( ) ;
removeAllViews ( ) ;
addView ( mChild1 ) ;
addView ( mChild0 ) ;
View tempView = mChild0 ;
mChild0 = mChild1 ;
mChild1 = tempView ;
if ( isReverse ) {
addView ( mChild1 ) ;
addView ( mChild0 ) ;
View tempView = mChild0 ;
mChild0 = mChild1 ;
mChild1 = tempView ;
} else {
addView ( mChild0 ) ;
addView ( mChild1 ) ;
}
requestLayout ( ) ;
}
@@ -536,11 +558,22 @@ public class SplitLayout extends ViewGroup {
child0Rect . set ( ( int ) ( mSplitPosition + mHandleSize / 2 ) , 0 , getWidth ( ) , getHeight ( ) ) ;
}
}
cachedCanvas . drawBitmap ( cachedBitmapArray [ 0 ] , null , child0Rect , null ) ;
cachedCanvas . drawBitmap ( cachedBitmapArray [ 0 ] , new Rect ( 0 , 0 , cachedBitmapArray [ 0 ] . getWidth ( ) , cachedBitmapArray [ 0 ] . getHeight ( ) ) , child0Rect , mPaint ) ;
cachedCanvas . drawRect ( child0Rect , mPaint ) ;
cachedCanvas . drawBitmap ( cachedBitmapArray [ 1 ] , null , child1Rect , null ) ;
cachedCanvas . drawBitmap ( cachedBitmapArray [ 1 ] , new Rect ( 0 , 0 , cachedBitmapArray [ 1 ] . getWidth ( ) , cachedBitmapArray [ 1 ] . getHeight ( ) ) , child1Rect , mPaint ) ;
cachedCanvas . drawRect ( child1Rect , mPaint ) ;
canvas . drawBitmap ( cachedBitmap , 0 , 0 , null ) ;
canvas . drawBitmap ( cachedBitmap , 0 , 0 , mPaint ) ;
}
}
/**
* 设置子View的最小宽度
* */
public int getmChildMinSize ( ) {
return mChildMinSize ;
}
public void setmChildMinSize ( int mChildMinSize ) {
this . mChildMinSize = mChildMinSize ;
}
}