Android 如何旋转系统覆盖按钮

Android 如何旋转系统覆盖按钮,android,system,overlay,Android,System,Overlay,我在互联网上找到了这段代码,它很有效 它创建了一个系统覆盖按钮,我可以在屏幕上移动它 我该怎么做,这个按钮现在应该可以旋转了 也许是两个手指的旋转 Package de.mobilej.overlay; import android.app.Service; import android.content.Context; import android.content.Intent; import android.graphics.PixelFormat; import android.os.IB

我在互联网上找到了这段代码,它很有效

它创建了一个系统覆盖按钮,我可以在屏幕上移动它

我该怎么做,这个按钮现在应该可以旋转了

也许是两个手指的旋转

Package de.mobilej.overlay;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
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.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.Toast;

public class OverlayShowingService extends Service implements OnTouchListener, OnClickListener {
private View topLeftView;

private Button overlayedButton;
private float offsetX;
private float offsetY;
private int originalXPos;
private int originalYPos;
private boolean moving;
private WindowManager wm;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

    wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

    overlayedButton = new Button(this);
    overlayedButton.setText("TEST TEST TEST TEST TEST TEST TEST TEST TEST");
    overlayedButton.setOnTouchListener(this);
    overlayedButton.setBackgroundColor(0x55fe4444);
    overlayedButton.setOnClickListener(this);

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.LEFT | Gravity.TOP;
    params.x = 0;
    params.y = 0;
    wm.addView(overlayedButton, params);

    topLeftView = new View(this);
    WindowManager.LayoutParams topLeftParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
    topLeftParams.gravity = Gravity.LEFT | Gravity.TOP;
    topLeftParams.x = 0;
    topLeftParams.y = 0;
    topLeftParams.width = 10;
    topLeftParams.height = 10;
    wm.addView(topLeftView, topLeftParams);

}

@Override
public void onDestroy() {
    super.onDestroy();
    if (overlayedButton != null) {
        wm.removeView(overlayedButton);
        wm.removeView(topLeftView);
        overlayedButton = null;
        topLeftView = null;
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        float x = event.getRawX();
        float y = event.getRawY();

        moving = false;
        int[] location = new int[2];
        overlayedButton.getLocationOnScreen(location);

        originalXPos = location[0];
        originalYPos = location[1];

        offsetX = originalXPos - x;
        offsetY = originalYPos - y;

    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        int[] topLeftLocationOnScreen = new int[2];
        topLeftView.getLocationOnScreen(topLeftLocationOnScreen);
        System.out.println("topLeftY="+topLeftLocationOnScreen[1]);
        System.out.println("originalY="+originalYPos);

        float x = event.getRawX();
        float y = event.getRawY();
       WindowManager.LayoutParams params = (LayoutParams) overlayedButton.getLayoutParams();

        int newX = (int) (offsetX + x);
        int newY = (int) (offsetY + y);

        if (Math.abs(newX - originalXPos) < 1 && Math.abs(newY - originalYPos) < 1 && !moving) {
            return false;
        }

        params.x = newX - (topLeftLocationOnScreen[0]);
        params.y = newY - (topLeftLocationOnScreen[1]);
        wm.updateViewLayout(overlayedButton, params);
        moving = true;
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        if (moving) {
            return true;
        }
    }

    return false;
}

@Override
public void onClick(View v) {
    Toast.makeText(this, "Overlay button click event", Toast.LENGTH_SHORT).show();

}}
Package de.mobilej.overlay;
导入android.app.Service;
导入android.content.Context;
导入android.content.Intent;
导入android.graphics.PixelFormat;
导入android.os.IBinder;
导入android.view.Gravity;
导入android.view.MotionEvent;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.view.OnTouchListener;
导入android.view.WindowManager;
导入android.view.WindowManager.LayoutParams;
导入android.widget.Button;
导入android.widget.Toast;
公共类OverlyShowingService将服务实现扩展到OnTouchListener、OnClickListener{
私有视图topLeftView;
私有按钮覆盖按钮;
私人浮动抵消;
私人浮动补贴;
私人int原创XPOS;
私人内部原始版本;
私有布尔移动;
私有窗口管理器;
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
public void onCreate(){
super.onCreate();
wm=(WindowManager)getSystemService(Context.WINDOW\u服务);
overlayedButton=新按钮(此按钮);
setText(“测试”);
setOnTouchListener(这个);
重叠按钮。设置背景颜色(0x55fe4444);
setOnClickListener(这个);
WindowManager.LayoutParams params=新建WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_内容,WindowManager.LayoutParams.WRAP_内容,WindowManager.LayoutParams.TYPE_系统_警报,WindowManager.LayoutParams.FLAG_不可聚焦| WindowManager.LayoutParams.FLAG_不可触摸(模式,像素格式.半透明);
params.gravity=gravity.LEFT | gravity.TOP;
参数x=0;
参数y=0;
wm.addView(覆盖按钮,参数);
topLeftView=新视图(此视图);
WindowManager.LayoutParams topLeftParams=新建WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_内容,WindowManager.LayoutParams.WRAP_内容,WindowManager.LayoutParams.TYPE_系统_警报,WindowManager.LayoutParams.FLAG_不可聚焦| WindowManager.LayoutParams.FLAG_不可触摸|模式,像素格式.半透明);
topLeftParams.gravity=gravity.LEFT | gravity.TOP;
topLeftParams.x=0;
topLeftParams.y=0;
topLeftParams.width=10;
topLeftParams.height=10;
wm.addView(topLeftView,topLeftParams);
}
@凌驾
公共空间{
super.ondestory();
if(overlayedButton!=null){
wm.removeView(覆盖按钮);
wm.removeView(topLeftView);
overlayedButton=null;
topLeftView=null;
}
}
@凌驾
公共布尔onTouch(视图v,运动事件){
if(event.getAction()==MotionEvent.ACTION\u向下){
float x=event.getRawX();
float y=event.getRawY();
移动=假;
int[]位置=新int[2];
覆盖按钮。getLocationOnScreen(位置);
originalXPos=位置[0];
原始ypos=位置[1];
offsetX=原始xpos-x;
offsetY=原始ypos-y;
}else if(event.getAction()==MotionEvent.ACTION\u MOVE){
int[]topLeftLocationOnScreen=新建int[2];
topLeftView.GetLocationOn屏幕(TopLeftLocationOn屏幕);
System.out.println(“topLeftY=“+topLeftLocationOnScreen[1]);
System.out.println(“originalY=“+originalYPos”);
float x=event.getRawX();
float y=event.getRawY();
WindowManager.LayoutParams params=(LayoutParams)overlayedButton.getLayoutParams();
int newX=(int)(抵销x+x);
int newY=(int)(offsetY+y);
if(Math.abs(newX-originalXPos)<1&&Math.abs(newY-originalYPos)<1&&moving){
返回false;
}
参数x=newX-(屏幕上的topLeftLocationOnScreen[0]);
参数y=newY-(屏幕上的TopLeftLocation[1]);
wm.UpdateView布局(覆盖按钮,参数);
移动=真;
}else if(event.getAction()==MotionEvent.ACTION\u UP){
如果(移动){
返回true;
}
}
返回false;
}
@凌驾
公共void onClick(视图v){
Toast.makeText(这是“覆盖按钮单击事件”,Toast.LENGTH_SHORT.show();
}}

