日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 360 悬浮窗,悬浮窗的实现(如360悬浮窗效果)

發布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 360 悬浮窗,悬浮窗的实现(如360悬浮窗效果) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

懸浮窗實現

相信大家,都知曉360的懸浮窗口,非常瀟灑。。。

如圖:

現在,我也實現了具有吸附效果的懸浮窗。有圖有真相...

看圖: 吸附屏幕兩側的效果

廢話少說,看代碼。代碼中,有詳細注釋...

布局文件:floating.xml(懸浮窗的布局)

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

tools:context=".MainActivity"

android:orientation="horizontal"

>

android:id="@+id/icon"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/rekoo"

/>

android:id="@+id/floats"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:background="@drawable/settings_bg_right"

android:layout_marginTop="2dp"

android:visibility="gone"

>

android:id="@+id/btn1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical"

android:layout_marginLeft="2dp"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/settings_bbs_pressed" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="論壇"

android:textColor="@android:color/white"

/>

android:id="@+id/btn2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="10dp"

android:orientation="vertical" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/settings_bind_phone_pressed" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="電話"

android:textColor="@android:color/white" />

android:id="@+id/btn3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="10dp"

android:orientation="vertical" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/settings_service_pressed" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="關閉"

android:textColor="@android:color/white" />

源碼:

package org.lqh.floatDemo;

import android.app.Activity;

import android.content.Context;

import android.graphics.PixelFormat;

import android.graphics.Rect;

import android.os.Bundle;

import android.view.Display;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.Window;

import android.view.WindowManager;

import android.view.WindowManager.LayoutParams;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener

{

private WindowManager windowManager = null;

private WindowManager.LayoutParams windowManagerParams = null;

private float mTouchX;

private float mTouchY;

private float x;

private float y;

private float mStartX;

private float mStartY;

private View view;

private ImageView icon;

private LinearLayout floats;

private LinearLayout btn1;

private LinearLayout btn2;

private LinearLayout btn3;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);//取消標題欄

getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,

WindowManager.LayoutParams. FLAG_FULLSCREEN);//全屏

setContentView(R.layout.activity_main);

view = LayoutInflater.from(this).inflate(R.layout.floating, null);

icon = (ImageView) view.findViewById(R.id.icon);

floats = (LinearLayout) view.findViewById(R.id.floats);

btn1 = (LinearLayout) floats.findViewById(R.id.btn1);

btn2 = (LinearLayout) floats.findViewById(R.id.btn2);

btn3 = (LinearLayout) floats.findViewById(R.id.btn3);

btn1.setOnClickListener(this);

btn2.setOnClickListener(this);

btn3.setOnClickListener(this);

createView();

}

private void createView() {

// 1、獲取WindowManager對象

windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

// 2、設置LayoutParams(全局變量)相關參數

windowManagerParams = new WindowManager.LayoutParams();

// windowManagerParams = ((FloatApplication) getApplication()).getWindowParams();

//3、設置相關的窗口布局參數 (懸浮窗口效果)

windowManagerParams.type = LayoutParams.TYPE_PHONE; // 設置window type

windowManagerParams.format = PixelFormat.RGBA_8888; // 設置圖片格式,效果為背景透明

//4、設置Window flag == 不影響后面的事件 和 不可聚焦

windowManagerParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL

| LayoutParams.FLAG_NOT_FOCUSABLE;

/*

* 注意,flag的值可以為:

* LayoutParams.FLAG_NOT_TOUCH_MODAL 不影響后面的事件

* LayoutParams.FLAG_NOT_FOCUSABLE 不可聚焦

* LayoutParams.FLAG_NOT_TOUCHABLE 不可觸摸

*/

//5、 調整懸浮窗口至左上角,便于調整坐標

windowManagerParams.gravity = Gravity.LEFT | Gravity.TOP;

// 以屏幕左上角為原點,設置x、y初始值

windowManagerParams.x = 0;

windowManagerParams.y = 80;

//6、設置懸浮窗口長寬數據

windowManagerParams.width = LayoutParams.WRAP_CONTENT;

windowManagerParams.height = LayoutParams.WRAP_CONTENT;

//獲得屏幕的寬高

Display display = windowManager.getDefaultDisplay();

final int screenWith = display.getWidth();

int screenHeight = display.getHeight();

System.out.println("screenWith="+screenWith+",screenHeight="+screenHeight);

icon.setOnTouchListener(new View.OnTouchListener()

{

@Override

public boolean onTouch(View v, MotionEvent event)

{

//1、獲取到狀態欄的高度

Rect frame = new Rect();

icon.getWindowVisibleDisplayFrame(frame);

int statusBarHeight = frame.top;

System.out.println("狀態欄高度:"+statusBarHeight);

//2、獲取相對屏幕的坐標,即以屏幕左上角為原點 。y軸坐標= y(獲取到屏幕原點的距離)-狀態欄的高度

x = event.getRawX();

y = event.getRawY() - statusBarHeight; // statusBarHeight是系統狀態欄的高度

System.out.println("x="+x+",y="+y);

//3、處理觸摸移動

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN: // 捕獲手指觸摸按下動作

// 獲取相對View的坐標,即以此View左上角為原點

mTouchX = event.getX();

mTouchY = event.getY();

mStartX = x;

mStartY = y;

System.out.println(",mTouchX=" + mTouchX + ",mTouchY="

+ mTouchY);

break;

case MotionEvent.ACTION_MOVE: // 捕獲手指觸摸移動動作

updateViewPosition();

break;

case MotionEvent.ACTION_UP: // 捕獲手指觸摸離開動作

float left = x-mTouchX;

if(left <= screenWith/2){//圖標icon吸附在左邊

x = mTouchX;

}else {//圖標icon吸附在右邊

x = mTouchX + screenWith;

}

updateViewPosition();

//移動終點的坐標,重置為0

mTouchX = mTouchY = 0;

//移動距離少于5 ,則視為點擊,觸發點擊的回調

if ((x - mStartX) < 5 && (y - mStartY) < 5) {

onClick(v);

}

break;

}

return true;

}

});

windowManager.addView(view, windowManagerParams); // 顯示myFloatView圖像

}

/**

* 用于更新 懸浮窗位置參數

* */

private void updateViewPosition() {

windowManagerParams.x = (int) (x - mTouchX);

windowManagerParams.y = (int) (y - mTouchY);

System.out.println("wp.x="+windowManagerParams.x+",wp.y="+windowManagerParams.y);

// 刷新屏幕顯示

windowManager.updateViewLayout(view, windowManagerParams);

}

@Override

public void onClick(View v)

{

switch (v.getId())

{

case R.id.icon:

if (floats.getVisibility() == View.VISIBLE)

{

floats.setVisibility(View.GONE);

} else {

floats.setVisibility(View.VISIBLE);

}

break;

case R.id.btn1:

Toast.makeText(this,"親,我是論壇!", 2).show();

break;

case R.id.btn2:

Toast.makeText(this,"親,我是手機驗證!", 2).show();

break;

case R.id.btn3:

windowManager.removeView(view);

android.os.Process.killProcess(android.os.Process.myPid());

break;

default:

break;

}

}

public void onDestroy() {

super.onDestroy();

// 在程序退出(Activity銷毀)時銷毀懸浮窗口

// windowManager.removeView(view);

}

}

如有大家,不明白之處,盡情留言評價。。。

來源:https://www.cnblogs.com/codeAnimal/p/3274508.html

總結

以上是生活随笔為你收集整理的android 360 悬浮窗,悬浮窗的实现(如360悬浮窗效果)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。