diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7e5e7dcf..8a07bddf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -29,13 +29,12 @@ - @@ -62,6 +61,7 @@ + MainCustomCameraViewActivity.newInstance(MainListActivity.this)); - + // 自定义CameraLayout + mBinding.btnCustomCameraLayout.setOnClickListener(v -> MainCustomCameraLayoutActivity.newInstance(MainListActivity.this)); } } diff --git a/app/src/main/java/com/zhongjh/cameraapp/phone/customlayout/MainCustomCameraLayoutActivity.java b/app/src/main/java/com/zhongjh/cameraapp/phone/customlayout/MainCustomCameraLayoutActivity.java new file mode 100644 index 00000000..b0f25411 --- /dev/null +++ b/app/src/main/java/com/zhongjh/cameraapp/phone/customlayout/MainCustomCameraLayoutActivity.java @@ -0,0 +1,187 @@ +package com.zhongjh.cameraapp.phone.customlayout; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.view.View; + +import androidx.databinding.DataBindingUtil; + +import com.zhongjh.albumcamerarecorder.album.filter.BaseFilter; +import com.zhongjh.albumcamerarecorder.settings.AlbumSetting; +import com.zhongjh.albumcamerarecorder.settings.CameraSetting; +import com.zhongjh.albumcamerarecorder.settings.GlobalSetting; +import com.zhongjh.albumcamerarecorder.settings.MultiMediaSetting; +import com.zhongjh.albumcamerarecorder.settings.RecorderSetting; +import com.zhongjh.cameraapp.BaseActivity; +import com.zhongjh.cameraapp.R; +import com.zhongjh.cameraapp.configuration.GifSizeFilter; +import com.zhongjh.cameraapp.configuration.Glide4Engine; +import com.zhongjh.cameraapp.databinding.ActivityMainCustomCameralayoutBinding; +import com.zhongjh.cameraapp.phone.customlayout.camera1.CameraFragment1; +import com.zhongjh.cameraapp.phone.customlayout.camera2.CameraFragment2; +import com.zhongjh.common.entity.SaveStrategy; +import com.zhongjh.common.enums.MimeType; +import com.zhongjh.progresslibrary.entity.MultiMediaView; +import com.zhongjh.progresslibrary.listener.MaskProgressLayoutListener; +import com.zhongjh.progresslibrary.widget.MaskProgressLayout; + +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +/** + * 可以自己完完全全自定义录制的布局、逻辑 + * 当然,随着更深入的逻辑修改,需要了解更多的类 + * 如果对于该自定义布局有更好的建议、优化请加QQ群讨论915053430 + * + * @author zhongjh + * @date 2022/8/24 + */ +public class MainCustomCameraLayoutActivity extends BaseActivity { + + ActivityMainCustomCameralayoutBinding mBinding; + private final String TAG = MainCustomCameraLayoutActivity.this.getClass().getSimpleName(); + + GlobalSetting mGlobalSetting; + CameraSetting cameraSetting; + + /** + * @param activity 要跳转的activity + */ + public static void newInstance(Activity activity) { + activity.startActivity(new Intent(activity, MainCustomCameraLayoutActivity.class)); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main_custom_cameralayout); + mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main_custom_cameralayout); + + // 以下为点击事件 + mBinding.mplImageList.setMaskProgressLayoutListener(new MaskProgressLayoutListener() { + + @Override + public void onAddDataSuccess(@NotNull List multiMediaViews) { + } + + @Override + public void onItemAdd(@NotNull View view, @NotNull MultiMediaView multiMediaView, int alreadyImageCount, int alreadyVideoCount, int alreadyAudioCount) { + // 点击添加 + boolean isOk = getPermissions(false); + if (isOk) { + openMain(alreadyImageCount, alreadyVideoCount, alreadyAudioCount); + } + } + + @Override + public void onItemClick(@NotNull View view, @NotNull MultiMediaView multiMediaView) { + // 点击详情 + if (multiMediaView.isImageOrGif() || multiMediaView.isVideo()) { + mGlobalSetting.openPreviewData(MainCustomCameraLayoutActivity.this, REQUEST_CODE_CHOOSE, + mBinding.mplImageList.getImagesAndVideos(), + mBinding.mplImageList.getImagesAndVideos().indexOf(multiMediaView)); + } + } + + @Override + public void onItemStartUploading(@NotNull MultiMediaView multiMediaView) { + // 开始模拟上传 - 指刚添加后的。这里可以使用你自己的上传事件 + MyTask timer = new MyTask(multiMediaView); + timers.put(multiMediaView, timer); + timer.schedule(); + } + + @Override + public void onItemClose(@NotNull View view, @NotNull MultiMediaView multiMediaView) { + // 停止上传 + timers.get(multiMediaView).cancel(); + timers.remove(multiMediaView); + } + + @Override + public void onItemAudioStartDownload(@NotNull View view, @NotNull String url) { + + } + + @Override + public boolean onItemVideoStartDownload(@NotNull View view, @NotNull MultiMediaView multiMediaView) { + return false; + } + + }); + + + } + + @Override + protected void onDestroy() { + super.onDestroy(); + if (mGlobalSetting != null) { + mGlobalSetting.onDestroy(); + } + } + + @Override + protected MaskProgressLayout getMaskProgressLayout() { + return mBinding.mplImageList; + } + + @Override + protected void openMain(int alreadyImageCount, int alreadyVideoCount, int alreadyAudioCount) { + // 拍摄有关设置 + cameraSetting = new CameraSetting(); + // 支持的类型:图片,视频 + cameraSetting.mimeTypeSet(MimeType.ofAll()); + + // 每次使用要重新赋值,因为会在每次关闭界面后删除该Fragment + if (mBinding.radioButton1.isChecked()) { + cameraSetting.setBaseCameraFragment(CameraFragment1.newInstance()); + } else if(mBinding.radioButton2.isChecked()) { + cameraSetting.setBaseCameraFragment(CameraFragment2.newInstance()); + } + + // 相册 + AlbumSetting albumSetting = new AlbumSetting(false) + // 支持的类型:图片,视频 + .mimeTypeSet(MimeType.ofAll()) + // 是否显示多选图片的数字 + .countable(true) + // 自定义过滤器 + .addFilter(new GifSizeFilter(320, 320, 5 * BaseFilter.K * BaseFilter.K)) + // 开启原图 + .originalEnable(true) + // 最大原图size,仅当originalEnable为true的时候才有效 + .maxOriginalSize(10); + + // 录音机 + RecorderSetting recorderSetting = new RecorderSetting(); + + // 全局 + mGlobalSetting = MultiMediaSetting.from(MainCustomCameraLayoutActivity.this).choose(MimeType.ofAll()); + + // 开启相册功能 + mGlobalSetting.albumSetting(albumSetting); + // 开启拍摄功能 + mGlobalSetting.cameraSetting(cameraSetting); + // 开启录音功能 + mGlobalSetting.recorderSetting(recorderSetting); + + mGlobalSetting + // 设置路径和7.0保护路径等等 + .allStrategy(new SaveStrategy(true, "com.zhongjh.cameraapp.fileprovider", "aabb")) + // for glide-V4 + .imageEngine(new Glide4Engine()) + // 最大5张图片、最大3个视频、最大1个音频 + .maxSelectablePerMediaType(null, + 5, + 3, + 3, + alreadyImageCount, + alreadyVideoCount, + alreadyAudioCount) + .forResult(REQUEST_CODE_CHOOSE); + } + +} diff --git a/app/src/main/java/com/zhongjh/cameraapp/phone/customlayout/camera1/CameraFragment1.java b/app/src/main/java/com/zhongjh/cameraapp/phone/customlayout/camera1/CameraFragment1.java new file mode 100644 index 00000000..8de85d34 --- /dev/null +++ b/app/src/main/java/com/zhongjh/cameraapp/phone/customlayout/camera1/CameraFragment1.java @@ -0,0 +1,145 @@ +package com.zhongjh.cameraapp.phone.customlayout.camera1; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.databinding.DataBindingUtil; +import androidx.recyclerview.widget.RecyclerView; + +import com.otaliastudios.cameraview.CameraView; +import com.zhongjh.albumcamerarecorder.camera.ui.camera.BaseCameraFragment; +import com.zhongjh.albumcamerarecorder.camera.ui.camera.presenter.BaseCameraPicturePresenter; +import com.zhongjh.albumcamerarecorder.camera.ui.camera.presenter.BaseCameraVideoPresenter; +import com.zhongjh.albumcamerarecorder.camera.ui.camera.state.CameraStateManagement; +import com.zhongjh.albumcamerarecorder.camera.widget.PhotoVideoLayout; +import com.zhongjh.albumcamerarecorder.widget.childclickable.IChildClickableLayout; +import com.zhongjh.cameraapp.R; +import com.zhongjh.cameraapp.databinding.FragmentCamera1Binding; + +/** + * 继承于BaseCameraFragment + * 1. setContentView 实现自己想要的布局 + * 2. 实现相关view + * 3. 使用默认的 BaseCameraPicturePresenter、BaseCameraVideoPresenter、CameraStateManagement + *

