悬浮窗效果
package com.loaderman.customviewdemo; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.PixelFormat; import android.os.Build; import android.os.Bundle; import android.provider.Settings; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity implements View.OnTouchListener, View.OnClickListener { private Button mCreateWndBtn, mRmvWndBtn; private ImageView mImageView; private WindowManager.LayoutParams mLayoutParams; private WindowManager mWindowManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); startActivityForResult(myIntent, 100); } else { initView(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 100) { initView(); } } private void initView() { mCreateWndBtn = (Button) findViewById(R.id.add_btn); mRmvWndBtn = (Button) findViewById(R.id.rmv_btn); mCreateWndBtn.setOnClickListener(this); mRmvWndBtn.setOnClickListener(this); mWindowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); } @Override public boolean onTouch(View v, MotionEvent event) { int rawX = (int) event.getRawX(); int rawY = (int) event.getRawY(); switch (event.getAction()) { case MotionEvent.ACTION_MOVE: { mLayoutParams.x = rawX; mLayoutParams.y = rawY; mWindowManager.updateViewLayout(mImageView, mLayoutParams); break; } default: break; } return false; } @Override protected void onDestroy() { try { mWindowManager.removeView(mImageView); } catch (IllegalArgumentException e) { e.printStackTrace(); } super.onDestroy(); } @Override public void onClick(View v) { if (v.getId() == R.id.add_btn) { mImageView = new ImageView(this); mImageView.setBackgroundResource(R.mipmap.ic_launcher); mLayoutParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, 2099, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED //控制window的显示特性 , PixelFormat.TRANSPARENT); mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR; mLayoutParams.gravity = Gravity.TOP | Gravity.LEFT; mLayoutParams.x = 0; mLayoutParams.y = 300; mImageView.setOnTouchListener(this); mWindowManager.addView(mImageView, mLayoutParams); } else if (v.getId() == R.id.rmv_btn) { mWindowManager.removeViewImmediate(mImageView); } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/add_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="add view"/> <Button android:id="@+id/rmv_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="remove view"/> </LinearLayout>
添加权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
效果:
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!