【问题标题】:How to rotate a System Overlay Button如何旋转系统覆盖按钮
【发布时间】:2016-08-17 21:05:47
【问题描述】:

我在互联网上找到了这段代码,它可以工作。

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

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

也许是两指旋转。

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();

}}

【问题讨论】:

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

标签: android system overlay


【解决方案1】:

旋转系统叠加层与旋转视图没有什么不同。您将从拦截触摸开始,然后将其转换为视图旋转。

要实现两指触摸旋转,您需要获取移动幅度,然后将其转换为应旋转视图的度数。

一旦你有了旋转度数,你就可以将它应用到布局中。

rotation and scaling and move using multi touch in androidrotation and scaling and move using multi touch in android 有一个关于使用多点触控旋转视图的好问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多