项目知识——创建悬浮框

1、AlertDialog直接创建

2、设置自定义样式的AlertDialog

3、自定义AlertDialog

4、了解WindowManager

5、通过WindowManager创建悬浮框

 

一、创建AlertDialog

详见AlertDialog与DialogFragment

二、自定义AlertDialog

但是自带的Dialog太low了,我想完成实现Dialog的enter、exit动画,修改Dialog的样式

1、实现enter、exit动画(底部向上浮出)

http://blog.csdn.net/centralperk/article/details/7494441

知识点:1、管理Dialog的还是Window,所以只要获取window就可以在Dialog上操作了

2、修改Dialog样式

http://blog.csdn.net/lieri111/article/details/6326332

三、自定义AlertDialog

我的自定义的layout只能在一块区域显示,根据我的分析,利用AlertDialog.Builder()创建的Dialog之前已经限定了layout,我自己的layout不过是添加进去的而已。

所以把自己的layout变成主layout,就创建一个ImageDialog类

public class ImageDialog extends AlertDialog {
    public static final int REQUST_PHOTO = 0;
    public static final int REQUEST_GALLERY = 2;
    public static final String INTENT_PATH = "ImagePath";
    private Button mBtnChoosePicture;
    private Button mBtnTakePhoto;
    private Context mContext;
    private File mStorageDir;
    public static Uri uri;

    public ImageDialog(Context context) {
        super(context);
        mContext = context;
    }

    public ImageDialog(Context context, int themeResId) {
        super(context, themeResId);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_choose_image);
        initView();
        initMonitor();
    }

    private void initView(){
        mBtnTakePhoto = (Button) findViewById(R.id.chooseImage_btn_photo);
        mBtnChoosePicture = (Button)findViewById(R.id.chooseImage_btn_picture);
        mStorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        uri = Uri.fromFile(new File(mStorageDir.getPath()+System.currentTimeMillis()+".png"));

    }

    private void initMonitor(){
        mBtnTakePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
                ((Activity)mContext).startActivityForResult(intent,REQUST_PHOTO);
            }
        });
        mBtnChoosePicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);

                intent.setType("image/*");

                intent.putExtra("crop", "true");

                intent.putExtra("aspectX", 2);

                intent.putExtra("aspectY", 1);

                intent.putExtra("outputX", 600);

                intent.putExtra("outputY", 300);

                intent.putExtra("scale", true);

                intent.putExtra("return-data", false);

                intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

                intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());

                intent.putExtra("noFaceDetection", true); // no face detection

                ((Activity)mContext).startActivityForResult(intent, REQUEST_GALLERY);
            }
        });
    }
}
ImageDialog

创建步骤:1、继承AlertDialog   2、重写setContentView() (跟Acitivity的创建方法是一样的)  

注:发现在Dialog没有startActivityResult()方法,方法是用((Activity)mContext).startActivityResult(),将构造方法中的context向上转型为Activity。

千万不能用getContext()获取,因为getContext()中没有token,就无法判定是哪个Activity,所以也就无法使用result()方法(Android开发艺术探索P307)

四、WindowManager

http://www.cnblogs.com/mengdd/p/3824782.html

自己写的Demo:

public class WindowDialog {
    private WindowManager windowManager;
    private View v;
    private boolean isShow = false;
    public void showDialog(Context context){
        if(isShow){
            return;
        }
        isShow = true;
        //除Activity之外获取WindowManager的方式  关于配置详情看  Android开发艺术探索P295
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        lp.format = PixelFormat.TRANSLUCENT;
        v = createView(context);
        windowManager.addView(v,lp);

    }
    //初始化View
    private View createView(Context context){

        View v = LayoutInflater.from(context).inflate(R.layout.dialog_window,null);
        final View unTransparent = v.findViewById(R.id.window_linear);
        //点击事件:当在非透明区域外的时候,退出Dialog
        v.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int x = (int)event.getX();
                int y = (int)event.getY();
                Rect rect = new Rect();
                unTransparent.getGlobalVisibleRect(rect);
                if (!rect.contains(x,y)){
                    hiddenWindow();
                }
                return false;
            }
        });
        //当物理键盘点击返回的时候退出
        v.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                switch (event.getAction()){
                    case KeyEvent.KEYCODE_BACK:
                        hiddenWindow();
                        break;
                }
                return false;
            }
        });
        return v;
    }

    public void hiddenWindow(){
        if(isShow){
            windowManager.removeView(v);
            isShow = false;
        }
    }
}
View Code

 

posted @ 2016-05-28 14:16  技术丶从积累开始  阅读(200)  评论(0编辑  收藏  举报