Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Activity of suspension window in Android Application

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the Android application suspension window Activity how to achieve, the article introduces in great detail, has a certain reference value, interested friends must read it!

Zoom method

Scaling activity requires the use of WindowManager.LayoutParams to control the width and height of window

Call in activity

Android.view.WindowManager.LayoutParams p = getWindow () .getAttributes (); p.height = 480; / / height p.width = 360; / / width p.dimAmount = 0.0f; / / do not darken the interface below getWindow () .setAttributes (p)

Dim: adj. Dim; dim; faint; not bright; dim; v. (cause) to become dim, weak, or dim; weaken, become indifferent, or lose luster.

If you modify the width and height of the WindowManager.LayoutParams, the window size of the activity will change.

To change back to the default size, call in activity

GetWindow () .setLayout (ViewGroup.LayoutParams.MATCH_PARENT ViewGroup.LayoutParams.MATCH_PARENT)

If you change the position when zooming out, you need to set the position of window to 0.

WindowManager.LayoutParams lp = getWindow () .getAttributes (); lp.x = 0ntWindow () .setAttributes (lp)

Activity becomes smaller, and there may be a black background behind it. This requires the following.

Suspension pattern

Create a new MeTranslucentAct in styles.xml.

@ color/colorPrimary @ color/colorPrimaryDark @ color/colorAccent true # 80000000 true @ android:style/Animation.Translucent

The main style is AppCompat.

Specifies the background of a window. The Activity used by android:windowBackground inherits from androidx.appcompat.app.AppCompatActivity.

After activity zooms out, the background is transparent, and you can see other pages behind.

Click to penetrate the blank

After the activity shrinks, click on the space next to it, and other components can receive the click event.

Add the tags FLAG_LAYOUT_NO_LIMITS and FLAG_NOT_TOUCH_MODAL to the WindowManager.LayoutParams before the setContentView of the onCreate method

WindowManager.LayoutParams layoutParams = getWindow () .getAttributes (); layoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;mBinding = DataBindingUtil.setContentView (this, R.layout.act_float_scale); movable suspension window

Monitor the touch event, calculate the distance of the finger movement, and then move the suspension window.

Private boolean mIsSmall = false; / / whether the current small window private float mLastTx = 0 * MBinding.root.setOnTouchListener ((v, event)-> {switch (event.getAction ()) {case MotionEvent.ACTION_DOWN: Log.d (TAG, "down" + event); mLastTx = event.getRawX (); mLastTy = event.getRawY (); return true Case MotionEvent.ACTION_MOVE: Log.d (TAG, "move" + event); float dx = event.getRawX ()-mLastTx; float dy = event.getRawY ()-mLastTy; mLastTx = event.getRawX (); mLastTy = event.getRawY () Log.d (TAG, "dx:" + dx + ", dy:" + dy); if (mIsSmall) {WindowManager.LayoutParams lp = getWindow () .getAttributes (); lp.x + = dx; lp.y + = dy; getWindow () .setAttributes (lp) } break; case MotionEvent.ACTION_UP: Log.d (TAG, "up" + event); return true; case MotionEvent.ACTION_CANCEL: Log.d (TAG, "cancel" + event); return true;} return false;})

MIsSmall is used to record whether the current activity has become smaller (suspended).

Returns true in the touch listener, indicating that the touch event is consumed.

Event.getX () and event.getY () get the touch coordinates of the current View. Event.getRawX () and event.getRawY () get the touch coordinates of the screen. The position of the touch point on the screen.

The complete code for the example

Databinding enabled

Android {dataBinding {enabled = true}} styles.xml

Create a new style

# 80000000 true @ android:style/Animation.Translucent layout

There are some buttons in the act_float_scale.xml to control the zoom in and out. ConstraintLayout uses it to listen for touch events.

Activity

FloatingScaleAct

Import android.os.Bundle;import android.util.Log;import android.view.Display;import android.view.MotionEvent;import android.view.ViewGroup;import android.view.WindowManager;import androidx.appcompat.app.AppCompatActivity;import androidx.databinding.DataBindingUtil;import com.rustfisher.tutorial2020.R;import com.rustfisher.tutorial2020.databinding.ActFloatScaleBinding;public class FloatingScaleAct extends AppCompatActivity {private static final String TAG = "rfDevFloatingAct"; ActFloatScaleBinding mBinding; private boolean mIsSmall = false; / / current small window private float mLastTx = 0 / / the previous position of the finger private float mLastTy = 0; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); WindowManager.LayoutParams layoutParams = getWindow () .getAttributes (); layoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; mBinding = DataBindingUtil.setContentView (this, R.layout.act_float_scale) MBinding.toSmall.setOnClickListener (v-> toSmall ()); mBinding.toReset.setOnClickListener (v-> {WindowManager.LayoutParams lp = getWindow (). GetAttributes (); lp.x = 0; lp.y = 0; getWindow (). SetAttributes (lp); getWindow (). SetLayout (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); mIsSmall = false }); mBinding.root.setOnTouchListener ((v, event)-> {switch (event.getAction ()) {case MotionEvent.ACTION_DOWN: Log.d (TAG, "down" + event); mLastTx = event.getRawX (); mLastTy = event.getRawY (); return true Case MotionEvent.ACTION_MOVE: Log.d (TAG, "move" + event); float dx = event.getRawX ()-mLastTx; float dy = event.getRawY ()-mLastTy; mLastTx = event.getRawX (); mLastTy = event.getRawY () Log.d (TAG, "dx:" + dx + ", dy:" + dy); if (mIsSmall) {WindowManager.LayoutParams lp = getWindow () .getAttributes (); lp.x + = dx; lp.y + = dy GetWindow () .setAttributes (lp);} break; case MotionEvent.ACTION_UP: Log.d (TAG, "up" + event); return true Case MotionEvent.ACTION_CANCEL: Log.d (TAG, "cancel" + event); return true;} return false;});} private void toSmall () {mIsSmall = true; WindowManager m = getWindowManager (); Display d = m.getDefaultDisplay () WindowManager.LayoutParams p = getWindow (). GetAttributes (); p.height = (int) (d.getHeight () * 0.35); p.width = (int) (d.getWidth () * 0.4); p.dimAmount = 0.0f; getWindow (). SetAttributes (p);}}

Register this activity in manifest

Running effect

Run OK on Hongmi 9A (Android 10charge MIUI 12.5.1 stable version) and Glory (Android 5.1)

The above is all the contents of the article "how to realize the suspension window Activity in Android applications". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report