以前调试浮动推拽时,点击跳转那块的UI 无法触发推拽,原因是给那块设置了点击事件。需要给整个Ui设置点击事件才不会抢到推拽的焦点。记录代码如下:
package com.base.chat.test.debug;
import android.content.res.Configuration;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.TouchUtils;
import com.base.chat.R;
import com.base.chat.test.TestActivity;
public class DebugIcon extends RelativeLayout {
private static final DebugIcon INSTANCE = new DebugIcon();
private int mIconId;
public static DebugIcon getInstance() {
return INSTANCE;
}
public static void setVisibility(boolean isShow) {
if (INSTANCE == null) {
return;
}
INSTANCE.setVisibility(isShow ? VISIBLE : GONE);
}
public DebugIcon() {
super(DebugUtils.getApp());
inflate(getContext(), R.layout.tmd_debug_icon, this);
ShadowHelper.applyDebugIcon(this);
TouchUtils.setOnTouchListener(this, new TouchUtils.OnTouchUtilsListener() {
private int rootViewWidth;
private int rootViewHeight;
private int viewWidth;
private int viewHeight;
private int statusBarHeight;
@Override
public boolean onDown(View view, int x, int y, MotionEvent event) {
viewWidth = view.getWidth();
viewHeight = view.getHeight();
View contentView = view.getRootView().findViewById(android.R.id.content);
rootViewWidth = contentView.getWidth();
rootViewHeight = contentView.getHeight();
statusBarHeight = BarUtils.getStatusBarHeight();
processScale(view, true);
return true;
}
@Override
public boolean onMove(View view, int direction, int x, int y, int dx, int dy, int totalX, int totalY, MotionEvent event) {
view.setX(Math.min(Math.max(0, view.getX() + dx), rootViewWidth - viewWidth));
view.setY(Math.min(Math.max(statusBarHeight, view.getY() + dy), rootViewHeight - viewHeight));
return true;
}
@Override
public boolean onStop(View view, int direction, int x, int y, int totalX, int totalY, int vx, int vy, MotionEvent event) {
stick2HorizontalSide(view);
processScale(view, false);
return true;
}
private void stick2HorizontalSide(View view) {
view.animate()
.setInterpolator(new DecelerateInterpolator())
.translationX(view.getX() + viewWidth / 2f > rootViewWidth / 2f ? rootViewWidth - viewWidth : 0)
.setDuration(100)
.withEndAction(new Runnable() {
@Override
public void run() {
savePosition();
}
})
.start();
}
private void processScale(final View view, boolean isDown) {
float value = isDown ? 1 - 0.1f : 1;
view.animate()
.scaleX(value)
.scaleY(value)
.setDuration(100)
.start();
}
});
//整个布局设置点击事件-- 而不是 在 debugIconIv 去设置点击事件,不然 事件 冲突 影响滑动
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TestActivity.start(v.getContext());
}
});
//关闭按钮
findViewById(R.id.cancelIconIv).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
INSTANCE.setVisibility(GONE);
}
});
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
wrapPosition();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
savePosition();
}
private void savePosition() {
if (getX() == 0 && getY() == 0) {
return;
}
DebugConfig.saveViewX(this, (int) getX());
DebugConfig.saveViewY(this, (int) getY());
}
@Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
wrapPosition();
}
private void wrapPosition() {
post(new Runnable() {
@Override
public void run() {
View contentView = getRootView().findViewById(android.R.id.content);
if (contentView == null) {
return;
}
setX(DebugConfig.getViewX(DebugIcon.this));
setY(DebugConfig.getViewY(DebugIcon.this, contentView.getHeight() / 3));
setX(getX() + getWidth() / 2f > contentView.getWidth() / 2f ? contentView.getWidth() - getWidth() : 0);
}
});
}
public void setIconId(final int iconId) {
ImageView debugPanelIconIv = findViewById(R.id.debugIconIv);
debugPanelIconIv.setImageResource(mIconId);
}
public int getIconId() {
return mIconId;
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<!--parent type is RelativeLayout-->
<ImageView
android:id="@+id/debugIconIv"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@mipmap/ic_test_round" />
<ImageView
android:id="@+id/cancelIconIv"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="45dp"
android:layout_marginBottom="50dp"
android:layout_gravity="top|end"
android:src="@drawable/reply_cancel" />
</merge>
public class DragConfig {
private static final String DEBUG_ICON_X = "DEBUG_ICON_X";
private static final String DEBUG_ICON_Y = "DEBUG_ICON_Y";
private static final String NO_MORE_REMINDER = "NO_MORE_REMINDER";
public static void saveDebugIconX(float x) {
getSp().put(DEBUG_ICON_X, x);
}
public static float getDebugIconX() {
return getSp().getFloat(DEBUG_ICON_X);
}
public static void saveDebugIconY(float y) {
getSp().put(DEBUG_ICON_Y, y);
}
public static float getDebugIconY() {
return getSp().getFloat(DEBUG_ICON_Y, ScreenUtils.getAppScreenHeight() / 3);
}
public static void saveNoMoreReminder() {
getSp().put(NO_MORE_REMINDER, true);
}
public static boolean isNoMoreReminder() {
return getSp().getBoolean(NO_MORE_REMINDER, false);
}
public static void saveViewY(View view, int y) {
if (ScreenUtils.isPortrait()) {
getSp().put(view.getClass().getSimpleName() + ".yP", y);
} else {
getSp().put(view.getClass().getSimpleName() + ".yL", y);
}
}
public static int getViewY(View view) {
return getViewY(view, 0);
}
public static int getViewY(View view, int defaultVal) {
if (ScreenUtils.isPortrait()) {
return getSp().getInt(view.getClass().getSimpleName() + ".yP", defaultVal);
} else {
return getSp().getInt(view.getClass().getSimpleName() + ".yL", defaultVal);
}
}
public static void saveViewX(View view, int x) {
if (ScreenUtils.isPortrait()) {
getSp().put(view.getClass().getSimpleName() + ".xP", x);
} else {
getSp().put(view.getClass().getSimpleName() + ".xL", x);
}
}
public static int getViewX(View view) {
if (ScreenUtils.isPortrait()) {
return getSp().getInt(view.getClass().getSimpleName() + ".xP");
} else {
return getSp().getInt(view.getClass().getSimpleName() + ".xL");
}
}
public static void saveViewHeight(View view, int height) {
if (ScreenUtils.isPortrait()) {
getSp().put(view.getClass().getSimpleName() + ".heightP", height);
} else {
getSp().put(view.getClass().getSimpleName() + ".heightL", height);
}
}
public static int getViewHeight(View view, int height) {
if (ScreenUtils.isPortrait()) {
return getSp().getInt(view.getClass().getSimpleName() + ".heightP", height);
} else {
return getSp().getInt(view.getClass().getSimpleName() + ".heightL", height);
}
}
public static void saveViewAlpha(View view, float alpha) {
getSp().put(view.getClass().getSimpleName() + ".alpha", alpha);
}
public static float getViewAlpha(View view) {
return getSp().getFloat(view.getClass().getSimpleName() + ".alpha", 1f);
}
private static SPUtils getSp() {
return SPUtils.getInstance("DebugUtils");
}
}