Android Add random-drag desktop hover window _android

Source: Internet
Author: User
With a new version of Android 360, all of the iphone assistants are envious of the 360 only on the desktop showing a tiny hover window?
In fact, there are two main steps to achieve this function:
1. Determine if the current display is for the desktop. This content I have in front of the post has been introduced, if you have not seen the quick steady look oh.
2. Use WindowManager to add a view to the top level
This knowledge point is for the main content of this article Oh. In this article, we will also talk about the following points of knowledge:
A. If you get to the height of the status bar
B. Drag in suspension window
C. Click events in the suspension window
Before we start, let's take a look at the effect chart:

Next, let's look at the code for Floatview:
Copy Code code as follows:

public class Floatview extends imageview{
private float Mtouchx;
private float Mtouchy;
private float X;
private float y;
private float Mstartx;
private float Mstarty;
Private Onclicklistener Mclicklistener;
Private WindowManager WindowManager = (WindowManager) getcontext ()
. Getapplicationcontext (). Getsystemservice (Context.window_service);
This windowmanagerparams variable is the obtained global variable to hold the properties of the suspended window
Private Windowmanager.layoutparams Windowmanagerparams = ((floatapplication) GetContext ()
. Getapplicationcontext ()). Getwindowparams ();
Public Floatview {
Super (context);
}
@Override
public boolean ontouchevent (Motionevent event) {
Get to the height of the status bar
Rect frame = new Rect ();
Getwindowvisibledisplayframe (frame);
int statusbarheight = Frame.top;
System.out.println ("Statusbarheight:" +statusbarheight);
Gets the coordinates of the relative screen, that is, the top left corner of the screen.
x = Event.getrawx ();
y = Event.getrawy ()-statusbarheight; Statusbarheight is the height of the system status bar
LOG.I ("tag", "Currx" + x + "====curry" + y);
Switch (event.getaction ()) {
Case Motionevent.action_down://Capture finger touch Press action
Gets the coordinates of the relative view, that is, the upper-left corner of this view is the original point
Mtouchx = Event.getx ();
Mtouchy = Event.gety ();
Mstartx = x;
Mstarty = y;
LOG.I ("tag", "startx" + Mtouchx + "====starty"
+ Mtouchy);
Break
Case Motionevent.action_move://Capture finger touch Move action
Updateviewposition ();
Break
Case MOTIONEVENT.ACTION_UP://Capture finger touch leave action
Updateviewposition ();
Mtouchx = Mtouchy = 0;
if ((X-MSTARTX) < 5 && (Y-mstarty) < 5) {
if (mclicklistener!=null) {
Mclicklistener.onclick (this);
}
}
Break
}
return true;
}
@Override
public void Setonclicklistener (Onclicklistener l) {
This.mclicklistener = l;
}
private void Updateviewposition () {
Update floating Window Position parameters
windowmanagerparams.x = (int) (X-MTOUCHX);
WINDOWMANAGERPARAMS.Y = (int) (y-mtouchy);
Windowmanager.updateviewlayout (this, windowmanagerparams); Refresh Display
}
}

Code Explanation
int statusbarheight = Frame.top;
To get the height of the status bar, why subtract the height of the status bar when Event.getrawy ()?
Because our suspension window cannot be displayed in the status bar, and then Getrawy to get the distance to the original point of the screen. When our screen is in Full-screen mode, the height of the status bar gets to 0
(X-MSTARTX) < 5 && (Y-mstarty) < 5
If we move a distance of less than 5 during the touch process, it is considered a click, triggering a click callback.
In addition we need to customize a application:
Copy Code code as follows:

public class Floatapplication extends application {
Private Windowmanager.layoutparams windowparams = new Windowmanager.layoutparams ();
Public Windowmanager.layoutparams Getwindowparams () {
return windowparams;
}
}

Code Explanation
The purpose of the custom application is to save the value of the windowparams, because when we drag the suspension window, if each time a new layoutparams is renewed, the update
The time will be found in the anomaly.
Windowparams values do not have to be saved in a custom application, as long as they are global.
Finally, let's look at the implementation of the activity.
Copy Code code as follows:

public class Mainactivity extends activity implements onclicklistener{
Private WindowManager WindowManager = null;
Private Windowmanager.layoutparams windowmanagerparams = null;
Private Floatview Floatview = null;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_no_title)//Cancel title bar
GetWindow (). SetFlags (Windowmanager.layoutparams. Flag_fullscreen,
Windowmanager.layoutparams. Flag_fullscreen);/Full Screen
Setcontentview (R.layout.activity_main);
CreateView ();
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true;
}
public void OnDestroy () {
Super.ondestroy ();
Destroy the suspension window when the program exits (activity is destroyed)
Windowmanager.removeview (Floatview);
}
private void CreateView () {
Floatview = new Floatview (Getapplicationcontext ());
Floatview.setonclicklistener (this);
Floatview.setimageresource (R.drawable.ic_launcher); Here simply use your own icon to do the demo
Get WindowManager
WindowManager = (WindowManager) getapplicationcontext (). Getsystemservice (Context.window_service);
Set Layoutparams (global variable) related parameters
Windowmanagerparams = ((floatapplication) getapplication ()). Getwindowparams ();
Windowmanagerparams.type = Layoutparams.type_phone; Set Window type
Windowmanagerparams.format = pixelformat.rgba_8888; Format the picture, the effect is transparent background
Set Window flag
Windowmanagerparams.flags = Layoutparams.flag_not_touch_modal
| layoutparams.flag_not_focusable;
/*
* Note that the value of the flag can be:
* Layoutparams.flag_not_touch_modal does not affect the following events
* Layoutparams.flag_not_focusable not focused
* Layoutparams.flag_not_touchable not touch
*/
Adjust the suspension window to the upper left corner for easy adjustment of coordinates
windowmanagerparams.gravity = Gravity.left | Gravity.top;
Set the x, y initial values in the upper-left corner of the screen as the origin.
windowmanagerparams.x = 0;
Windowmanagerparams.y = 0;
Set the suspension window long width data
Windowmanagerparams.width = layoutparams.wrap_content;
Windowmanagerparams.height = layoutparams.wrap_content;
Show Myfloatview image
Windowmanager.addview (Floatview, windowmanagerparams);
}
public void OnClick (View v) {
Toast.maketext (This, "clicked", Toast.length_short). Show ();
}
}

Code Explanation
In the activity we mainly add the suspension window and set his position. In addition to note the application of flags:
Layoutparams.flag_not_touch_modal does not affect the events that follow
Layoutparams.flag_not_focusable not focused
Layoutparams.flag_not_touchable not Touch
Finally, we remove the suspension window from the OnDestroy (). So, when we test, remember to press the home key to switch to the desktop.
Finally, remember, in the androidmanifest.xml, we need to use the Android.permission.SYSTEM_ALERT_WINDOW authority
And remember to affirm our custom application Oh.
The Androidmanifest.xml code is as follows
Copy Code code as follows:

<manifest xmlns:android= "Http://schemas.android.com/apk/res/android" "
package=" com.krislq.floating "
android:versioncode=" 1 "
Android:versionname=" 1.0 ">
<USES-SD K
android:minsdkversion= "8"
android:targetsdkversion= "/>
<uses-permission android:name=" Andro Id.permission.SYSTEM_ALERT_WINDOW "/>
<application
android:icon=" @drawable/ic_launcher "
Android: Label= "@string/app_name"
android:theme= "@style/apptheme" android:name= "floatapplication" >
<activity
Android:name=. Mainactivity "
android:label=" @string/title_activity_main ">
<intent-filter>
<action Android : Name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.