From 1dc23fdd3f41222c5b811ced1b628e8a5cc67801 Mon Sep 17 00:00:00 2001 From: "xiaoyan159@6800H" Date: Tue, 3 Sep 2024 17:46:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E6=8B=96=E5=8A=A8?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E5=B7=A6=E5=8F=B3=E4=BD=8D=E7=BD=AE=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=88=E7=BB=86=E8=8A=82=E9=9C=80=E4=BC=98?= =?UTF-8?q?=E5=8C=96=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/emulatorDisplays.xml | 47 +++ automotive/src/main/AndroidManifest.xml | 1 + .../library/splitlayout/SplitLayout.java | 273 +++++++++++++----- .../res/drawable-xxhdpi/vertical_line.9.png | Bin 0 -> 668 bytes .../main/res/layout/activity_split_layout.xml | 7 +- .../src/main/res/values/splitlayout.xml | 4 + 6 files changed, 260 insertions(+), 72 deletions(-) create mode 100644 .idea/emulatorDisplays.xml create mode 100644 automotive/src/main/res/drawable-xxhdpi/vertical_line.9.png diff --git a/.idea/emulatorDisplays.xml b/.idea/emulatorDisplays.xml new file mode 100644 index 0000000..2785e11 --- /dev/null +++ b/.idea/emulatorDisplays.xml @@ -0,0 +1,47 @@ + + + + + + \ No newline at end of file diff --git a/automotive/src/main/AndroidManifest.xml b/automotive/src/main/AndroidManifest.xml index b0c2496..0e7dc6a 100644 --- a/automotive/src/main/AndroidManifest.xml +++ b/automotive/src/main/AndroidManifest.xml @@ -6,6 +6,7 @@ android:required="true" /> + 0 && System.currentTimeMillis() - mChildLongPressTime >= LONG_PRESS_TIME ) { + if (isEventAllInChildView(switchLongPressChildView, ev)) { + // 震动提示用户,开始支持拖动移动 + performHapticFeedback(HapticFeedbackConstants.DRAG_START); + mIsSwitching = true; + mIsSwitchStart = false; + } else { // 划出当前View,则重置长按初始时间为0,且初始点按的childView也置为null + mChildLongPressTime = 0; + switchLongPressChildView = null; + } + } + } + } + break; + case MotionEvent.ACTION_POINTER_UP: + if (hasSwitchChild) { + switchChildViewPosition(); + } + hasSwitchChild = false; + mIsSwitching = false; + mIsSwitchStart = false; + switchLongPressChildView = null; + mChildLongPressTime = 0; + break; + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + if (mIsDragging) { + mHandleDrawable.setState(EMPTY_STATE_SET); + if (mOrientation == VERTICAL) { + float deltaY = y - mLastMotionY; + updateSplitPositionWithDelta(deltaY); + } else { + float deltaX = x - mLastMotionX; + updateSplitPositionWithDelta(deltaX); + } + mLastMotionX = x; + mLastMotionY = y; + mIsDragging = false; + } + break; } + return mIsDragging || mIsSwitchStart || mIsSwitching || ev.getActionMasked() == MotionEvent.ACTION_DOWN || ev.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN; + } - break; + public boolean isEventAllInChildView(View childView, MotionEvent event) { + if (childView == null || event == null) { + return false; + } + int left = childView.getLeft(); + int top = childView.getTop(); + int right = childView.getRight(); + int bottom = childView.getBottom(); + + for (int i = 0; i < event.getPointerCount(); i++) { + // 有任意一个点不在当前View内,则返回false + if (event.getX(i) < left || event.getX(i) > right || event.getY(i) < top || event.getY(i) > bottom) { + return false; + } } - return mIsDragging; + return true; } private boolean isUnderSplitHandle(float x, float y) { @@ -268,19 +373,25 @@ 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); + 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); +// 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); 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), - child1Rect.centerY() - (getContext().getDrawable(R.drawable.icon_app).getBounds().height()/2), null); +// cachedCanvas.drawBitmap(((BitmapDrawable)getContext().getDrawable(R.drawable.icon_app)).getBitmap(), +// child1Rect.centerX() - (getContext().getDrawable(R.drawable.icon_app).getBounds().width()/2), +// child1Rect.centerY() - (getContext().getDrawable(R.drawable.icon_app).getBounds().height()/2), null); canvas.drawBitmap(cachedBitmap, 0, 0, null); } + } else if (mIsSwitching) { // 开始拖动切换 + if (hasSwitchChild) { + resetChildSnapshotBitmap(canvas, true); + } else { + resetChildSnapshotBitmap(canvas, false); + } } } @@ -357,12 +468,6 @@ public class SplitLayout extends ViewGroup { // 获取当前两个子View的Bitmap截图,用于在拖动过程中的实时绘制 private void getBitmapFromChildView() { - // 先回收已有的缓存图片 - for (Bitmap bitmap: cachedBitmapArray) { - if (bitmap != null) { - bitmap.recycle(); - } - } if (mChild0!=null) { cachedBitmapArray[0] = loadBitmapFromViewBySystem(mChild0); } @@ -396,6 +501,7 @@ public class SplitLayout extends ViewGroup { return dragForgroundColor; } + // 切换两个子View的位置 public void switchChildViewPosition() { checkChildren(); removeAllViews(); @@ -406,4 +512,35 @@ public class SplitLayout extends ViewGroup { mChild1 = tempView; requestLayout(); } + + // 设置两个子View的快照的显示,可反转 + public void resetChildSnapshotBitmap(Canvas canvas, boolean isReverse) { + if (cachedBitmapArray!= null&&cachedBitmapArray.length>1) { + cachedCanvas.drawColor(getContext().getColor(android.R.color.transparent), PorterDuff.Mode.CLEAR); + Rect child0Rect = new Rect(); + Rect child1Rect = new Rect(); + if (mOrientation == VERTICAL) { + if (!isReverse) { + child0Rect.set(0, 0, getWidth(), (int) (mSplitPosition - mHandleSize / 2)); + child1Rect.set(0, (int) (mSplitPosition + mHandleSize / 2), getWidth(), getHeight()); + } else { + child1Rect.set(0, 0, getWidth(), (int) (mSplitPosition - mHandleSize / 2)); + child0Rect.set(0, (int) (mSplitPosition + mHandleSize / 2), getWidth(), getHeight()); + } + } else { + if (!isReverse) { + child0Rect.set(0, 0, (int) (mSplitPosition - mHandleSize / 2), getHeight()); + child1Rect.set((int) (mSplitPosition + mHandleSize / 2), 0, getWidth(), getHeight()); + } else { + child1Rect.set(0, 0, (int) (mSplitPosition - mHandleSize / 2), getHeight()); + child0Rect.set((int) (mSplitPosition + mHandleSize / 2), 0, getWidth(), getHeight()); + } + } + cachedCanvas.drawBitmap(cachedBitmapArray[0], null, child0Rect, null); + cachedCanvas.drawRect(child0Rect, mPaint); + cachedCanvas.drawBitmap(cachedBitmapArray[1], null, child1Rect, null); + cachedCanvas.drawRect(child1Rect, mPaint); + canvas.drawBitmap(cachedBitmap, 0, 0, null); + } + } } diff --git a/automotive/src/main/res/drawable-xxhdpi/vertical_line.9.png b/automotive/src/main/res/drawable-xxhdpi/vertical_line.9.png new file mode 100644 index 0000000000000000000000000000000000000000..8c96d476b6761418d077b085e849d2dc938a0761 GIT binary patch literal 668 zcmeAS@N?(olHy`uVBq!ia0vp^vOwI#!3HFEEj~Pxfq^O6)5S5QBJS<2jaj!77+fDt zSpFu!tL@Ul?zQG0HgEM_DBu*YmQqwFywgPf>91Wk-hGna!EwaV{n8T0^78yQ*B@`) z8T$PFpZmX3jAr_%O+NYbMvhth_SAELzVO}Tdz`<{FE*{{*?W7Fj{Fzq+9~QK z_rEZwyYl|MSHXS$SL10H{>%Jd)(I|`e-W$fXMbV0+nl-u<-Hbv9o|b7{%X8u`sfSu z-s~eU*{iO1j4GsS@O=NUc=E#4?#EmAOIzjLxv;oyL4O~2y~(%ZcCXogy=Y90+}Cqp zz3CUDZ^>^Q-?Kb-?SE6YyZXE@FcARLLQH@C@3ftQ?X$gCSF%2mTz6q{&YpL`RL9`y L>gTe~DWM4fsc%^M literal 0 HcmV?d00001 diff --git a/automotive/src/main/res/layout/activity_split_layout.xml b/automotive/src/main/res/layout/activity_split_layout.xml index bcca7e5..aa03710 100644 --- a/automotive/src/main/res/layout/activity_split_layout.xml +++ b/automotive/src/main/res/layout/activity_split_layout.xml @@ -41,14 +41,13 @@ android:layout_height="match_parent" android:layout_marginBottom="48dp" app:splitFraction="0.3" - app:splitHandleDrawable="@drawable/split_drawable" - app:splitDragForgroundColor="@color/material_yellow_500" + app:splitHandleDrawable="@drawable/vertical_line" + app:splitDragForgroundColor="@color/draw_progress_bg_color" app:splitOrientation="horizontal"> + android:layout_height="match_parent"> + + + +