将触摸事件传递到当前视图下方的屏幕-Android

时间:2018-10-09 18:15:36

标签: android android-touch-event

我有一个从View扩展的课程

public class SideLightView extends View {
...
...
...
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    width = getWidth();
    height = getHeight();

    if (GlobalValues.DEBUG_MODE) Log.e(TAG, "Screen Width : " + width + "  
    Height : " + height);

    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(strokeWidth);
//        paint.setColor(color);

    path.moveTo(0, 0);
    path.lineTo(0, height);
    path.lineTo(width, height);
    path.lineTo(width, 0);
    path.lineTo(0, 0);

    paint.setShader(new LinearGradient(0, 0, 0, height, top_color, 
    bottom_color, Shader.TileMode.CLAMP));
    canvas.drawPath(path, paint);

}

@Override
public boolean onTouchEvent(MotionEvent event) {
//        super.onTouchEvent(event);
    return false;
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
//        super.dispatchTouchEvent(event);
    return false;
}
}

我正在使用如下所示的Window Manager在服务类中添加此视图:

mWindowManager = (WindowManager) mContext.getSystemService(WINDOW_SERVICE);


    int LAYOUT_FLAG;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;  
// for solving error window type 2006
    } else {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;  // for solving 
error window type 2038
    }

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT,
            LAYOUT_FLAG,
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
//                        | WindowManager.LayoutParams.FLAG_FULLSCREEN
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                    | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSPARENT);


    params.gravity = Gravity.LEFT;


    sideLightView = new SideLightView(mContext);
    sideLightView.setClickable(false);
    sideLightView.setFocusable(false);

    mWindowManager.addView(sideLightView, params);
    Animation fade_in_out_animation = AnimationUtils.loadAnimation(mContext, 
R.anim.fade_in_and_out);
    fade_in_out_animation.setDuration(800);

    sideLightView.setStrokeWidth(40);

    sideLightView.startAnimation(fade_in_out_animation);

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            removeView();
        }
    }, utility.getSideLigthDuration() * 1000); //adding 3 sec delay

我的目的是在锁定屏幕上显示此视图,以供用户在其他应用程序上显示所需的秒数,但由于它在屏幕周围只有一个笔触大小的矩形框,而在中心部分完全没有其他内容,因此保持空白或透明,可以正确显示,但也会消耗我的触摸事件。

每当该视图显示在屏幕上时,它都会消耗触摸事件,用户会感觉手机挂了几秒钟。

我想为此视图禁用触摸监听,最重要的是,将所有触摸事件传递给下面的屏幕,就像用户在此视图出现时通常使用的屏幕一样

我尝试了什么:

  • 我重写了视图的 onTouchEvent dispatchTouchEvent 方法,并为其返回了false-不变。
  • 对于所有覆盖的方法均返回true-不变
  • 我将Clickable和Focusable设置为false-未更改
  • 我尝试删除并添加了以下标志:FLAG_NOT_TOUCH_MODAL,FLAG_NOT_FOCUSABLE,FLAG_WATCH_OUTSIDE_TOUCH-无更改

如果有人能弄清楚这种情况。 预先感谢。

2 个答案:

答案 0 :(得分:1)

您可以尝试先为父级布局设置void convert_to_camel(char* phrase){ int j =0; for(int i=0;i<full_len-1;i++){ if(isalphanum(phrase[i])){ phrase[j] = phrase[i]; j++; length++; } } int flag = 0; char new[50]; for (int i=0;i<length;i++){ if(phrase[i]== '95'){ flag = 1; } if(flag ==1){ new[i] = toUpper(phrase[i+1]); i++; new[i] = phrase[i+1]; flag = 0; } else{ new[i] = phrase[i]; } } focusable= "false"

现在,在子视图上实现OnTouchListener,并且在接收到Touch Event时从子视图返回true。

enabled="false

答案 1 :(得分:1)

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
            PixelFormat.TRANSPARENT);

添加WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE标志应该可以解决您的问题。