欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

圣誕節(jié),寫個程序練練手————Android 全界面懸浮按鈕實現(xiàn)

 更新時間:2015年12月25日 14:51:04   投稿:mrr  
這篇文章主要介紹了圣誕節(jié),寫個程序練練手————Android 全界面懸浮按鈕實現(xiàn)的相關(guān)資料,需要的朋友可以參考下

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

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

簡單的介紹之后,我們直接來看代碼實現(xiàn),注釋已經(jīng)寫在代碼中。

MainActivity.java
package 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>

實現(xiàn)懸浮窗功能的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; 
  //創(chuàng)建浮動窗口設(shè)置布局參數(shù)的對象 
  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); 
    //設(shè)置window type 
    wmParams.type = LayoutParams.TYPE_PHONE;  
    //設(shè)置圖片格式,效果為背景透明 
    wmParams.format = PixelFormat.RGBA_8888;  
    //設(shè)置浮動窗口不可聚焦(實現(xiàn)操作除浮動窗口外的其他可見窗口的操作) 
    wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;    
    //調(diào)整懸浮窗顯示的停靠位置為左側(cè)置頂 
    wmParams.gravity = Gravity.LEFT | Gravity.TOP;     
    // 以屏幕左上角為原點,設(shè)置x、y初始值,相對于gravity 
    wmParams.x = 0; 
    wmParams.y = 152; 
    //設(shè)置懸浮窗口長寬數(shù)據(jù)  
    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)); 
    //設(shè)置監(jiān)聽浮動窗口的觸摸移動 
    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為狀態(tài)欄的高度
   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則釋放事件,可以出發(fā)點擊否。
  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>

以上內(nèi)容是實現(xiàn)Android 全界面懸浮按鈕的全部敘述,希望大家喜歡。

相關(guān)文章

  • android常見手動和自動輪播圖效果

    android常見手動和自動輪播圖效果

    這篇文章主要為大家詳細介紹了android常見手動和自動輪播圖效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Android 虛擬按鍵與沉浸式的適配方法

    Android 虛擬按鍵與沉浸式的適配方法

    今天小編就為大家分享一篇Android 虛擬按鍵與沉浸式的適配方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • android?viewflipper實現(xiàn)左右滑動切換顯示圖片

    android?viewflipper實現(xiàn)左右滑動切換顯示圖片

    這篇文章主要為大家詳細介紹了android?viewflipper實現(xiàn)左右滑動切換顯示圖片,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android開發(fā)實現(xiàn)文件存儲功能

    Android開發(fā)實現(xiàn)文件存儲功能

    這篇文章主要為大家詳細介紹了Android開發(fā)實現(xiàn)文件存儲功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • Android 仿淘寶商品屬性標簽頁

    Android 仿淘寶商品屬性標簽頁

    這篇文章主要介紹了Android 仿淘寶商品屬性標簽頁的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-11-11
  • Android GridView不改變背景色實現(xiàn)網(wǎng)格線效果

    Android GridView不改變背景色實現(xiàn)網(wǎng)格線效果

    這篇文章主要介紹了Android GridView不改變背景色實現(xiàn)網(wǎng)格線效果,需要的朋友可以參考下
    2016-03-03
  • Android衛(wèi)星菜單效果的實現(xiàn)方法

    Android衛(wèi)星菜單效果的實現(xiàn)方法

    這篇文章主要介紹了Android衛(wèi)星菜單效果的實現(xiàn)方法,需要的朋友可以參考下
    2017-05-05
  • Android?PopUpWindow實現(xiàn)卡片式彈窗

    Android?PopUpWindow實現(xiàn)卡片式彈窗

    大家好,本篇文章主要講的是Android?PopUpWindow實現(xiàn)卡片式彈窗,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Kotlin的Collection與Sequence操作異同點詳解

    Kotlin的Collection與Sequence操作異同點詳解

    這篇文章主要介紹了Kotlin的Collection與Sequence操作異同點詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • Android開發(fā)技巧之Fragment的懶加載

    Android開發(fā)技巧之Fragment的懶加載

    我們都知道fragment放在viewPager里面,viewpager會幫我們預先加載一個,但是當我們要看fragment里面的內(nèi)容時,我們也許只會去看第一個,不會去看第二個,如果這時候不去實現(xiàn)fragment的懶加載的話,就會多余的去加載一些數(shù)據(jù),造成用戶多消耗流量。下面來一起看看吧。
    2016-10-10

最新評論