android編程實現懸浮表單的方法_Android

來源:互聯網
上載者:User

本文執行個體講述了android編程實現懸浮表單的方法。分享給大家供大家參考,具體如下:

突然對懸浮表單感興趣,查資料做了個小Demo,效果是點擊按鈕後,關閉當前Activity,顯示懸浮視窗,視窗可以拖動,雙擊後消失。效果圖如下:

它的使用原理很簡單,就是借用了WindowManager這個管理類來實現的。

1.首先在AndroidManifest.xml中添加使用許可權:

複製代碼 代碼如下:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

2.懸浮視窗布局實現

public class DesktopLayout extends LinearLayout {  public DesktopLayout(Context context) {    super(context);    setOrientation(LinearLayout.VERTICAL);// 水平排列    //設定寬高    this.setLayoutParams( new LayoutParams(LayoutParams.WRAP_CONTENT,        LayoutParams.WRAP_CONTENT));    View view = LayoutInflater.from(context).inflate(         R.layout.desklayout, null);     this.addView(view);  }

3.在activity中讓它顯示出來。

// 取得系統表單mWindowManager = (WindowManager) getApplicationContext()    .getSystemService("window");// 表單的配置樣式mLayout = new WindowManager.LayoutParams();// 設定表單顯示類型——TYPE_SYSTEM_ALERT(系統提示)mLayout.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;// 設定表單焦點及觸摸:// FLAG_NOT_FOCUSABLE(不能獲得按鍵輸入焦點)mLayout.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;// 設定顯示的模式mLayout.format = PixelFormat.RGBA_8888;// 設定對齊的方法mLayout.gravity = Gravity.TOP | Gravity.LEFT;// 設定表單寬度和高度mLayout.width = WindowManager.LayoutParams.WRAP_CONTENT;mLayout.height = WindowManager.LayoutParams.WRAP_CONTENT;

詳細 MainActivity 代碼如下:

package com.yc.yc_suspendingform;import android.app.Activity;import android.graphics.PixelFormat;import android.graphics.Rect;import android.os.Bundle;import android.util.Log;import android.view.Gravity;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.view.WindowManager;import android.widget.Button;import com.yc.yc_floatingform.R;public class MainActivity extends Activity {  private WindowManager mWindowManager;  private WindowManager.LayoutParams mLayout;  private DesktopLayout mDesktopLayout;  private long startTime;  // 聲明螢幕的寬高  float x, y;  int top;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);        createWindowManager();    createDesktopLayout();    Button btn = (Button) findViewById(R.id.btn);    btn.setOnClickListener(new OnClickListener() {      public void onClick(View v) {        showDesk();      }    });  }  /**   * 建立懸浮表單   */  private void createDesktopLayout() {    mDesktopLayout = new DesktopLayout(this);    mDesktopLayout.setOnTouchListener(new OnTouchListener() {      float mTouchStartX;      float mTouchStartY;      @Override      public boolean onTouch(View v, MotionEvent event) {        // 擷取相對螢幕的座標,即以螢幕左上方為原點        x = event.getRawX();        y = event.getRawY() - top; // 25是系統狀態列的高度        Log.i("startP", "startX" + mTouchStartX + "====startY"            + mTouchStartY);        switch (event.getAction()) {        case MotionEvent.ACTION_DOWN:          // 擷取相對View的座標,即以此View左上方為原點          mTouchStartX = event.getX();          mTouchStartY = event.getY();          Log.i("startP", "startX" + mTouchStartX + "====startY"              + mTouchStartY);          long end = System.currentTimeMillis() - startTime;          // 雙擊的間隔在 300ms以下          if (end < 300) {            closeDesk();          }          startTime = System.currentTimeMillis();          break;        case MotionEvent.ACTION_MOVE:          // 更新浮動視窗位置參數          mLayout.x = (int) (x - mTouchStartX);          mLayout.y = (int) (y - mTouchStartY);          mWindowManager.updateViewLayout(v, mLayout);          break;        case MotionEvent.ACTION_UP:          // 更新浮動視窗位置參數          mLayout.x = (int) (x - mTouchStartX);          mLayout.y = (int) (y - mTouchStartY);          mWindowManager.updateViewLayout(v, mLayout);          // 可以在此記錄最後一次的位置          mTouchStartX = mTouchStartY = 0;          break;        }        return true;      }    });  }  @Override  public void onWindowFocusChanged(boolean hasFocus) {    super.onWindowFocusChanged(hasFocus);    Rect rect = new Rect();    // /取得整個視圖部分,注意,如果你要設定標題樣式,這個必須出現在標題樣式之後,否則會出錯    getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);    top = rect.top;//狀態列的高度,所以rect.height,rect.width分別是系統的高度的寬度    Log.i("top",""+top);  }  /**   * 顯示DesktopLayout   */  private void showDesk() {    mWindowManager.addView(mDesktopLayout, mLayout);    finish();  }  /**   * 關閉DesktopLayout   */  private void closeDesk() {    mWindowManager.removeView(mDesktopLayout);    finish();  }  /**   * 設定WindowManager   */  private void createWindowManager() {    // 取得系統表單    mWindowManager = (WindowManager) getApplicationContext()        .getSystemService("window");    // 表單的配置樣式    mLayout = new WindowManager.LayoutParams();    // 設定表單顯示類型——TYPE_SYSTEM_ALERT(系統提示)    mLayout.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;    // 設定表單焦點及觸摸:    // FLAG_NOT_FOCUSABLE(不能獲得按鍵輸入焦點)    mLayout.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;    // 設定顯示的模式    mLayout.format = PixelFormat.RGBA_8888;    // 設定對齊的方法    mLayout.gravity = Gravity.TOP | Gravity.LEFT;    // 設定表單寬度和高度    mLayout.width = WindowManager.LayoutParams.WRAP_CONTENT;    mLayout.height = WindowManager.LayoutParams.WRAP_CONTENT;  }}

完整執行個體代碼代碼點擊此處本站下載。

希望本文所述對大家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.