+ * 该Fragment的例子让我们增加几个view,添加我们想要的事件。 + * 使用 TODO 关键字可搜索相关自定义代码 + * + * @author zhongjh + * @date 2022/8/12 + */ +public class CameraFragment1 extends BaseCameraFragment { + + FragmentCamera1Binding mBinding; + BaseCameraPicturePresenter cameraPicturePresenter = new BaseCameraPicturePresenter(this); + BaseCameraVideoPresenter cameraVideoPresenter = new BaseCameraVideoPresenter(this); + CameraStateManagement cameraStateManagement = new CameraStateManagement(this); + + public static CameraFragment1 newInstance() { + return new CameraFragment1(); + } + + @Override + public View setContentView(LayoutInflater inflater, ViewGroup container) { + mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_camera1,container,false); + return mBinding.getRoot(); + } + + @Override + public void initView(View view, Bundle savedInstanceState) { + } + + /** + * TODO + * 覆写该事件,赋值自定义按钮事件 + */ + @Override + protected void initListener() { + super.initListener(); + mBinding.btnCustom.setOnClickListener(v -> Toast.makeText(getMyContext(), "我是自定义的", Toast.LENGTH_SHORT).show()); + } + + @NonNull + @Override + public IChildClickableLayout getChildClickableLayout() { + return mBinding.rlMain; + } + + @Nullable + @Override + public View getTopView() { + return mBinding.clMenu; + } + + @NonNull + @Override + public CameraView getCameraView() { + return mBinding.cameraView; + } + + @Override + public RecyclerView getRecyclerViewPhoto() { + return mBinding.rlPhoto; + } + + @Nullable + @Override + public View[] getMultiplePhotoView() { + return new View[]{mBinding.vLine1, mBinding.vLine2}; + } + + @NonNull + @Override + public PhotoVideoLayout getPhotoVideoLayout() { + return mBinding.pvLayout; + } + + @NonNull + @Override + public com.zhongjh.albumcamerarecorder.widget.ImageViewTouch getSinglePhotoView() { + return mBinding.imgPhoto; + } + + @Nullable + @Override + public View getCloseView() { + return mBinding.imgClose; + } + + @Nullable + @Override + public ImageView getFlashView() { + return mBinding.imgFlash; + } + + @Nullable + @Override + public ImageView getSwitchView() { + return mBinding.imgSwitch; + } + + @NonNull + @Override + public CameraStateManagement getCameraStateManagement() { + return cameraStateManagement; + } + + @NonNull + @Override + public BaseCameraPicturePresenter getCameraPicturePresenter() { + return cameraPicturePresenter; + } + + @NonNull + @Override + public BaseCameraVideoPresenter getCameraVideoPresenter() { + return cameraVideoPresenter; + } + +} diff --git a/app/src/main/java/com/zhongjh/cameraapp/phone/customlayout/camera2/CameraFragment2.java b/app/src/main/java/com/zhongjh/cameraapp/phone/customlayout/camera2/CameraFragment2.java new file mode 100644 index 00000000..61fe9292 --- /dev/null +++ b/app/src/main/java/com/zhongjh/cameraapp/phone/customlayout/camera2/CameraFragment2.java @@ -0,0 +1,165 @@ +package com.zhongjh.cameraapp.phone.customlayout.camera2; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.widget.AppCompatButton; +import androidx.constraintlayout.widget.ConstraintLayout; +import androidx.recyclerview.widget.RecyclerView; + +import com.otaliastudios.cameraview.CameraView; +import com.zhongjh.albumcamerarecorder.camera.ui.camera.BaseCameraFragment; +import com.zhongjh.albumcamerarecorder.camera.ui.camera.presenter.BaseCameraPicturePresenter; +import com.zhongjh.albumcamerarecorder.camera.ui.camera.presenter.BaseCameraVideoPresenter; +import com.zhongjh.albumcamerarecorder.camera.ui.camera.state.CameraStateManagement; +import com.zhongjh.albumcamerarecorder.camera.widget.PhotoVideoLayout; +import com.zhongjh.albumcamerarecorder.widget.childclickable.ChildClickableRelativeLayout; +import com.zhongjh.albumcamerarecorder.widget.childclickable.IChildClickableLayout; +import com.zhongjh.cameraapp.R; + +/** + * 继承于BaseCameraFragment + * 主要演示 BaseCameraPicturePresenter + *

+ * 使用 TODO 关键字可搜索相关自定义代码 + * + * @author zhongjh + * @date 2022/8/12 + */ +public class CameraFragment2 extends BaseCameraFragment { + + ViewHolder mViewHolder; + CameraPicturePresenter cameraPicturePresenter = new CameraPicturePresenter(this); + BaseCameraVideoPresenter cameraVideoPresenter = new BaseCameraVideoPresenter(this); + CameraStateManagement cameraStateManagement = new CameraStateManagement(this); + + public static CameraFragment2 newInstance() { + return new CameraFragment2(); + } + + @Override + public View setContentView(LayoutInflater inflater, ViewGroup container) { + return inflater.inflate(R.layout.fragment_camera_zjh, container, false); + } + + @Override + public void initView(View view, Bundle savedInstanceState) { + mViewHolder = new ViewHolder(view); + } + + @NonNull + @Override + public IChildClickableLayout getChildClickableLayout() { + return mViewHolder.rlMain; + } + + @Nullable + @Override + public View getTopView() { + return mViewHolder.clMenu; + } + + @NonNull + @Override + public CameraView getCameraView() { + return mViewHolder.cameraView; + } + + @Override + public RecyclerView getRecyclerViewPhoto() { + return mViewHolder.rlPhoto; + } + + @Nullable + @Override + public View[] getMultiplePhotoView() { + return new View[]{mViewHolder.vLine1, mViewHolder.vLine2}; + } + + @NonNull + @Override + public PhotoVideoLayout getPhotoVideoLayout() { + return mViewHolder.pvLayout; + } + + @NonNull + @Override + public com.zhongjh.albumcamerarecorder.widget.ImageViewTouch getSinglePhotoView() { + return mViewHolder.imgPhoto; + } + + @Nullable + @Override + public View getCloseView() { + return mViewHolder.imgClose; + } + + @Nullable + @Override + public ImageView getFlashView() { + return mViewHolder.imgFlash; + } + + @Nullable + @Override + public ImageView getSwitchView() { + return mViewHolder.imgSwitch; + } + + @NonNull + @Override + public CameraStateManagement getCameraStateManagement() { + return cameraStateManagement; + } + + @NonNull + @Override + public BaseCameraPicturePresenter getCameraPicturePresenter() { + return cameraPicturePresenter; + } + + @NonNull + @Override + public BaseCameraVideoPresenter getCameraVideoPresenter() { + return cameraVideoPresenter; + } + + public static class ViewHolder { + + View rootView; + ChildClickableRelativeLayout rlMain; + com.zhongjh.albumcamerarecorder.widget.ImageViewTouch imgPhoto; + ImageView imgFlash; + ImageView imgSwitch; + PhotoVideoLayout pvLayout; + RecyclerView rlPhoto; + View vLine1; + View vLine2; + ImageView imgClose; + CameraView cameraView; + ConstraintLayout clMenu; + AppCompatButton btnCustom; + + ViewHolder(View rootView) { + this.rootView = rootView; + this.rlMain = rootView.findViewById(R.id.rlMain); + this.imgPhoto = rootView.findViewById(R.id.imgPhoto); + this.imgFlash = rootView.findViewById(R.id.imgFlash); + this.imgSwitch = rootView.findViewById(R.id.imgSwitch); + this.pvLayout = rootView.findViewById(R.id.pvLayout); + this.rlPhoto = rootView.findViewById(R.id.rlPhoto); + this.vLine1 = rootView.findViewById(R.id.vLine1); + this.vLine2 = rootView.findViewById(R.id.vLine2); + this.imgClose = rootView.findViewById(R.id.imgClose); + this.cameraView = rootView.findViewById(R.id.cameraView); + this.clMenu = rootView.findViewById(R.id.clMenu); + this.btnCustom = rootView.findViewById(R.id.btnCustom); + } + } + +} diff --git a/app/src/main/java/com/zhongjh/cameraapp/phone/customlayout/camera2/CameraPicturePresenter.java b/app/src/main/java/com/zhongjh/cameraapp/phone/customlayout/camera2/CameraPicturePresenter.java new file mode 100644 index 00000000..6adddd91 --- /dev/null +++ b/app/src/main/java/com/zhongjh/cameraapp/phone/customlayout/camera2/CameraPicturePresenter.java @@ -0,0 +1,28 @@ +package com.zhongjh.cameraapp.phone.customlayout.camera2; + +import android.widget.Toast; + +import com.zhongjh.albumcamerarecorder.camera.ui.camera.BaseCameraFragment; +import com.zhongjh.albumcamerarecorder.camera.ui.camera.presenter.BaseCameraPicturePresenter; +import com.zhongjh.albumcamerarecorder.camera.ui.camera.presenter.BaseCameraVideoPresenter; +import com.zhongjh.albumcamerarecorder.camera.ui.camera.state.CameraStateManagement; + +/** + * 主要演示 BaseCameraPicturePresenter + * 可以看父类能提供什么方法给你覆写,更详细的注释等请看父类 + * + * @author zhongjh + * @date 2022/8/25 + */ +public class CameraPicturePresenter extends BaseCameraPicturePresenter { + + public CameraPicturePresenter(BaseCameraFragment baseCameraFragment) { + super(baseCameraFragment); + } + + @Override + public void takePhoto() { + super.takePhoto(); + Toast.makeText(baseCameraFragment.getMyContext(), "拍照时触发自定义事件!", Toast.LENGTH_SHORT).show(); + } +} diff --git a/app/src/main/res/layout/activity_main_custom_cameralayout.xml b/app/src/main/res/layout/activity_main_custom_cameralayout.xml new file mode 100644 index 00000000..c041081d --- /dev/null +++ b/app/src/main/res/layout/activity_main_custom_cameralayout.xml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main_list.xml b/app/src/main/res/layout/activity_main_list.xml index 40a584df..c4eb9bbc 100644 --- a/app/src/main/res/layout/activity_main_list.xml +++ b/app/src/main/res/layout/activity_main_list.xml @@ -79,6 +79,14 @@ android:layout_marginTop="16dp" android:text="@string/custom_cameraView" /> +