| | |
| | | package com.dayu.pipirrapp.activity; |
| | | |
| | | import android.animation.AnimatorSet; |
| | | import android.animation.ObjectAnimator; |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.net.Uri; |
| | | import android.os.Bundle; |
| | | import android.provider.MediaStore; |
| | | import android.util.Log; |
| | | import android.view.LayoutInflater; |
| | | import android.view.View; |
| | | import android.view.animation.Animation; |
| | | import android.view.animation.AnimationUtils; |
| | | |
| | | import androidx.activity.result.ActivityResultLauncher; |
| | | import androidx.activity.result.contract.ActivityResultContracts; |
| | | import androidx.recyclerview.widget.GridLayoutManager; |
| | | import androidx.recyclerview.widget.RecyclerView; |
| | | import androidx.recyclerview.widget.SimpleItemAnimator; |
| | |
| | | import com.dayu.pipirrapp.databinding.ActivityOrderDealBinding; |
| | | import com.dayu.pipirrapp.tool.FullyGridLayoutManager; |
| | | import com.dayu.pipirrapp.tool.GlideEngine; |
| | | import com.dayu.pipirrapp.view.TitleBar; |
| | | import com.luck.picture.lib.basic.PictureSelectionModel; |
| | | import com.luck.picture.lib.basic.PictureSelector; |
| | | import com.luck.picture.lib.config.PictureConfig; |
| | | import com.luck.picture.lib.config.PictureMimeType; |
| | | import com.luck.picture.lib.config.SelectMimeType; |
| | | import com.luck.picture.lib.config.SelectModeConfig; |
| | | import com.luck.picture.lib.decoration.GridSpacingItemDecoration; |
| | | import com.luck.picture.lib.engine.CompressFileEngine; |
| | | import com.luck.picture.lib.engine.ImageEngine; |
| | | import com.luck.picture.lib.entity.LocalMedia; |
| | | import com.luck.picture.lib.interfaces.OnGridItemSelectAnimListener; |
| | | import com.luck.picture.lib.interfaces.OnQueryFilterListener; |
| | | import com.luck.picture.lib.interfaces.OnSelectAnimListener; |
| | | import com.luck.picture.lib.entity.MediaExtraInfo; |
| | | import com.luck.picture.lib.interfaces.OnExternalPreviewEventListener; |
| | | import com.luck.picture.lib.interfaces.OnKeyValueResultCallbackListener; |
| | | import com.luck.picture.lib.utils.DateUtils; |
| | | import com.luck.picture.lib.utils.DensityUtil; |
| | | import com.luck.picture.lib.utils.MediaUtils; |
| | | import com.luck.picture.lib.utils.PictureFileUtils; |
| | | |
| | | import java.io.File; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | import top.zibin.luban.CompressionPredicate; |
| | | import top.zibin.luban.Luban; |
| | | import top.zibin.luban.OnNewCompressListener; |
| | | import top.zibin.luban.OnRenameListener; |
| | | |
| | | /** |
| | | * OrderDealDetailActivity - |
| | |
| | | * @since 2024-11-27 |
| | | */ |
| | | public class OrderDealActivity extends BaseActivity { |
| | | |
| | | private String TAG = "OrderDealActivity"; |
| | | ActivityOrderDealBinding binding; |
| | | RecyclerView mRecyclerView; |
| | | AddPictureAdapter mAdapter; |
| | | int maxSelectNum = 10;//最大照片 |
| | | int maxSelectVideoNum = 0;//最大视频 |
| | | private final List<LocalMedia> mData = new ArrayList<>(); |
| | | private ActivityResultLauncher<Intent> launcherResult; |
| | | private ImageEngine imageEngine; |
| | | |
| | | @Override |
| | | protected void onCreate(Bundle savedInstanceState) { |
| | | super.onCreate(savedInstanceState); |
| | | binding = ActivityOrderDealBinding.inflate(LayoutInflater.from(this)); |
| | | setContentView(binding.getRoot()); |
| | | launcherResult = createActivityResultLauncher(); |
| | | initView(); |
| | | } |
| | | |
| | | |
| | | void initView() { |
| | | new TitleBar(this).setTitleText("处理工单").setLeftIco().setLeftIcoListening(v -> OrderDealActivity.this.finish()); |
| | | mRecyclerView = binding.recycler; |
| | | FullyGridLayoutManager manager = new FullyGridLayoutManager(this, |
| | | 4, GridLayoutManager.VERTICAL, false); |
| | |
| | | mAdapter = new AddPictureAdapter(this, mData); |
| | | mAdapter.setSelectMax(maxSelectNum + maxSelectVideoNum); |
| | | mRecyclerView.setAdapter(mAdapter); |
| | | imageEngine = GlideEngine.createGlideEngine(); |
| | | mAdapter.setOnItemClickListener(new AddPictureAdapter.OnItemClickListener() { |
| | | @Override |
| | | public void onItemClick(View v, int position) { |
| | | |
| | | // 预览图片、视频、音频 |
| | | PictureSelector.create(OrderDealActivity.this) |
| | | .openPreview() |
| | | .setImageEngine(imageEngine) |
| | | .setExternalPreviewEventListener(new MyExternalPreviewEventListener()) |
| | | .startActivityPreview(position, true, mAdapter.getData()); |
| | | } |
| | | |
| | | @Override |
| | | public void openPicture() { |
| | | //添加图片 |
| | | mOpenPicture(); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 添加图片 |
| | | */ |
| | | private void mOpenPicture() { |
| | | // 进入相册 |
| | | PictureSelectionModel selectionModel = PictureSelector.create(this) |
| | | .openGallery(SelectMimeType.ofImage()) |
| | | .setMaxSelectNum(maxSelectNum) |
| | | .setMaxVideoSelectNum(maxSelectVideoNum) |
| | | .setImageEngine(GlideEngine.createGlideEngine()) |
| | | .setImageEngine(imageEngine) |
| | | //设置图片压缩 |
| | | .setCompressEngine(new ImageFileCompressEngine()) |
| | | .setSelectedData(mAdapter.getData()); |
| | | |
| | | selectionModel.forResult(PictureConfig.CHOOSE_REQUEST); |
| | | selectionModel.forResult(launcherResult); |
| | | } |
| | | |
| | | /** |
| | | * 创建一个ActivityResultLauncher |
| | | * |
| | | * @return |
| | | */ |
| | | private ActivityResultLauncher<Intent> createActivityResultLauncher() { |
| | | return registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), |
| | | result -> { |
| | | int resultCode = result.getResultCode(); |
| | | if (resultCode == RESULT_OK) { |
| | | ArrayList<LocalMedia> selectList = PictureSelector.obtainSelectorList(result.getData()); |
| | | analyticalSelectResults(selectList); |
| | | } else if (resultCode == RESULT_CANCELED) { |
| | | Log.i(TAG, "onActivityResult PictureSelector Cancel"); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 外部预览监听事件 |
| | | */ |
| | | private class MyExternalPreviewEventListener implements OnExternalPreviewEventListener { |
| | | |
| | | @Override |
| | | public void onPreviewDelete(int position) { |
| | | mAdapter.remove(position); |
| | | mAdapter.notifyItemRemoved(position); |
| | | } |
| | | |
| | | @Override |
| | | public boolean onLongPressDownload(Context context, LocalMedia media) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 处理选择结果 |
| | | * |
| | | * @param result |
| | | */ |
| | | private void analyticalSelectResults(ArrayList<LocalMedia> result) { |
| | | for (LocalMedia media : result) { |
| | | if (media.getWidth() == 0 || media.getHeight() == 0) { |
| | | if (PictureMimeType.isHasImage(media.getMimeType())) { |
| | | MediaExtraInfo imageExtraInfo = MediaUtils.getImageSize(this, media.getPath()); |
| | | media.setWidth(imageExtraInfo.getWidth()); |
| | | media.setHeight(imageExtraInfo.getHeight()); |
| | | } else if (PictureMimeType.isHasVideo(media.getMimeType())) { |
| | | MediaExtraInfo videoExtraInfo = MediaUtils.getVideoSize(this, media.getPath()); |
| | | media.setWidth(videoExtraInfo.getWidth()); |
| | | media.setHeight(videoExtraInfo.getHeight()); |
| | | } |
| | | } |
| | | Log.i(TAG, "文件名: " + media.getFileName()); |
| | | Log.i(TAG, "是否压缩:" + media.isCompressed()); |
| | | Log.i(TAG, "压缩:" + media.getCompressPath()); |
| | | Log.i(TAG, "初始路径:" + media.getPath()); |
| | | Log.i(TAG, "绝对路径:" + media.getRealPath()); |
| | | Log.i(TAG, "是否裁剪:" + media.isCut()); |
| | | Log.i(TAG, "裁剪路径:" + media.getCutPath()); |
| | | Log.i(TAG, "是否开启原图:" + media.isOriginal()); |
| | | Log.i(TAG, "原图路径:" + media.getOriginalPath()); |
| | | Log.i(TAG, "沙盒路径:" + media.getSandboxPath()); |
| | | Log.i(TAG, "水印路径:" + media.getWatermarkPath()); |
| | | Log.i(TAG, "视频缩略图:" + media.getVideoThumbnailPath()); |
| | | Log.i(TAG, "原始宽高: " + media.getWidth() + "x" + media.getHeight()); |
| | | Log.i(TAG, "裁剪宽高: " + media.getCropImageWidth() + "x" + media.getCropImageHeight()); |
| | | Log.i(TAG, "文件大小: " + PictureFileUtils.formatAccurateUnitFileSize(media.getSize())); |
| | | Log.i(TAG, "文件时长: " + media.getDuration()); |
| | | } |
| | | runOnUiThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | boolean isMaxSize = result.size() == mAdapter.getSelectMax(); |
| | | int oldSize = mAdapter.getData().size(); |
| | | mAdapter.notifyItemRangeRemoved(0, isMaxSize ? oldSize + 1 : oldSize); |
| | | mAdapter.getData().clear(); |
| | | |
| | | mAdapter.getData().addAll(result); |
| | | mAdapter.notifyItemRangeInserted(0, result.size()); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 自定义图片压缩 |
| | | */ |
| | | private static class ImageFileCompressEngine implements CompressFileEngine { |
| | | |
| | | @Override |
| | | public void onStartCompress(Context context, ArrayList<Uri> source, OnKeyValueResultCallbackListener call) { |
| | | Luban.with(context).load(source).ignoreBy(100).setRenameListener(new OnRenameListener() { |
| | | @Override |
| | | public String rename(String filePath) { |
| | | int indexOf = filePath.lastIndexOf("."); |
| | | String postfix = indexOf != -1 ? filePath.substring(indexOf) : ".jpg"; |
| | | return DateUtils.getCreateFileName("CMP_") + postfix; |
| | | } |
| | | }).filter(new CompressionPredicate() { |
| | | @Override |
| | | public boolean apply(String path) { |
| | | if (PictureMimeType.isUrlHasImage(path) && !PictureMimeType.isHasHttp(path)) { |
| | | return true; |
| | | } |
| | | return !PictureMimeType.isUrlHasGif(path); |
| | | } |
| | | }).setCompressListener(new OnNewCompressListener() { |
| | | @Override |
| | | public void onStart() { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onSuccess(String source, File compressFile) { |
| | | if (call != null) { |
| | | call.onCallback(source, compressFile.getAbsolutePath()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onError(String source, Throwable e) { |
| | | if (call != null) { |
| | | call.onCallback(source, null); |
| | | } |
| | | } |
| | | }).launch(); |
| | | } |
| | | } |
| | | |
| | | |