1

I have implemented an Overlay view... currently the view is detachable by edge swiping from left to right (like paging). I would like to prevent this. How can I do that?

I have tried the method recommended in How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically? to disable paging by swiping with finger in ViewPager. However, it didn't help my case.

This is how I inflate my view,

private void initView() {
        if (null == mInflater) {
            mInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        if (null == mscreenView) {
            mscreenView = mInflater.inflate(R.layout.view_screen, null);
        }
    }

My view_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/screen_background_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent">


        <RelativeLayout
            android:id="@+id/screen_background_status_dummy"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_alignParentTop="true"
            android:background="@android:color/transparent" />

        <RelativeLayout
            android:id="@+id/screen_background_in_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/screen_background_status_dummy"
            android:background="@android:color/transparent">
            <ImageView
                android:id="@+id/screen_background_image"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_centerInParent="true"
                android:background="@drawable/"
                android:contentDescription="@string/background_image" />
        </RelativeLayout>

    </RelativeLayout>

    <com.xyz.screenusingservice.ScreenView
        android:id="@+id/screen_forground_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">

        <RelativeLayout
            android:id="@+id/screen_forground_status_dummy"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@android:color/holo_orange_light" />

        <ImageView
            android:id="@+id/screen_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:background="@drawable/black_with_logo" />
    </com.xyz.screenusingservice.ScreenView>

</RelativeLayout>

The ScreenView.java

public class ScreenView extends RelativeLayout {
    private Context mActivityContext = null;
    private final String TAG = "screenView";
    private static final String SCREEN_WINDOW_HIDDEN = "com.route1.screenusingservice.WINDOW_HIDDEN";

    public static boolean isWindowDetached = true;

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, @Nullable Rect previouslyFocusedRect) {
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
        Log.d(TAG, "Full screen view onFocusChanged");
    }

    @Override
    protected void onDetachedFromWindow () {
        super.onDetachedFromWindow();
        isWindowDetached = true;
        screen.setServiceStopped(true);
        Log.e(TAG, "Fullscreen view onDetachedFromWindow");
        screen screen = screen.getInstance(mActivityContext);
        if (null != screen) { //TODO find a better solution to stop the service
                screen.stopscreenService();
        }
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return false;
    }

    @Override
    public boolean onInterceptHoverEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        //requestDisallowInterceptTouchEvent(false);
        return false;
    }



    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        Log.d(TAG, "onScrollChanged");
    }



    @Override
    public void onFinishInflate() {
        super.onFinishInflate();
        isWindowDetached = false;
        ImageView imageView = (ImageView ) findViewById(R.id.screen_image );
        if(null != imageView) {
            imageView.invalidate();
            Bitmap bitmap = screen.getBitmap();
            if (bitmap != null) {
                synchronized (this) {
                    bitmap = makeTransparent(bitmap, bitmap.getWidth(), bitmap.getHeight());
                    if (null != bitmap) {
                        //imageView.setImageBitmap(bitmap);
                        //imageView.background
                        BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
                        imageView.setBackground(ob);
                    }
                }
            }
        }
    }

    // Convert transparentColor to be transparent in a Bitmap.
    public Bitmap makeTransparent(Bitmap bitmap, int width, int height) {

        int [] allpixels = new int [ bitmap.getHeight()*bitmap.getWidth()];
        bitmap.getPixels(allpixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),bitmap.getHeight());
        List<Rect> rectList = screen.getTransparentCoordinates();
        if(rectList != null) {
            for (Rect rect : rectList) {
                rect = getValidRect(new Rect(0, 0, width, height), rect);
                if (rect != null) {
                    for (int i = rect.left; i < rect.right; i++) {
                        for (int j = rect.top; j < rect.bottom; j++) {
                            allpixels[i + j * width] = Color.alpha(Color.TRANSPARENT);
                        }
                    }
                }
            }
        }
        bitmap.setPixels(allpixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
        return bitmap;
    }




    public Rect getValidRect(Rect destRect, Rect srcRect){
        //return (rect.left >= 0 && rect.right >= 0 && rect.width() <=viewPortWidth && rect.height() <= viewPortHeight)
        if(!destRect.contains(srcRect)) {
            boolean isInterSected = srcRect.setIntersect(srcRect, destRect);
            if(!isInterSected){
                return new Rect(0, 0, 0, 0);
            }
        }
        return srcRect;
    }


    public ScreenView(Context context) {
        super(context);
        mActivityContext = context;
        initView();
    }

    public ScreenView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mActivityContext = context;
        initView();
    }

    public ScreenView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mActivityContext = context;
        initView();
    }

    @TargetApi(21)
    public ScreenView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mActivityContext = context;
        initView();
    }

    private void initView() {
    }

}

My activity onCreate method,

protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        sscreenActivityContext = this;
        mMainHandler = new SendMassgeHandler();
        getWindow().setType(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        } else {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }        
    }
ismail alaoui
  • 5,748
  • 2
  • 21
  • 38

0 Answers0