feat: 增加左右切换的功能

This commit is contained in:
xiaoyan159@6800H 2024-09-03 09:23:59 +08:00
parent 7fc22a7c6b
commit bad1f99c66
5 changed files with 52 additions and 31 deletions

1
.idea/misc.xml generated
View File

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">

View File

@ -5,6 +5,7 @@
android:name="android.hardware.type.automotive"
android:required="true" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:appCategory="audio"

View File

@ -15,6 +15,8 @@ import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.util.Log;
import android.view.HapticFeedbackConstants;
@ -62,7 +64,8 @@ public class SplitLayout extends ViewGroup {
private Bitmap cachedBitmap;
private Canvas cachedCanvas; // 使用双缓冲方式绘制拖动中的View变化提高效率
private Paint mPaint = new Paint();
private int dragForgroundColor = Color.argb(0.88f, 1f, 1f, 1f);
private int dragForgroundColor = Color.argb(0.88f, 1f, 1f, 1f); // 默认的前景色
private Vibrator vibrator;
public SplitLayout(Context context) {
this(context, null, 0);
@ -103,6 +106,9 @@ public class SplitLayout extends ViewGroup {
a.recycle();
mPaint.setColor(dragForgroundColor);
// 获取震动权限
vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(VibrationEffect.createOneShot(500, 125));
}
@Override
@ -217,9 +223,9 @@ public class SplitLayout extends ViewGroup {
private boolean isUnderSplitHandle(float x, float y) {
if (mOrientation == VERTICAL) {
return y >= (mSplitPosition - mHandleSize / 2) && y <= (mSplitPosition + mHandleSize / 2);
return y >= (mSplitPosition - (mHandleSize*2)) && y <= (mSplitPosition + (mHandleSize*2));
} else {
return x >= (mSplitPosition - mHandleSize / 2) && x <= (mSplitPosition + mHandleSize / 2);
return x >= (mSplitPosition - (mHandleSize*2)) && x <= (mSplitPosition + (mHandleSize*2));
}
}
@ -389,4 +395,15 @@ public class SplitLayout extends ViewGroup {
public int getDragForgroundColor() {
return dragForgroundColor;
}
public void switchChildViewPosition() {
checkChildren();
removeAllViews();
addView(mChild1);
addView(mChild0);
View tempView = mChild0;
mChild0 = mChild1;
mChild1 = tempView;
requestLayout();
}
}

View File

@ -22,29 +22,29 @@ public class SplitLayoutActivity extends Activity {
mVerticalSplitLayout = (SplitLayout) findViewById(R.id.splitlayout_vertical);
mHorizontalSplitLayout = (SplitLayout) findViewById(R.id.splitlayout_horizontal);
findViewById(R.id.tv_child0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(SplitLayoutActivity.this, "CHILD 0", Toast.LENGTH_SHORT).show();
Log.d("SplitLayoutActivity", "tv_child0 clicked");
}
});
// findViewById(R.id.tv_child0).setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Toast.makeText(SplitLayoutActivity.this, "CHILD 0", Toast.LENGTH_SHORT).show();
// Log.d("SplitLayoutActivity", "tv_child0 clicked");
// }
// });
findViewById(R.id.tv_child0).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("SplitLayoutActivity", "tv_child0 onTouch");
return false;
}
});
findViewById(R.id.layout_child0).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("SplitLayoutActivity", "layout_child0 onTouch");
return false;
}
});
// findViewById(R.id.tv_child0).setOnTouchListener(new View.OnTouchListener() {
// @Override
// public boolean onTouch(View v, MotionEvent event) {
// Log.d("SplitLayoutActivity", "tv_child0 onTouch");
// return false;
// }
// });
//
// findViewById(R.id.layout_child0).setOnTouchListener(new View.OnTouchListener() {
// @Override
// public boolean onTouch(View v, MotionEvent event) {
// Log.d("SplitLayoutActivity", "layout_child0 onTouch");
// return false;
// }
// });
}
public void onClickVerticalSample(View v) {
@ -63,11 +63,14 @@ public class SplitLayoutActivity extends Activity {
// Toast.makeText(this, "CHILD 1", Toast.LENGTH_SHORT).show();
// }
public void onClickRotateScreen(View v) {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// } else {
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//
// }
}
// 切换两个子View的显示位置
mHorizontalSplitLayout.switchChildViewPosition();
}
}

View File

@ -40,6 +40,7 @@
android:layout_width="match_parent"
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:splitOrientation="horizontal">