Window基础知识图解与demo

Window基础知识图解

Window基础知识图解与demo_第1张图片

Demo实例1:可以移动的悬浮窗

package com.pang.window;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;

public class MainActivity extends Activity {

    protected boolean first = true;
    Button vFloatingButton;
    LayoutParams vLayoutParams;
    WindowManager vWindowManager;
    int rawX;
    int rawY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window=getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        vWindowManager = getWindowManager();
        vFloatingButton =new Button(this);
        vFloatingButton.setText("浮动窗");

        vLayoutParams = new WindowManager.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
                0,0,PixelFormat.TRANSPARENT);
        vLayoutParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL;
        //x,y指的是距离屏幕中心的距离,x=100,从屏幕中心开始下移100px,y=100,从屏幕中心开始左移100px
        vLayoutParams.x=100;
        vLayoutParams.y=100;


        vFloatingButton.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int viewX = (int)event.getRawX();
                int viewY = (int)event.getRawY();


                if(first){
                    //获取屏幕中心的位置
                    rawX= (int)event.getRawX()-vFloatingButton.getWidth()/2;
                    rawY = (int)event.getRawY()-vFloatingButton.getHeight()/2;
                    first = false;
                }

                switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    //坐标系换算,不会的baidu-.-
                    vLayoutParams.x=viewX-rawX;
                    vLayoutParams.y=viewY-rawY;
                    vWindowManager.updateViewLayout(v, vLayoutParams);

                    break;


                default:
                    break;
                }
                return false;
            }
        });

        vWindowManager.addView(vFloatingButton, vLayoutParams);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

伸手党点在论坛搜:可以移动的悬浮窗(PS:要一个积分)

你可能感兴趣的:(我的学习之路)