In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to achieve a mobile suspension window in Android. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
First of all, let's build a windowmangerdemo project.
Add a button button to activity_main
Then add two layout files, window_small.xml and window_big.xml, to the layout Layout, which are used to click on the small window and expand the large window.
Window_small.xml
We directly add a relative layout management clamor to the linear layout management. Then set up the background picture.
FloatWindowSmallView
Our floatWindowSmallView inherits from LinearLayout
Rewrite the onTouchEvent event, which we use to click to move and expand our suspension box at any time
Public boolean onTouchEvent (MotionEvent event) {
Switch (event.getAction ()) {
Case MotionEvent.ACTION_DOWN:
/ / record the necessary data when the finger is pressed, even if the coordinate value needs to be subtracted from the height of the status bar.
XInView = event.getX ()
YInView = event.getY ()
XDownInScreen = event.getRawX ()
YDownInScreen = event.getRawY ()-getStatusBarHeight ()
XInScreen = event.getRawX ()
YInScreen = event.getRawY ()-getStatusBarHeight ()
Break
Case MotionEvent.ACTION_MOVE:
XInScreen = event.getRawX ()
YInScreen = event.getRawY ()-getStatusBarHeight ()
/ / update the position of the small suspension window as the finger moves
UpdateViewPosition ()
Break
Case MotionEvent.ACTION_UP:
/ / if xDownInScreen and xInScreen are equal and yDownInScreen and yInScreen are equal when the finger leaves the screen, the click event is considered to have been triggered.
If (xDownInScreen = = xInScreen & & yDownInScreen = = yInScreen) {
OpenBigWindow ()
}
Break
Default:
Break
}
Return true
}
Window_big.xml
We set up three vertical ImageButton to represent the large form that is expanded after clicking on the small form.
FloatWindowBigView
The code is relatively simple, just write the corresponding event directly.
Public class FloatWindowBigView extends LinearLayout {
Public static int viewWidth
Public static int viewHeight
Public FloatWindowBigView (final Context context) {
Super (context)
LayoutInflater.from (context) .propagate (R.layout.window_big, this)
View view = findViewById (R.id.big_window_layout)
/ / viewWidth = view.getLayoutParams () .width
/ / viewHeight = view.getLayoutParams () .height
ViewWidth= 120
ViewHeight = 370
ImageButton imgbig = (ImageButton) findViewById (R.id.imgbig)
ImageButton imgcamera = findViewById (R.id.imgcamera)
ImageButton imgvoice = findViewById (R.id.imgvoice)
Imgcamera.setOnClickListener (new OnClickListener () {
@ Override
Public void onClick (View v) {
Toast.makeText (context, "clicked on the camera", Toast.LENGTH_SHORT) .show ()
}
});
Imgvoice.setOnClickListener (new OnClickListener () {
@ Override
Public void onClick (View v) {
Toast.makeText (context, "clicked speech recognition", Toast.LENGTH_SHORT). Show ()
}
});
Imgbig.setOnClickListener (new OnClickListener () {
@ Override
Public void onClick (View v) {
/ / when you click back, remove the large suspension window and create a small suspension window
MyWindowManager.removeBigWindow (context)
MyWindowManager.createSmallWindow (context)
}
});
}
}
MyWindowManager
This class is the event class that we manage the size of the suspension box, including closing the small suspension window, expanding the large suspension window, etc.
We put the parameters of the size and size of the suspension penetration corresponding to different LayoutParams, all of which are processed here.
FloatWindow
Make a floatWindow that inherits from Service
Use Handler and Time controls to refresh whether the suspended window always exists.
/ * *
* determine whether the current interface is a code scanning interface.
, /
Private boolean isForeground (String [] classNames) {
If (classNames.length 0) {
ComponentName cpn = list.get (0). TopActivity
For (String className: classNames) {
If (className.equals (cpn.getClassName () {
Return true
}
}
}
Return false
} class RefreshTask extends TimerTask {
@ Override
Public void run () {
/ / if the current interface is the one to be displayed, and there is no suspended window, a suspended window is created.
If (isForeground (activityname) & &! MyWindowManager.isWindowShowing ()) {
Handler.post (new Runnable () {
@ Override
Public void run () {
MyWindowManager.createSmallWindow (getApplicationContext ())
}
});
}
/ / the interface to be displayed in the current interface, and a suspended window is displayed, then remove the suspended window.
Else if (! isForeground (activityname) & & MyWindowManager.isWindowShowing ()) {
Handler.post (new Runnable () {
@ Override
Public void run () {
MyWindowManager.removeSmallWindow (getApplicationContext ())
MyWindowManager.removeBigWindow (getApplicationContext ())
}
});
}
/ / the current interface is the interface to be displayed, and if a suspended window is displayed, the memory data will be updated.
Else if (isForeground (activityname) & & MyWindowManager.isWindowShowing ()) {
Handler.post (new Runnable () {
@ Override
Public void run () {
MyWindowManager.updateUsedPercent (getApplicationContext ())
}
});
}
}
} the above is how to achieve a mobile suspension window in the Android shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.