Android realizes global floating pop-up, and beginners can read it

 public WindowManager.LayoutParams wmParams;



public WindowManager getWm() {

    return wm;

}



public void setWm(WindowManager wm) {

    this.wm = wm;

}



public WindowManager.LayoutParams getWmParams() {

    return wmParams;

}



public void setWmParams(WindowManager.LayoutParams wmParams) {

    this.wmParams = wmParams;

    this.wmParams.x = 0;//screenWidth/2; //  The window is attached to the right first

}



public SmallWindowView(Context context) {

    this(context, null);

}



public SmallWindowView(Context context, AttributeSet attrs) {

    this(context, attrs, 0);

}



public SmallWindowView(final Context context, AttributeSet attrs, int defStyle) {

    super(context, attrs, defStyle);

    statusHeight = getStatusHeight(context);

    DisplayMetrics dm = getResources().getDisplayMetrics();

    screenHeight = dm.heightPixels;

    screenWidth = dm.widthPixels;

    addOnAttachStateChangeListener(new OnAttachStateChangeListener() {

        @Override

        public void onViewAttachedToWindow(View v) {

            //In response to the button inside the window, click jump to open a new page

            findViewById(R.id.btn).setOnClickListener(new OnClickListener() {

                @Override

                public void onClick(View v) {

                    Intent intent = new Intent(context, SecondActivity.class);

                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    context.startActivity(intent);

                }

            });

            removeOnAttachStateChangeListener(this);

        }



        @Override

        public void onViewDetachedFromWindow(View v) {

        }

    });

}



/**

 * Gets the height of the status bar

 * @param context

 * @return

 */

public static int getStatusHeight(Context context) {

    int statusHeight = -1;

    try {

        Class clazz = Class.forName("com.android.internal.R$dimen");

        Object object = clazz.newInstance();

        int height = Integer.parseInt(clazz.getField("status_bar_height")

                .get(object).toString());

        statusHeight = context.getResources().getDimensionPixelSize(height);

    } catch (Exception e) {

        e.printStackTrace();

    }

    return statusHeight;

}



@Override

public boolean onTouchEvent(MotionEvent event) {

    switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:

            // Relative x coordinate and relative Y coordinate of touch point in View

            //mTouchStartX = event.getX();

            //mTouchStartY = event.getY();



            // x-coordinate and y-coordinate of the touch point relative to the screen

            mLastRawX = event.getRawX();

            mLastRawY = event.getRawY() - statusHeight;

            Log.e(TAG, "startX = " + mLastRawX + " startY = " + mLastRawY);

            break;

        case MotionEvent.ACTION_MOVE:

            updateViewPosition(event.getRawX(), event.getRawY());

            mLastRawX = event.getRawX();

            mLastRawY = event.getRawY();

            break;

        case MotionEvent.ACTION_UP:

            break;

        default:

            break;

    }

    return true;

}



/** Update floating window position parameters */

private void updateViewPosition(float x, float y) {

    wmParams.gravity = Gravity.NO_GRAVITY;

    //Calculate movement distance

    int dx = (int) (x - mLastRawX);

    int dy = (int) (y - mLastRawY);

    Log.e(TAG, "updateViewPosition: dx = " + dx + " dy = " + dy);

    //The default is to start with the screen center point as (0,0)

    wmParams.x += dx;

    wmParams.y += dy;

    Log.e(TAG, "updateViewPosition: wmParams.x = " + wmParams.x + " wmParams.y = " + wmParams.y);

    wm.updateViewLayout(this, wmParams);

}

}



Define a base class Activity Use and process permission applications:



public class BaseActivity extends AppCompatActivity {

private WindowManager wm;

private SmallWindowView windowView;

private WindowManager.LayoutParams mLayoutParams;

private int OVERLAY_PERMISSION_REQ_CODE = 2;

private boolean isRange = false;



@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    wm = ((App)getApplication()).getWindowManager();

    windowView = ((App)getApplication()).getWindowView();

    mLayoutParams = ((App)getApplication()).getLayoutParams();

}



public void alertWindow() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // Above 7.0, it is necessary to use boot to set the floating permission to open the window

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // type above 8.0 needs to be set to this

            mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;

        }

        requestDrawOverLays();

    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // 6.0 dynamic application

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW}, 1);

    }

}



@Override

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        if (wm != null && windowView.getWm() == null) {

            wm.addView(windowView, mLayoutParams);

        }

    } else {

        Toast.makeText(this, "Permission application failed", Toast.LENGTH_SHORT).show();

    }

}



private int[] location = new int[2]; // Small window position coordinates



@Override

public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {

        isRange = calcPointRange(event);

    }

    if (isRange) {

        windowView.dispatchTouchEvent(event);

    }

    return super.onTouchEvent(event);

}



/**

 *  Calculate whether the coordinates of the current click event are in the small window

 * @param event

 * @return

 */

private boolean calcPointRange(MotionEvent event) {

    windowView.getLocationOnScreen(location);

    int width = windowView.getMeasuredWidth();

    int height = windowView.getMeasuredHeight();

    float curX = event.getRawX();

    float curY = event.getRawY();

    if (curX >= location[0] && curX <= location[0] + width && curY >= location[1] && curY <= location[1] + height) {

        return true;

    }

    return false;

last

In fact, there are so many knowledge points about Android development. There are still so many things to ask during the interview. So there is no other trick in the interview. It only depends on how well you are prepared for these knowledge points. So, when you go out for an interview, just look at the stage you have reviewed.

For the high-frequency interview questions of Baidu, Tencent, Netease, byte beat, Ali and other companies in 2021 shared above, the bloggers also sorted these technical points into videos and PDF s (actually spent a lot more energy than expected), including knowledge context + many details. Due to the limited space, the above is only shown to you in the form of pictures.

CodeChina open source project: Android learning notes summary + mobile architecture Video + big factory interview real questions + project actual combat source code

[Android mind map (skill tree)]

Knowledge system? Here is also the thought brain map of Android advanced learning, for your reference.

[Android advanced architecture video learning resources]

Just fine.

For the high-frequency interview questions of Baidu, Tencent, Netease, byte beat, Ali and other companies in 2021 shared above, the bloggers also sorted these technical points into videos and PDF s (actually spent a lot more energy than expected), including knowledge context + many details. Due to the limited space, the above is only shown to you in the form of pictures.

CodeChina open source project: Android learning notes summary + mobile architecture Video + big factory interview real questions + project actual combat source code

[Android mind map (skill tree)]

Knowledge system? Here is also the thought brain map of Android advanced learning, for your reference.

[external chain picture transferring... (img-qJaRb6pd-1630386006517)]

[Android advanced architecture video learning resources]

**Android part of the fine talk video is even more powerful after learning** Enter BATJ factory, etc. (prepare for war)! Now it is said that the Internet winter is nothing more than that you get on the wrong car and wear less (skills). If you get on the right car, your technical ability is strong enough and the cost of changing the company is high, how can you be laid off? It's just to eliminate the end business Curd! Now there are a flood of junior programmers in the market. This set of tutorials is aimed at Android development engineers who have been working for 1-6 years and are in a bottleneck period. If you want to break through your salary increase in the next year, you can be an advanced Android middle and senior architect It's like a duck to water for you. Get it quickly!

Keywords: Android Design Pattern

Added by JJ123 on Sat, 18 Dec 2021 01:24:25 +0200