旋转系统覆盖与旋转视图无异。您可以从截取触摸开始,然后将其转换为视图旋转

Package de.mobilej.overlay;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
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.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.Toast;

public class OverlayShowingService extends Service implements OnTouchListener, OnClickListener {
private View topLeftView;

private Button overlayedButton;
private float offsetX;
private float offsetY;
private int originalXPos;
private int originalYPos;
private boolean moving;
private WindowManager wm;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

    wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

    overlayedButton = new Button(this);
    overlayedButton.setText("TEST TEST TEST TEST TEST TEST TEST TEST TEST");
    overlayedButton.setOnTouchListener(this);
    overlayedButton.setBackgroundColor(0x55fe4444);
    overlayedButton.setOnClickListener(this);

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.LEFT | Gravity.TOP;
    params.x = 0;
    params.y = 0;
    wm.addView(overlayedButton, params);

    topLeftView = new View(this);
    WindowManager.LayoutParams topLeftParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
    topLeftParams.gravity = Gravity.LEFT | Gravity.TOP;
    topLeftParams.x = 0;
    topLeftParams.y = 0;
    topLeftParams.width = 10;
    topLeftParams.height = 10;
    wm.addView(topLeftView, topLeftParams);

}

@Override
public void onDestroy() {
    super.onDestroy();
    if (overlayedButton != null) {
        wm.removeView(overlayedButton);
        wm.removeView(topLeftView);
        overlayedButton = null;
        topLeftView = null;
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        float x = event.getRawX();
        float y = event.getRawY();

        moving = false;
        int[] location = new int[2];
        overlayedButton.getLocationOnScreen(location);

        originalXPos = location[0];
        originalYPos = location[1];

        offsetX = originalXPos - x;
        offsetY = originalYPos - y;

    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        int[] topLeftLocationOnScreen = new int[2];
        topLeftView.getLocationOnScreen(topLeftLocationOnScreen);
        System.out.println("topLeftY="+topLeftLocationOnScreen[1]);
        System.out.println("originalY="+originalYPos);

        float x = event.getRawX();
        float y = event.getRawY();
       WindowManager.LayoutParams params = (LayoutParams) overlayedButton.getLayoutParams();

        int newX = (int) (offsetX + x);
        int newY = (int) (offsetY + y);

        if (Math.abs(newX - originalXPos) < 1 && Math.abs(newY - originalYPos) < 1 && !moving) {
            return false;
        }

        params.x = newX - (topLeftLocationOnScreen[0]);
        params.y = newY - (topLeftLocationOnScreen[1]);
        wm.updateViewLayout(overlayedButton, params);
        moving = true;
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        if (moving) {
            return true;
        }
    }

    return false;
}

@Override
public void onClick(View v) {
    Toast.makeText(this, "Overlay button click event", Toast.LENGTH_SHORT).show();

}}
要实现双手指触摸旋转,您需要获得移动的幅度,然后将其转换为应旋转视图的度数

获得旋转度后,将其应用于布局


关于使用多点触摸旋转视图,有一个不错的问题

您发布的代码与您的问题并不相关,您自己似乎也没有尝试解决这个问题。堆栈溢出上的人员不会为您编写代码,他们会帮助您修复代码。@Tibrogargan该代码与如何使用触摸旋转系统覆盖项的问题极为相关。Stack Overflow用于提问,这些问题可以用简单的方法回答,比如从哪里开始或从哪里获取更多信息。不要让它成为一个充满敌意的环境。