聖誕節,寫個程式練練手————Android 全介面懸浮按鈕實現_Android

來源:互聯網
上載者:User

開始我以為懸浮視窗,可以用Android中得PopupWindow 來實現,雖然也實現了,但局限性非常大。比如PopupWindow必須要有載體View,也就是說,必須要指定在那個View的上面來實現。以該View為相對位置,來顯示PopupWindow。這就局限了其智能在使用者互動的視窗上,相對的顯示。而無法自由的拖動位置和在案頭顯示。

於是查閱了一些資料,有兩種實現方法。一種是自訂Toast,Toast是運行於所有介面之上的,也就是說沒有介面可以覆蓋它。另一種是Android中得CompatModeWrapper類,ConmpatModeWrapper是基類,實現大部分功能的是它的內部類WindowManagerImpl。該對象可以通過getApplication().getSystemService(Context.WINDOW_SERVICE)得到。(註:如果是通過activity.getSystemService(Context.WINDOW_SERVICE)得到的只是屬於Activity的LocalWindowManager)。

簡單的介紹之後,我們直接來看代碼實現,注釋已經寫在代碼中。

MainActivity.javapackage com.example.floatviewdemo;import com.example.floatviewdemo.service.FloatViewService;import android.app.Activity;import android.content.Intent;import android.os.Bundle;public class MainActivity extends Activity {  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);  }  @Override protected void onStart() {  Intent intent = new Intent(MainActivity.this, FloatViewService.class);      //啟動FloatViewService      startService(intent);  super.onStart(); }  @Override protected void onStop() { // 銷毀懸浮窗 Intent intent = new Intent(MainActivity.this, FloatViewService.class);     //終止FloatViewService     stopService(intent);     super.onStop(); }}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="#fff"  android:paddingBottom="@dimen/activity_vertical_margin"  android:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  tools:context="com.example.floatviewdemo.MainActivity" >  <TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/hello_world" /></RelativeLayout>

實現懸浮窗功能的service類

package com.example.floatviewdemo.service;import com.example.floatviewdemo.R;import android.annotation.SuppressLint;import android.app.Service;import android.content.Intent;import android.graphics.PixelFormat;import android.os.IBinder;import android.util.Log;import android.view.Gravity;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.view.WindowManager;import android.view.WindowManager.LayoutParams;import android.widget.ImageButton;import android.widget.LinearLayout;import android.widget.Toast;public class FloatViewService extends Service  {  private static final String TAG = "FloatViewService";   //定義浮動視窗布局   private LinearLayout mFloatLayout;   private WindowManager.LayoutParams wmParams;   //建立浮動視窗設定布局參數的對象   private WindowManager mWindowManager;   private ImageButton mFloatView;   @Override   public void onCreate()    {     super.onCreate();     Log.i(TAG, "onCreate");     createFloatView();      }   @SuppressWarnings("static-access") @SuppressLint("InflateParams") private void createFloatView()   {     wmParams = new WindowManager.LayoutParams();     //通過getApplication擷取的是WindowManagerImpl.CompatModeWrapper     mWindowManager = (WindowManager)getApplication().getSystemService(getApplication().WINDOW_SERVICE);     //設定window type     wmParams.type = LayoutParams.TYPE_PHONE;      //設定圖片格式,效果為背景透明     wmParams.format = PixelFormat.RGBA_8888;      //設定浮動視窗不可聚焦(實現操作除浮動視窗外的其他可見視窗的操作)     wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;        //調整懸浮窗顯示的固定位置為左側置頂     wmParams.gravity = Gravity.LEFT | Gravity.TOP;         // 以螢幕左上方為原點,設定x、y初始值,相對於gravity     wmParams.x = 0;     wmParams.y = 152;     //設定懸浮視窗長寬資料      wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;     wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;     LayoutInflater inflater = LayoutInflater.from(getApplication());     //擷取浮動視窗視圖所在布局     mFloatLayout = (LinearLayout) inflater.inflate(R.layout.alert_window_menu, null);     //添加mFloatLayout     mWindowManager.addView(mFloatLayout, wmParams);     //浮動視窗按鈕     mFloatView = (ImageButton) mFloatLayout.findViewById(R.id.alert_window_imagebtn);    mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0,         View.MeasureSpec.UNSPECIFIED), View.MeasureSpec         .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));     //設定監聽浮動視窗的觸摸移動     mFloatView.setOnTouchListener(new OnTouchListener()      {      boolean isClick;  @SuppressLint("ClickableViewAccessibility") @Override  public boolean onTouch(View v, MotionEvent event) {  switch (event.getAction()) {  case MotionEvent.ACTION_DOWN:   mFloatView.setBackgroundResource(R.drawable.circle_red);   isClick = false;   break;  case MotionEvent.ACTION_MOVE:   isClick = true;   // getRawX是觸摸位置相對於螢幕的座標,getX是相對於按鈕的座標   wmParams.x = (int) event.getRawX()    - mFloatView.getMeasuredWidth() / 2;   // 減25為狀態列的高度   wmParams.y = (int) event.getRawY()    - mFloatView.getMeasuredHeight() / 2 - 75;   // 重新整理   mWindowManager.updateViewLayout(mFloatLayout, wmParams);   return true;  case MotionEvent.ACTION_UP:   mFloatView.setBackgroundResource(R.drawable.circle_cyan);   return isClick;// 此處返回false則屬於移動事件,返回true則釋放事件,可以出發點擊否。  default:   break;  }  return false;  }    });      mFloatView.setOnClickListener(new OnClickListener()      {       @Override       public void onClick(View v)        {         Toast.makeText(FloatViewService.this, "一百塊都不給我!", Toast.LENGTH_SHORT).show();       }     });   }   @Override   public void onDestroy()    {     super.onDestroy();     if(mFloatLayout != null)     {       //移除懸浮視窗       mWindowManager.removeView(mFloatLayout);     }   } @Override public IBinder onBind(Intent intent) { return null; } }

懸浮窗的xml檔案

alert_window_menu.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:orientation="vertical" >  <ImageButton    android:id="@+id/alert_window_imagebtn"     android:layout_width="50dp"    android:layout_height="50dp"    android:background="@drawable/float_window_menu"    android:contentDescription="@null"    /></LinearLayout>

以上內容是實現Android 全介面懸浮按鈕的全部敘述,希望大家喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.