Android自定义底部显示对话框

1、定义style
<style name="myDialog" parent="android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <!-- 边框 -->
    <item name="android:windowIsFloating">true</item>
    <!-- 是否浮现在activity之上 -->
    <item name="android:windowIsTranslucent">true</item>
    <!-- 半透明 -->
    <item name="android:windowNoTitle">true</item>
    <!-- 无标题 -->
    <item name="android:windowBackground">@android:color/transparent</item>
    <!-- 背景透明 -->
    <item name="android:backgroundDimEnabled">true</item>
    <!-- 模糊 -->
</style>

2、自定义Dialog
/**
 * 拍照的dialog
 */
public class ImageChooseDialog extends Dialog implements OnClickListener {
    private Button choose_album;
    private Button choose_cam;
    private Button choose_cancel;
    public final static int PHOTO_SELETED = 3012;
    public final static int PHOTO_SCROP = 3013;


    public ImageChooseDialog(Context context) {
        super(context, R.style.myDialog);
        this.setCancelable(true);
        setCustomView();
    }

    public ImageChooseDialog(Context context, boolean cancelable) {
        super(context, R.style.myDialog);
        this.setCancelable(cancelable);
        setCustomView();
    }

    public ImageChooseDialog(Context context, int theme) {
        super(context, R.style.myDialog);
        setCustomView();
    }

    /**
     * 设置整个弹出框的视图
     */
    private void setCustomView() {
        View mView = LayoutInflater.from(getContext()).inflate(
                R.layout.choose_avatar, null);
        choose_album = (Button) mView.findViewById(R.id.choose_album);
        choose_cam = (Button) mView.findViewById(R.id.choose_cam);
        choose_cancel = (Button) mView.findViewById(R.id.choose_cancel);
        choose_cancel.setOnClickListener(this);
        choose_album.setOnClickListener(this);
        choose_cam.setOnClickListener(this);

        super.setContentView(mView);
    }


    @Override
    public void setContentView(View view) {
        // 重写本方法,使外部调用时不可破坏控件的视图。
        // 也可以使用本方法改变CustomDialog的内容部分视图,比如让用户把内容视图变成复选框列表,图片等。这需要获取mView视图里的其它控件
    }


    /**
     * 确定键监听器
     */
    public void setChooseAlbumListener(View.OnClickListener listener) {
        choose_album.setOnClickListener(listener);
    }

    /**
     * 取消键监听器
     */
    public void setChooseCamareListener(View.OnClickListener listener) {
        choose_cam.setOnClickListener(listener);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.choose_cancel:
                dismiss();
                break;
            case R.id.choose_album:

                dismiss();
                break;
            case R.id.choose_cam:

                dismiss();
                break;
            default:
                break;
        }
    }


    /**
     * @param activity 打开相机
     */
    public static void openCamare(Activity activity) {
        if (!SDCardFileUtils.avaiableSDCard()) {
            AndroidUtils.custToast(activity, "没有检测到存储卡!");
            return;
        }
        Uri imageUri = null;
        String fileName = null;
        Intent openCameraIntent = new Intent(
                MediaStore.ACTION_IMAGE_CAPTURE);
        // 删除上一次截图的临时文件
        SharedPreferences sharedPreferences = activity.getSharedPreferences("temp",
                Context.MODE_PRIVATE);
        SDCardFileUtils.deletePhotoAtPathAndName(Environment
                        .getExternalStorageDirectory().getAbsolutePath(),
                sharedPreferences.getString("tempName", ""));

        // 保存本次截图临时文件名字
        fileName = String.valueOf(System.currentTimeMillis()) + ".jpg";
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("tempName", fileName);
        editor.commit();
        imageUri = Uri.fromFile(new File(Environment
                .getExternalStorageDirectory(), fileName));
        // 指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换
        openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        activity.startActivityForResult(openCameraIntent, PHOTO_SELETED);
    }

    /**
     * @param activity 打开图库
     */
    public static void openAlbum(Activity activity) {
        if (!SDCardFileUtils.avaiableSDCard()) {
            AndroidUtils.custToast(activity, "没有检测到存储卡!");
            return;
        }
        Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT);
        openAlbumIntent.setDataAndType(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        activity.startActivityForResult(openAlbumIntent, PHOTO_SELETED);
    }

    /**
     * 裁剪图片
     *
     * @param activity activity
     * @param uri      地址
     * @param outputX  宽度像素
     * @param outputY  高度像素
     */
    public static void cropImage(Activity activity, Uri uri, int outputX, int outputY) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            String url = SDCardFileUtils.getPath(activity, uri);
            intent.setDataAndType(Uri.fromFile(new File(url)), "image/*");
        } else {
            intent.setDataAndType(uri, "image/*");
        }
        // croptrue是设置在开启的intent中设置显示的view可以剪裁
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX,outputY 是剪裁图片的宽高
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);

        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true);
        intent.putExtra("return-data", true);
        activity.startActivityForResult(intent, PHOTO_SCROP);
    }


}
3、自定义XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:minWidth="1000dp"
              android:orientation="vertical">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp">

        <Button
            android:id="@+id/choose_album"
            android:layout_width="fill_parent"
            android:layout_height="45dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/btn_login_bg"
            android:textColor="@color/white"
            android:text="本地相册"
            android:padding="5dp"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/choose_cam"
            android:layout_width="fill_parent"
            android:layout_height="45dp"
            android:layout_marginTop="5dp"
            android:padding="5dp"
            android:text="相机拍照"
            android:background="@drawable/btn_login_bg"
            android:textColor="@color/white"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/choose_cancel"
            android:layout_width="fill_parent"
            android:layout_height="45dp"
            android:padding="5dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/btn_grey"
            android:textColor="@color/white"
            android:text="取消"
            android:textSize="20sp"/>
    </LinearLayout>

</LinearLayout>
4、调用对话框

/**
 * 显示弹窗的dialog
 */
public void showImageDialog() {

    if (chooseDialog == null) {
        chooseDialog = new ImageChooseDialog(context);
        chooseDialog.setChooseAlbumListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseDialog.dismiss();
                ImageChooseDialog.openAlbum(CheckInActivity.this);
            }
        });
        chooseDialog.setChooseCamareListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseDialog.dismiss();
                ImageChooseDialog.openCamare(CheckInActivity.this);

            }
        });
        chooseDialog.show();
        Window dialogWindow = chooseDialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
    } else {
        if (!chooseDialog.isShowing()) {
            chooseDialog.show();
        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值