1. 程式人生 > >彈框封裝

彈框封裝

場景

選圖、拍照、取消。

封裝

PicChoiceDialog

package widget.dialog.three;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xalikai.bnmdteacherend.R;

/**
 * 照片選擇彈框
 *
 * @date 2018/12/10
 */
public class PicChoiceDialog extends Dialog {
    private ClickListenerInterface clickListenerInterface;

    public PicChoiceDialog(Context context) {
        super(context, R.style.PicChoiceDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        setContentView(R.layout.dialog_pic_choice);
        LinearLayout camera = findViewById(R.id.camera);
        LinearLayout gallery = findViewById(R.id.gallery);
        TextView cancel = findViewById(R.id.cancel);
        camera.setOnClickListener(new ClickListener());
        gallery.setOnClickListener(new ClickListener());
        cancel.setOnClickListener(new ClickListener());
        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        assert window != null;
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setGravity(Gravity.BOTTOM);
        window.setAttributes(attributes);
    }

    public void setClickListener(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }

    private class ClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.camera:
                    clickListenerInterface.showCamera();
                    break;
                case R.id.gallery:
                    clickListenerInterface.showGallery();
                    break;
                case R.id.cancel:
                    dismiss();
                    break;
                default:
                    break;
            }
        }
    }

    public interface ClickListenerInterface {
        /**
         * 相機
         */
        void showCamera();

        /**
         * 相簿
         */
        void showGallery();
    }
}

styles

<style name="PicChoiceDialog" parent="@android:style/Theme.Dialog">
        <!--邊框-->
        <item name="android:windowFrame">@null</item>
        <!--浮現於Activity上否-->
        <item name="android:windowIsFloating">true</item>
        <!--半透明-->
        <item name="android:windowIsTranslucent">true</item>
        <!--無標題-->
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">@android:color/transparent</item>
        <!--背景透明-->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!--模糊-->
        <item name="android:backgroundDimEnabled">true</item>
        <!--遮罩層-->
        <item name="android:backgroundDimAmount">0.5</item>
        <item name="android:windowAnimationStyle">@style/pic_choice_dialog_anim</item>
    </style>

    <style name="pic_choice_dialog_anim" mce_bogus="1" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/slide_in_bottom</item>
        <item name="android:windowExitAnimation">@anim/slide_out_bottom</item>
    </style>

slide_in_bottom

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="100"
        android:fromYDelta="100%"
        android:toYDelta="0" />
</set>

slide_out_bottom

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="100"
        android:fromYDelta="0"
        android:toYDelta="100%" />
</set>

佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/transparent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/camera"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_50"
        android:background="@color/background"
        android:gravity="center"
        tools:ignore="UseCompoundDrawables">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/camera"
            tools:ignore="ContentDescription" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/dp_12"
            android:gravity="center"
            android:text="@string/camera"
            android:textColor="@color/fontInput"
            android:textSize="@dimen/sp_15"
            tools:ignore="HardcodedText,RtlHardcoded" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/px1"
        android:background="@color/line_color" />

    <LinearLayout
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_50"
        android:background="@color/background"
        android:gravity="center"
        tools:ignore="UseCompoundDrawables">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/gallery"
            tools:ignore="ContentDescription" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/dp_12"
            android:gravity="center"
            android:text="@string/gallery"
            android:textColor="@color/fontInput"
            android:textSize="@dimen/sp_15"
            tools:ignore="HardcodedText,RtlHardcoded" />
    </LinearLayout>

    <TextView
        android:id="@+id/cancel"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_50"
        android:layout_marginTop="@dimen/dp_20"
        android:background="@color/background"
        android:gravity="center"
        android:text="@string/cancel"
        android:textColor="@color/fontInput"
        android:textSize="@dimen/sp_15"
        tools:ignore="HardcodedText,RtlHardcoded" />
</LinearLayout>

使用

package module.personalcenter.activity;

...

/**
 * 個人資訊
 *
 * @date 2018/10/31
 */
public class PersonalInfoActivity extends BaseActivity implements PicChoiceDialog.ClickListenerInterface {
    
    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_personal_info);
        ButterKnife.bind(this);
    }

    @Override
    protected void stepUI() {
        
    }

    @OnClick({R.id.personalInfoRlSelectPic})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.personalInfoRlSelectPic:
                PicChoiceDialog picChoiceDialog = new PicChoiceDialog(this);
                picChoiceDialog.setClickListener(this);
                picChoiceDialog.show();
                break;
            default:
                break;
        }
    }

    @Override
    protected void initConfiguration() {
      
    }

    @Override
    protected void initData() {
        
    }

    @Override
    protected void startLogic() {

    }

    @Override
    protected void setListener() {

    }

    /**
     * 顯示相機
     */
    @Override
    public void showCamera() {
        Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        String f = System.currentTimeMillis() + ".jpg";
        Uri fileUri = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory("").getPath() + f));
        openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        int requestCodeTakePicture = 1001;
        startActivityForResult(openCameraIntent, requestCodeTakePicture);
    }

    /**
     * 顯示相簿
     */
    @Override
    public void showGallery() {
        Intent mOpenGalleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
        mOpenGalleryIntent.setType("image/*");
        int requestSelectPic = 1002;
        startActivityForResult(mOpenGalleryIntent, requestSelectPic);
    }
}