Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample: Showcase expectedImageSize in CustomActivity. #503

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.View
import androidx.core.app.ActivityCompat
import com.canhub.cropper.CropImage.ActivityResult
import com.canhub.cropper.CropImageActivity
import com.canhub.cropper.CropImageView
import com.example.croppersample.R
import com.example.croppersample.databinding.ExtendedActivityBinding
import timber.log.Timber
Expand All @@ -26,33 +27,52 @@ internal class SampleCustomActivity : CropImageActivity() {
}

private lateinit var binding: ExtendedActivityBinding
private var counter = 0

override fun onCreate(savedInstanceState: Bundle?) {
binding = ExtendedActivityBinding.inflate(layoutInflater)

super.onCreate(savedInstanceState)
updateRotationCounter(counter.toString())

binding.saveBtn.setOnClickListener { cropImage() }
binding.backBtn.setOnClickListener { onBackPressedDispatcher.onBackPressed() }
binding.rotateText.setOnClickListener { onRotateClick() }

binding.cropImageView.setOnCropWindowChangedListener {
updateExpectedImageSize()
}

setCropImageView(binding.cropImageView)
}

override fun onSetImageUriComplete(
view: CropImageView,
uri: Uri,
error: Exception?,
) {
super.onSetImageUriComplete(view, uri, error)

updateRotationCounter()
updateExpectedImageSize()
}

private fun updateExpectedImageSize() {
binding.expectedImageSize.text = binding.cropImageView.expectedImageSize().toString()
}

override fun setContentView(view: View) {
super.setContentView(binding.root)
}

private fun updateRotationCounter(counter: String) {
binding.rotateText.text = getString(R.string.rotation_value, counter)
private fun updateRotationCounter() {
binding.rotateText.text = getString(R.string.rotation_value, binding.cropImageView.rotatedDegrees.toString())
}

override fun onPickImageResult(resultUri: Uri?) {
super.onPickImageResult(resultUri)

if (resultUri != null) binding.cropImageView.setImageUriAsync(resultUri)
if (resultUri != null) {
binding.cropImageView.setImageUriAsync(resultUri)
}
}

override fun getResultIntent(uri: Uri?, error: java.lang.Exception?, sampleSize: Int): Intent {
Expand Down Expand Up @@ -91,9 +111,7 @@ internal class SampleCustomActivity : CropImageActivity() {
}

private fun onRotateClick() {
counter += 90
binding.cropImageView.rotateImage(90)
if (counter == 360) counter = 0
updateRotationCounter(counter.toString())
updateRotationCounter()
}
}
38 changes: 20 additions & 18 deletions sample/src/main/res/layout/extended_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/bg_purple_gradient"
>
<ImageView
android:importantForAccessibility="no"
android:id="@+id/backBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:background="?attr/selectableItemBackgroundBorderless"
android:padding="@dimen/keyline_x4"
app:srcCompat="@drawable/ic_arrow_back_24"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -20,7 +21,7 @@
android:id="@+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:background="?attr/selectableItemBackgroundBorderless"
android:padding="@dimen/keyline_x4"
android:text="@string/save"
android:textColor="@color/orange"
Expand All @@ -33,35 +34,36 @@
android:id="@+id/canhubLogo"
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="@drawable/canhub_logo_white"
android:src="@drawable/canhub_logo_white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/backBtn"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:id="@+id/rotateText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/keyline_x4"
android:gravity="center"
android:padding="@dimen/keyline_x2"
android:text="@string/extend_body"
android:textIsSelectable="false"
tools:text="@string/rotation_value"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/canhubLogo"
app:layout_constraintTop_toTopOf="@id/canhubLogo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/canhubLogo"
/>
<TextView
android:id="@+id/rotateText"
android:id="@+id/expectedImageSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="@dimen/keyline_x4"
android:paddingTop="@dimen/keyline_x4"
android:paddingEnd="@dimen/keyline_x4"
android:paddingBottom="@dimen/keyline_x4"
android:text="@string/rotation_value"
android:padding="@dimen/keyline_x4"
android:gravity="center"
tools:text="Crop Rect"
android:textIsSelectable="false"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/text"
app:layout_constraintTop_toBottomOf="@id/rotateText"
/>
<com.canhub.cropper.CropImageView
android:id="@+id/cropImageView"
Expand All @@ -74,6 +76,6 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/rotateText"
app:layout_constraintTop_toBottomOf="@id/expectedImageSize"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 0 additions & 1 deletion sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
<string name="callLibraryWithoutUriCameraOnly_text">Call library directly without uri. Camera only</string>
<string name="callLibraryWithoutUriGalleryOnly_text">Call library directly without uri. Gallery only</string>
<string name="save">Save</string>
<string name="extend_body">This is a sample code using the library. The sample here is for extend CropImageActivity, to be about to custom build your personal view around it. For that we need to use CropImageView.</string>
<string name="rotation_value">Click to change rotation: %1$s</string>
<string name="crop_corner__shape">Corner Shape</string>
<string name="show_crop_label_text">Show Crop Label Text</string>
Expand Down