PictureViewer image viewing frame conflicts with Glide

PictureViewer image viewing frame conflicts with Glide


Head up picture

foreword

In the near future, a new function will be added to the company's project, which is to click on the picture to enlarge it and download it. Thinking about it in my mind, there was a third-party picture viewing framework pictureViewer in a previous project. It's quite convenient to use, and you can start using it without saying a word. As a result, I was stumped all afternoon because of the Glide image loading framework. Glide in the project conflicts with Glide in pictureViewer. It was resolved after some tossing.

Cause of

The pictureViewer framework and the framework used by the project are not the same version of Glide. I use Glide 4.8.0 in the project
Glide.png in the project
and Glide 3.7.0 in the pictureView framework, and the update in GitHub is still in 2017. It is estimated that it is no longer maintained, but this framework is not very big. It's not too difficult to write a good one, but we programmers like to be lazy hhhhh.
Framework Glide version in gitHub.png

Solution

I also spent a long time on the solution. I tried changing the framework, but it still seems to be the same after changing. Also tried to resolve conflicts in dependencies

implementation ('com.github.SuperKotlin:PictureViewer:2.0.1'){
          exclude group: 'com.github.bumptech.glide'
}

But still did not solve this problem. No way, only the last method can be used. Download the project on GitHub and add it to your own project, and then modify the Glide version inside. So what to do?

first step

First download GitHub to the computer, address: PictureViewer GitHub address
pictureViewer.png
and then unzip, unzip, open Android Studio in the project > click File > move the mouse to New > move to Import Module and click

1.png

Add the pictureViewer address you downloaded and decompressed to the project > then click Finish (I have already added it, so Finish cannot be clicked)
2.png
and then add the following sentence to the dependencies in the project's gradle, thus completing the import of the Module.

  //图片查看器
    implementation project (':PictureViewer')

Step 2: Modify the internal code

First replace Glide with the same version used in the project:
location.png

implementation 'com.github.bumptech.glide:glide:4.8.0'

Then you will find that there is an error in the framework:
Error interface.png
the reason is that when you upgrade to Glide V4, Glide modifies and deletes some methods. See this article
or the official document of Glide V4.
In Glide V4, .asBitmap() is placed after .with(). The placeholder image and transition animation, etc. have also been modified, and there is no SimpleTarget method. then what should we do? Don't worry, although it doesn't have SimpleTarget, it still has the method SimpleTarget, we just need to convert it.

After modification (commented out is the previous code)

if (!TextUtils.isEmpty(mImageUrl)) {
    
    
            /*Glide.with(getActivity())
                    .load(mImageUrl)
                    .asBitmap()
                    .placeholder(mImageLoading)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
                            mBitmap = bitmap;
                            mImageView.setImageBitmap(mBitmap);
                            mAttacher.update();
                        }
                    });*/
            DrawableCrossFadeFactory drawableCrossFadeFactory = new DrawableCrossFadeFactory.Builder().setCrossFadeEnabled(true).build();
            Glide.with(getActivity())
                    .load(mImageUrl)
                    .apply(new RequestOptions().placeholder(mImageLoading))
                    .transition(DrawableTransitionOptions.with(drawableCrossFadeFactory))
            .into(new SimpleTarget<Drawable>() {
    
    
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
    
    
                    mBitmap=drawableToBitmap(resource);
                    mImageView.setImageBitmap(mBitmap);
                    mAttacher.update();
                }
            });

There is also a method for converting Drawable to Bitmap.

    /**
     * Drawable转换成一个Bitmap
     *
     * @param drawable drawable对象
     * @return
     */
    public static final Bitmap drawableToBitmap(Drawable drawable) {
    
    
        Bitmap bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }

Well, after reaching this step, the work is done. The next step is how to use it. In fact, it is very simple to give the code directly: I only have a single picture here, and only one picture is passed each time. The method I defined only needs to pass in the context and the address of the picture home. If there are multiple pictures, just pass in the List collection of multiple picture addresses. See this article for specific details. A beautiful picture viewer PictrueViewer

 Tools.showSinglePic(context,Declare.ServerletImgUrl2 + imgUrlList.getFollow_data().get(0).getImages());
    /**
     * 查看单张图片
     * @param imgUrl
     */
    public static void showSinglePic(Context context,String  imgUrl) {
    
    
        ArrayList<String > imgUrls=new ArrayList<>();
        //查看单张,只需要存入一个图片值
        if (imgUrls.size()==0){
    
    
            imgUrls.add(imgUrl);
        }
        PictureConfig config = new PictureConfig.Builder()
                .setListData(imgUrls)    //图片数据List<String> list
                .setPosition(0)    //图片下标(从第position张图片开始浏览)
                .setDownloadPath("RoadFile")    //图片下载文件夹地址
//                .setIsShowNumber(true)//是否显示数字下标
                .needDownload(true)    //是否支持图片下载
                .setPlacrHolder(R.mipmap.btn_add)    //占位符图片(图片加载完成前显示的资源图片,来源drawable或者mipmap)
                .build();
        ImagePagerActivity.startActivity(context, config);
    }

Everything is ready, let's take a look at the result:
result.gif

Summarize

Although there are not many places to modify, and it may not be a very rigorous method, but I have been working on it for a few hours, and I feel that the process of slowly exploring is pretty good, with a slight sense of accomplishment. Ha ha!

The code word is not easy, please like it!

Guess you like

Origin blog.csdn.net/weixin_43683367/article/details/103008228