Skip to content

Commit

Permalink
add crop image
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafahashim committed Nov 7, 2023
1 parent 6d2eb49 commit e00ac3b
Show file tree
Hide file tree
Showing 55 changed files with 1,785 additions and 337 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ android library for gallery images and videos, and capture camera image and vide
## Add it in your root build.gradle at the end of repositories or settings.gradle:
```groovy
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
```

#Step 2. Add the dependency

```groovy
implementation 'com.github.mostafahashim:GalleryLibrary:1.2.3'
implementation 'com.github.mostafahashim:GalleryLibrary:1.2.4'
```
# Usage
Expand All @@ -31,6 +31,7 @@ fun openGallery() {

GalleryLib(this).showGallery(
isDialog = binding.viewModel?.isDialog?.value ?: false,
isOpenEdit = binding.viewModel?.isOpenEdit?.value ?: false,
selectionType = if (binding.viewModel?.isShowImages?.value!! && binding.viewModel?.isShowVideos?.value!!) GalleryConstants.GalleryTypeImagesAndVideos
else if (binding.viewModel?.isShowImages?.value!!) GalleryConstants.GalleryTypeImages
else if (binding.viewModel?.isShowVideos?.value!!) GalleryConstants.GalleryTypeVideos
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class MainActivity : AppCompatActivity(), MainViewModel.Observer {

GalleryLib(this).showGallery(
isDialog = binding.viewModel?.isDialog?.value ?: false,
isOpenEdit = binding.viewModel?.isOpenEdit?.value ?: false,
selectionType = if (binding.viewModel?.isShowImages?.value!! && binding.viewModel?.isShowVideos?.value!!) GalleryConstants.GalleryTypeImagesAndVideos
else if (binding.viewModel?.isShowImages?.value!!) GalleryConstants.GalleryTypeImages
else if (binding.viewModel?.isShowVideos?.value!!) GalleryConstants.GalleryTypeVideos
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MainViewModel : ViewModel() {

var recyclerGalleryAdapter: RecyclerGalleryAdapter
var isDialog = MutableLiveData(true)
var isOpenEdit = MutableLiveData(true)
var isRTL = MutableLiveData(true)
var isShowImages = MutableLiveData(true)
var isShowVideos = MutableLiveData(true)
Expand Down
25 changes: 20 additions & 5 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,28 @@
android:orientation="vertical"
android:padding="@dimen/padding_16">

<CheckBox
android:layout_width="wrap_content"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/padding_10"
android:checked="@={viewModel.isDialog}"
android:text="@string/is_dialog"
android:textColor="@color/black" />
android:orientation="horizontal">

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="@={viewModel.isDialog}"
android:text="@string/is_dialog"
android:textColor="@color/black" />

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/padding_40"
android:checked="@={viewModel.isOpenEdit}"
android:text="@string/edit_after_select"
android:textColor="@color/black" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<string name="rtl_direction">RTL Direction</string>
<string name="ltr_direction">LTR Direction</string>
<string name="grid_columns">Grid Columns</string>
<string name="edit_after_select">Edit after select</string>
</resources>
4 changes: 3 additions & 1 deletion gallerylib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ dependencies {
implementation 'androidx.media3:media3-exoplayer:1.1.1'
implementation 'androidx.media3:media3-exoplayer-dash:1.1.1'
implementation 'androidx.media3:media3-ui:1.1.1'
//crop
implementation("com.vanniktech:android-image-cropper:4.5.0")
}

afterEvaluate {
Expand All @@ -88,7 +90,7 @@ afterEvaluate {

groupId = 'com.github.mostafahashim'
artifactId = 'gallery-lib'
version = '1.2.3'
version = '1.2.4'
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions gallerylib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@
android:theme="@style/AppTheme.NoActionBar.LightScreen"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="hashim.gallerylib.view.selected.SelectedActivity"
android:configChanges="density|keyboard|keyboardHidden|orientation|screenSize|locale|layoutDirection"
android:hardwareAccelerated="true"
android:resizeableActivity="true"
android:theme="@style/AppTheme.NoActionBar.LightScreen"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="hashim.gallerylib.view.crop.CropActivity"
android:configChanges="density|keyboard|keyboardHidden|orientation|screenSize|locale|layoutDirection"
android:hardwareAccelerated="true"
android:resizeableActivity="true"
android:theme="@style/AppTheme.NoActionBar.LightScreen"
android:windowSoftInputMode="adjustResize" />

<activity
android:name="hashim.gallerylib.view.cameraActivity.CameraActivity"
android:configChanges="density|keyboard|keyboardHidden|orientation|screenSize|locale|layoutDirection"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package hashim.gallerylib.imageviewer.viewer.dialog

import android.content.Context
import android.view.KeyEvent
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.appcompat.app.AlertDialog
import hashim.gallerylib.R
import hashim.gallerylib.imageviewer.viewer.builder.BuilderData
Expand Down Expand Up @@ -31,6 +33,10 @@ internal class ImageViewerDialog<T>(
.setOnKeyListener { _, keyCode, event -> onDialogKeyEvent(keyCode, event) }
.create()
.apply {
window!!.setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
setOnShowListener { viewerView.open(builderData.transitionView, animateOpen) }
setOnDismissListener { builderData.onDismissListener?.onDismiss() }
}
Expand Down Expand Up @@ -82,6 +88,10 @@ internal class ImageViewerDialog<T>(

private fun setupViewerView() {
viewerView.apply {
layoutParams = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
)
isZoomingAllowed = builderData.isZoomingAllowed
isSwipeToDismissAllowed = builderData.isSwipeToDismissAllowed

Expand All @@ -91,8 +101,10 @@ internal class ImageViewerDialog<T>(

setBackgroundColor(builderData.backgroundColor)
// setImages(builderData.images, builderData.startPosition, builderData.imageLoader)
setImages(builderData.images, builderData.startPosition, builderData.imageLoader,
builderData.viewHolderLoader)
setImages(
builderData.images, builderData.startPosition, builderData.imageLoader,
builderData.viewHolderLoader
)

onPageChange = { position -> builderData.imageChangeListener?.onImageChange(position) }
onDismiss = { dialog.dismiss() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ data class GalleryModel(
var isSelected: Boolean = false,
var type: String = "",
var isVideo: Boolean = false,
var isReleasePlayer: Boolean = false,
var albumName: String = "",
var index_when_selected: Int = 0,
var item_date_modified: Int = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ object GalleryConstants {

const val Language = "localeLanguage"
const val selected = "selected"
const val isOpenEdit = "isOpenEdit"
const val maxSelectionCount = "MaxPhotosNumber"
const val gridColumnsCount = "GridColumnsCount"
const val showType = "showType"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package hashim.gallerylib.util

import android.annotation.TargetApi
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.os.Build
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.SeekBar
import androidx.annotation.ColorInt
import androidx.appcompat.widget.AppCompatSeekBar
import androidx.core.content.ContextCompat
import hashim.gallerylib.R


class HorizontalProgressWheelView : View {

private val mCanvasClipBounds = Rect()

private var mScrollingListener: ScrollingListener? = null
private var mLastTouchedPosition = 0f

private var mProgressLinePaint: Paint? = null
private var mProgressMiddleLinePaint: Paint? = null
private var mProgressLineWidth = 0
private var mProgressLineHeight: Int = 0
private var mProgressLineMargin = 0

private var mScrollStarted = false
private var mTotalScrollDistance = 0f

private var mMiddleLineColor = 0

constructor(context: Context) : super(context) {
init()
}

constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
init()
}

constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
init()
}

constructor(
context: Context,
attrs: AttributeSet,
defStyleAttr: Int,
defStyleRes: Int
) : super(context, attrs, defStyleAttr) {
init()
}

fun setScrollingListener(scrollingListener: ScrollingListener?) {
mScrollingListener = scrollingListener
}

fun setMiddleLineColor(@ColorInt middleLineColor: Int) {
mMiddleLineColor = middleLineColor
mProgressMiddleLinePaint!!.color = mMiddleLineColor
invalidate()
}

override fun onTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN -> mLastTouchedPosition = event.x
MotionEvent.ACTION_UP -> if (mScrollingListener != null) {
mScrollStarted = false
mScrollingListener!!.onScrollEnd()
}

MotionEvent.ACTION_MOVE -> {
val distance = event.x - mLastTouchedPosition
if (distance != 0f) {
if (!mScrollStarted) {
mScrollStarted = true
if (mScrollingListener != null) {
mScrollingListener!!.onScrollStart()
}
}
onScrollEvent(event, distance)
}
}
}
return true
}

override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
canvas.getClipBounds(mCanvasClipBounds)
val linesCount = mCanvasClipBounds.width() / (mProgressLineWidth + mProgressLineMargin)
val deltaX = mTotalScrollDistance % (mProgressLineMargin + mProgressLineWidth).toFloat()
for (i in 0 until linesCount) {
if (i < linesCount / 4) {
mProgressLinePaint!!.alpha = (255 * (i / (linesCount / 4).toFloat())).toInt()
} else if (i > linesCount * 3 / 4) {
mProgressLinePaint!!.alpha =
(255 * ((linesCount - i) / (linesCount / 4).toFloat())).toInt()
} else {
mProgressLinePaint!!.alpha = 255
}
canvas.drawLine(
-deltaX + mCanvasClipBounds.left + i * (mProgressLineWidth + mProgressLineMargin),
mCanvasClipBounds.centerY() - mProgressLineHeight / 4.0f,
-deltaX + mCanvasClipBounds.left + i * (mProgressLineWidth + mProgressLineMargin),
mCanvasClipBounds.centerY() + mProgressLineHeight / 4.0f, mProgressLinePaint!!
)
}
canvas.drawLine(
mCanvasClipBounds.centerX().toFloat(),
mCanvasClipBounds.centerY() - mProgressLineHeight / 2.0f,
mCanvasClipBounds.centerX().toFloat(),
mCanvasClipBounds.centerY() + mProgressLineHeight / 2.0f,
mProgressMiddleLinePaint!!
)
}

private fun onScrollEvent(event: MotionEvent, distance: Float) {
mTotalScrollDistance -= distance
postInvalidate()
mLastTouchedPosition = event.x
if (mScrollingListener != null) {
mScrollingListener!!.onScroll(-distance, mTotalScrollDistance)
}
}

private fun init() {
mMiddleLineColor =
ContextCompat.getColor(context, R.color.colorPrimaryDark)
mProgressLineWidth =
context.resources.getDimensionPixelSize(R.dimen.padding_2)
mProgressLineHeight =
context.resources.getDimensionPixelSize(R.dimen.padding_20)
mProgressLineMargin =
context.resources.getDimensionPixelSize(R.dimen.padding_10)
mProgressLinePaint = Paint(Paint.ANTI_ALIAS_FLAG)
mProgressLinePaint!!.style = Paint.Style.STROKE
mProgressLinePaint!!.strokeWidth = mProgressLineWidth.toFloat()
mProgressLinePaint!!.color = ContextCompat.getColor(context, R.color.gray_dark)
mProgressMiddleLinePaint = Paint(mProgressLinePaint)
mProgressMiddleLinePaint!!.color = mMiddleLineColor
mProgressMiddleLinePaint!!.strokeCap = Paint.Cap.ROUND
mProgressMiddleLinePaint!!.strokeWidth =
context.resources.getDimensionPixelSize(R.dimen.padding_4)
.toFloat()

}

interface ScrollingListener {
fun onScrollStart()
fun onScroll(delta: Float, totalDistance: Float)
fun onScrollEnd()
}

}
5 changes: 4 additions & 1 deletion gallerylib/src/main/java/hashim/gallerylib/util/functions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package hashim.gallerylib.util

import android.app.Activity
import android.content.Context
import android.util.DisplayMetrics

tailrec fun Context.activity(): Activity = when (this) {
is Activity -> this
else -> (this as? ContextWrapper)!!.baseContext.activity()
}
}

fun Int.dpToPx(displayMetrics: DisplayMetrics): Int = (this * displayMetrics.density).toInt()
Loading

0 comments on commit e00ac3b

Please sign in to comment.