Android custom view to realize multi picture selection control

preface

I believe many friends will encounter image upload in development, especially multi image upload. The most classic is the image selection of wechat. In many cases, multi graph selection will be used, so with this article, I took some time to write a control today. • Support custom selection of picture style • support setting of picture selection quantity • support picture preview and deletion • support picture taking

Let's see the effect first

Implementation analysis

If we don't define controls, we want to realize such a function, which is nothing more than writing a GridView, displaying pictures for selection when item is clicked, and refreshing the data of GridView when returning to the interface. We write these logic in our custom GridView to become a new control.

1. GridView effect display, logical implementation.

The code is relatively simple and annotated. In the initialization method of view, obtain some information and the display information of GridView, and set the adapter Association.

Click events are set in the item of the GridView and provided to the onactivityresult method used to refresh data externally.

You should understand from the above code.

2. Picture preview and deletion

This step is very simple. Get the Imagelist and position transmitted, display the image, remove the data corresponding to the Imagelist when clicking delete, and refresh the viewpager

Direct code

I believe everyone can understand the above code, load the image with viewpager, and send the Imagelist back when clicking back and deleting the image. In this way, the Imagelist is obtained in onactivityresult, the adapter is refreshed, and the GridView is re rendered.

use

1. Introducing imagepickerview into layout

2, get ImagePickerView in Activity, and call ImagePickerView data refresh method in onActivityResult method imagePicker. onActivityResult(requestCode,resultCode,data);

3. Get the path of the selected picture and call imagepicker Getimagelist() returns the list of image selections

Here is a code example

epilogue

After looking at the implementation of imagepickerview, do you find that a custom control is actually very simple. When we make custom controls, in fact, most of the time we just add a little more to the functions provided by the Android system. Standing on the shoulders of giants can see farther.

This article does not mention image loading and image selection. I refer to the article for image loading. Interested friends can go to GitHub to check, https://github.com/easonline/AndroidImagePicker 。 I modified the source code in my demo and uniformly used fresco to load pictures.

If you need to refer to the source code, please refer to: source code download

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>