我想创建一个有手势覆盖的e视图,这个视图可以在应用程序之外的任何地方调用。

时间:2016-10-12 12:45:33

标签: android service android-view

我创建了浮在屏幕上的聊天,但是如何从中调用视图。一直在寻找这个。 want to make a view like this

这是我一直在努力做的观点。它有手势覆盖Gesture Magic此应用正在使用此视图

2 个答案:

答案 0 :(得分:1)

概念很简单,您基本上需要创建一个服务,通过将视图添加/删除到窗口管理器来动态管理视图。

public class StandOutViewService extends Service {

    private WindowManager mWindowManager;
    private View mStandOutView;

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

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

        mStandOutView = new View(this);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 100;
        params.y = 500;

        mWindowManager.addView(mStandOutView, params);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

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

}

您可以查看此库以获得一个起点: https://github.com/pingpongboss/StandOut

请注意,您需要使用以下方式处理权限:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

对于Android 6及更高版本,您可能需要指示您的用户通过辅助功能设置菜单激活您的服务。

答案 1 :(得分:0)

我找到了答案并想与大家分享希望它会有所帮助

        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            900,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,


            PixelFormat.TRANSLUCENT);
    params.x=0;
    if (FloatDirections.y>700){
    params.y=FloatDirections.y-700;}
    else {
        params.y=0;
    }
    params.gravity= Gravity.TOP|Gravity.CENTER;
    serviceRunning=true;
    Toast.makeText(getApplicationContext(),String.valueOf(FloatDirections.x)+"  "+String.valueOf(FloatDirections.y),Toast.LENGTH_SHORT).show();
    wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    myView = inflater.inflate(R.layout.gesture_recognition, null);
    b11=(Button)myView.findViewById(R.id.newbtn);
    GestureOverlayView ov = (GestureOverlayView)myView.findViewById(R.id.gestures_overlay_float);
    ov.addOnGesturePerformedListener(this);

然后使用OnGesturePerformed

   @Override
public void onGesturePerformed(GestureOverlayView gestureOverlayView, Gesture gesture) {
  //  Toast.makeText(getApplicationContext(),"hi",Toast.LENGTH_SHORT).show();
    mydatabase.execSQL("CREATE TABLE IF NOT EXISTS PackageName(GestureName VARCHAR,PackName VARCHAR,Action VARCHAR);");
    cursor=mydatabase.rawQuery("select * from PackageName",null);

    ArrayList<Prediction> predictions = store.recognize(gesture);