高仿360等系統級懸浮按鈕

package com.example.com.hengsing;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity { 
    //一個有隻有一個按鈕的activity 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        Button btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    funClick(null);
   }
  });
       
    } 
    public void funClick(View v){ 
        //按鈕被點擊 
       this.startService(new Intent(this,Mser.class)); 
// new TableShowView(this).fun(); 如果只是在activity中啓動  
// 當activity跑去後臺的時候[暫停態,或者銷燬態] 我們設置的顯示到桌面的view也會消失 
// 所以這裏採用的是啓動一個服務,服務中創建我們需要顯示到table上的view,並將其註冊到windowManager上   
        this.finish(); 
    } 

package com.example.com.hengsing;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class Mser extends Service { 
    //服務 
    //這個類純蛋疼用 只是爲了在activity點擊button後 在開啓一個service  
     
    @Override 
    public IBinder onBind(Intent intent) { 
        // TODO Auto-generated method stub 
        return null; 
    } 
 
    public void onCreate() { 
  //創建service時一個 實例化一個TableShowView對象並且調用他的fun()方法把它註冊到windowManager上 
        super.onCreate(); 
        new TableShowView(this).fun(); 
    } 
 
 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
        // TODO Auto-generated method stub 
        return super.onStartCommand(intent, flags, startId); 
    } 

 

package com.example.com.hengsing;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

public class TableShowView extends View {
 // 如果是想顯示歌詞則繼承TextView並複寫ondraw方法。
 // 開啓一個線程不斷的調用ondraw方法去更改你所寫的繼承自TextView的內容
 // 這裏隨便寫了個集成自view的= =這個不是重點

 Context      c;
 WindowManager    mWM;  // WindowManager
 WindowManager.LayoutParams mWMParams; // WindowManager參數
 View      win;
 int tag = 0;
 int oldOffsetX;
 int oldOffsetY;

 public TableShowView(Context context) {
  // TODO Auto-generated constructor stub
  super(context);
  c = context;
 }

 public void fun() {
  // 設置載入view WindowManager參數
  mWM = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
  win = LayoutInflater.from(c).inflate(R.layout.ctrl_window, null);
  win.setBackgroundColor(Color.TRANSPARENT);
  // 這裏是隨便載入的一個佈局文件
  
  win.setOnTouchListener(new OnTouchListener() {
   // 觸屏監聽
   float lastX, lastY;

   public boolean onTouch(View v, MotionEvent event) {
    final int action = event.getAction();

    float x = event.getX();
    float y = event.getY();
    
    if(tag == 0){
       oldOffsetX= mWMParams.x; // 偏移量
       oldOffsetY = mWMParams.y; // 偏移量
    }
    
      
    if (action == MotionEvent.ACTION_DOWN) {
     lastX = x;
     lastY = y;

    }
    else if (action == MotionEvent.ACTION_MOVE) {
     mWMParams.x += (int) (x - lastX); // 偏移量
     mWMParams.y += (int) (y - lastY); // 偏移量
     
     tag = 1;
     mWM.updateViewLayout(win, mWMParams);
    }

    else if (action ==  MotionEvent.ACTION_UP){
     int newOffsetX = mWMParams.x;
     int newOffsetY = mWMParams.y;     
     if(oldOffsetX == newOffsetX && oldOffsetY == newOffsetY){
      Toast.makeText(c, "你點到我了……疼!!!!", 1).show();
     }else {
      tag = 0;
     }
    }
    return true;
   }
  });
  


  WindowManager wm = mWM;
  WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
  mWMParams = wmParams;
  wmParams.type = 2003; // type是關鍵,這裏的2002表示系統級窗口,你也可以試試2003。
  wmParams.flags = 40;// 這句設置桌面可控

  wmParams.width = 50;
  wmParams.height = 50;
  wmParams.format = -3; // 透明

  wm.addView(win, wmParams);// 這句是重點 給WindowManager中丟入剛纔設置的值
         // 只有addview後才能顯示到頁面上去。
  // 註冊到WindowManager win是要剛纔隨便載入的layout,wmParams是剛纔設置的WindowManager參數集
  // 效果是將win註冊到WindowManager中並且它的參數是wmParams中設置餓

  
  

 }

}
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章