项目作者: jdsdhp

项目描述 :
A simple and useful Android Gallery based on Picasso and PhotoView.
高级语言: Kotlin
项目地址: git://github.com/jdsdhp/gallery-droid.git
创建时间: 2021-01-06T07:48:23Z
项目社区:https://github.com/jdsdhp/gallery-droid

开源协议:Other

下载


☘ GalleryDroid

A simple and useful Android Gallery based on Picasso and PhotoView.

JitPack
API
License
Twitter

Including in your project

Gradle

  1. allprojects {
  2. repositories {
  3. maven { url 'https://jitpack.io' }
  4. }
  5. }
  6. dependencies {
  7. implementation "com.github.jdsdhp:kutil:0.1.9"
  8. implementation 'com.github.jdsdhp:gallery-droid:0.1.4'
  9. }

Basic Usage

Gallery Screen Picture Screen Inmersive Screen

Kotlin

Images in this example have been loaded from Lorem Picsum.

Usage from Activity or Fragment. The fragment (GalleryFragment) is usually added through a transition or directly from XML.

  1. supportFragmentManager.beginTransaction()
  2. .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) //Optional
  3. .replace(R.id.container, GalleryFragment.newInstance())
  4. .commit()

All that remains is to add elements to the gallery. It can be done using the onAttachFragment method in the Activity or Fragment where you have the GalleryFragment.

  1. override fun onAttachFragment(fragment: Fragment) {
  2. super.onAttachFragment(fragment)
  3. if (fragment is GalleryFragment) {
  4. fragment.injectGallery(
  5. GalleryDroid(
  6. listOf(
  7. Picture(
  8. fileURL = "https://picsum.photos/id/15/720/1000",
  9. fileThumbURL = "https://picsum.photos/id/15/200",
  10. fileName = "Lorem Ipsum 1", //Optional
  11. ),
  12. Picture(
  13. fileURL = "https://picsum.photos/id/16/720/1000",
  14. fileThumbURL = "https://picsum.photos/id/16/200",
  15. fileName = "Lorem Ipsum 2", //Optional
  16. ),
  17. Picture(
  18. fileURL = "https://picsum.photos/id/14/720/1000",
  19. fileThumbURL = "https://picsum.photos/id/14/200",
  20. fileName = "Lorem Ipsum 3", //Optional
  21. )
  22. )
  23. )
  24. )
  25. }
  26. }

XML

Add this to your AndroidManifest.xml file if you are using the default activity provided by the library. In case GalleryFragment is being used directly, it would not be necessary.

  1. <activity
  2. android:name="com.jesusd0897.gallerydroid.view.activity.PictureDetailActivity"
  3. android:configChanges="orientation|screenSize"
  4. android:theme="@style/Theme.MaterialComponents.NoActionBar" ></activity>

Advanced Usage

Custom Gallery Custom Layout Custom Transformation

Kotlin

By overwriting onAttachFragment you can add some customizations to the gallery.

  1. override fun onAttachFragment(fragment: Fragment) {
  2. super.onAttachFragment(fragment)
  3. if (fragment is GalleryFragment) {
  4. fragment.injectGallery(
  5. GalleryDroid(
  6. //...your pictures
  7. )
  8. .layoutManager(GalleryDroid.LAYOUT_STAGGERED_GRID)
  9. .loadingPlaceholder(
  10. ContextCompat.getDrawable(this, R.drawable.ic_custom_loading_placeholder)
  11. )
  12. .errorPlaceholder(
  13. ContextCompat.getDrawable(this, R.drawable.ic_custom_error_placeholder)
  14. )
  15. .pictureCornerRadius(16f)
  16. .pictureElevation(8f)
  17. .transformer(GalleryDroid.TRANSFORMER_CUBE_OUT)
  18. .spacing(12)
  19. .portraitColumns(2)
  20. .landscapeColumns(4)
  21. .emptyTitle("Upps")
  22. .emptySubTitle(getString(R.string.no_items_found))
  23. .emptyIcon(ContextCompat.getDrawable(this, R.drawable.ic_round_find_in_page))
  24. .decoratorLayout(R.layout.custom_decorator)
  25. .autoClickHandler(true)
  26. .useLabels(false)
  27. )
  28. }
  29. }
Adding click listeners

If you want to hear the clicks on the elements of the gallery you can add an OnPictureItemClickListener to the GalleryFragment. If the “autoClickHandler” property has been used to true, PictureDetailActivity will still be opened, so that this does not happen it must be set to false.

  1. fragment.onItemClickListener = object : OnPictureItemClickListener {
  2. override fun onClick(picture: Picture, position: Int) {
  3. Toast.makeText(this@MainActivity, "onClick = $picture", Toast.LENGTH_SHORT)
  4. .show()
  5. }
  6. override fun onLongClick(picture: Picture, position: Int) {
  7. Toast.makeText(this@MainActivity, "onLongClick = $picture", Toast.LENGTH_SHORT)
  8. .show()
  9. }
  10. }
Using just fragment detail

It’s a way to use your own activity or fragment instead of using PictureDetailActivity as the default detail view handler. PicturePagerFragment is the one that contains the image viewer and can be used directly through a transition after having created your own OnPictureItemClickListener and having set “autoClickHandler” to false.

In your own onClick method:

  1. override fun onClick(picture: Picture, position: Int){
  2. supportFragmentManager
  3. .beginTransaction()
  4. .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) //Optional
  5. .replace(R.id.container,
  6. PicturePagerFragment.newInstance(
  7. pictures,
  8. position,
  9. transformerId
  10. )
  11. )
  12. .commit()
  13. }

Sample project

It’s very important to check out the sample app. Most techniques that you would want to implement are already implemented in the examples.

View the sample app’s source code here

License

  1. Copyright (c) 2021 jesusd0897.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.