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

WIP - Pinch to zoom TextView handling #202

Closed
wants to merge 5 commits into from
Closed
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
121 changes: 33 additions & 88 deletions photoeditor/src/main/java/com/automattic/photoeditor/PhotoEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
Expand All @@ -25,12 +24,12 @@ import androidx.annotation.IntRange
import androidx.annotation.RequiresPermission
import androidx.annotation.UiThread
import androidx.emoji.text.EmojiCompat
import androidx.emoji.text.EmojiCompat.InitCallback
import com.automattic.photoeditor.gesture.MultiTouchListener
import com.automattic.photoeditor.gesture.MultiTouchListener.OnMultiTouchListener
import com.automattic.photoeditor.util.BitmapUtil
import com.automattic.photoeditor.views.PhotoEditorView
import com.automattic.photoeditor.views.ViewType
import com.automattic.photoeditor.views.ViewType.EMOJI
import com.automattic.photoeditor.views.added.AddedView
import com.automattic.photoeditor.views.added.AddedViewList
import com.automattic.photoeditor.views.brush.BrushDrawingView
Expand Down Expand Up @@ -178,48 +177,16 @@ class PhotoEditor private constructor(builder: Builder) :
fun addImage(desiredImage: Bitmap) {
getLayout(ViewType.IMAGE)?.apply {
val imageView = findViewById<ImageView>(R.id.imgPhotoEditorImage)
val frmBorder = findViewById<FrameLayout>(R.id.frmBorder)
val imgClose = findViewById<ImageView>(R.id.imgPhotoEditorClose)

imageView.setImageBitmap(desiredImage)

val multiTouchListenerInstance = newMultiTouchListener
multiTouchListenerInstance.setOnGestureControl(object : MultiTouchListener.OnGestureControl {
override fun onClick() {
val isBackgroundVisible = frmBorder.tag != null && frmBorder.tag as Boolean
frmBorder.setBackgroundResource(if (isBackgroundVisible) 0 else R.drawable.rounded_border_tv)
imgClose.visibility = if (isBackgroundVisible) View.GONE else View.VISIBLE
frmBorder.tag = !isBackgroundVisible
}

override fun onLongClick() {}
})

setOnTouchListener(multiTouchListenerInstance)

addViewToParent(this, ViewType.IMAGE)
}
}

fun addNewImageView(isAnimated: Boolean, uri: Uri) {
getLayout(ViewType.IMAGE)?.apply {
val imageView = findViewById<ImageView>(R.id.imgPhotoEditorImage)
val frmBorder = findViewById<FrameLayout>(R.id.frmBorder)
val imgClose = findViewById<ImageView>(R.id.imgPhotoEditorClose)

val multiTouchListenerInstance = newMultiTouchListener
multiTouchListenerInstance.setOnGestureControl(object : MultiTouchListener.OnGestureControl {
override fun onClick() {
val isBackgroundVisible = frmBorder.tag != null && frmBorder.tag as Boolean
frmBorder.setBackgroundResource(if (isBackgroundVisible) 0 else R.drawable.rounded_border_tv)
imgClose.visibility = if (isBackgroundVisible) View.GONE else View.VISIBLE
frmBorder.tag = !isBackgroundVisible
}

override fun onLongClick() {}
})

setOnTouchListener(multiTouchListenerInstance)

addViewToParent(this, if (isAnimated) ViewType.STICKER_ANIMATED else ViewType.IMAGE, uri)

Expand All @@ -233,22 +200,6 @@ class PhotoEditor private constructor(builder: Builder) :
fun addNewImageView(isAnimated: Boolean): ImageView? {
getLayout(ViewType.IMAGE)?.apply {
val imageView = findViewById<ImageView>(R.id.imgPhotoEditorImage)
val frmBorder = findViewById<FrameLayout>(R.id.frmBorder)
val imgClose = findViewById<ImageView>(R.id.imgPhotoEditorClose)

val multiTouchListenerInstance = newMultiTouchListener
multiTouchListenerInstance.setOnGestureControl(object : MultiTouchListener.OnGestureControl {
override fun onClick() {
val isBackgroundVisible = frmBorder.tag != null && frmBorder.tag as Boolean
frmBorder.setBackgroundResource(if (isBackgroundVisible) 0 else R.drawable.rounded_border_tv)
imgClose.visibility = if (isBackgroundVisible) View.GONE else View.VISIBLE
frmBorder.tag = !isBackgroundVisible
}

override fun onLongClick() {}
})

setOnTouchListener(multiTouchListenerInstance)

addViewToParent(this, if (isAnimated) ViewType.STICKER_ANIMATED else ViewType.IMAGE)

Expand All @@ -270,12 +221,6 @@ class PhotoEditor private constructor(builder: Builder) :
brushDrawingView.brushDrawingMode = false
getLayout(ViewType.TEXT)?.apply {
val textInputTv = findViewById<TextView>(R.id.tvPhotoEditorText)
val imgClose = findViewById<ImageView>(R.id.imgPhotoEditorClose)
val frmBorder = findViewById<FrameLayout>(R.id.frmBorder)

// hide cross and background borders for now
imgClose.visibility = View.GONE
frmBorder.setBackgroundResource(0)

textInputTv.text = text
textInputTv.setTextColor(colorCodeTextView)
Expand Down Expand Up @@ -365,9 +310,6 @@ class PhotoEditor private constructor(builder: Builder) :
brushDrawingView.brushDrawingMode = false
getLayout(ViewType.EMOJI)?.apply {
val emojiTextView = findViewById<TextView>(R.id.tvPhotoEditorText)
val frmBorder = findViewById<FrameLayout>(R.id.frmBorder)
val imgClose = findViewById<ImageView>(R.id.imgPhotoEditorClose)

if (emojiTypeface != null) {
emojiTextView.typeface = emojiTypeface
emojiTextView.text = emojiName
Expand All @@ -394,11 +336,7 @@ class PhotoEditor private constructor(builder: Builder) :
}
})
}
emojiTextView.textSize = 56f

// hide cross and background borders for now
imgClose.visibility = View.GONE
frmBorder.setBackgroundResource(0)
// emojiTextView.textSize = 56f

val multiTouchListenerInstance = newMultiTouchListener
multiTouchListenerInstance.setOnGestureControl(object : MultiTouchListener.OnGestureControl {
Expand All @@ -421,11 +359,22 @@ class PhotoEditor private constructor(builder: Builder) :
* @param rootView rootview of image,text and emoji
*/
private fun addViewToParent(rootView: View, viewType: ViewType, sourceUri: Uri? = null) {
val params = RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
)
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE)
parentView.addView(rootView, params)
if (viewType != EMOJI) {
val params = RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
)
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE)
parentView.addView(rootView, params)
} else {
val params = RelativeLayout.LayoutParams(
context.resources.getDimensionPixelSize(R.dimen.autosize_tv_initial_height),
context.resources.getDimensionPixelSize(R.dimen.autosize_tv_initial_height)
)
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE)
rootView.tvPhotoEditorText.layoutParams = params

parentView.addView(rootView, params)
}
addedViews.add(AddedView(rootView, viewType, sourceUri))
mOnPhotoEditorListener?.onAddViewListener(viewType, addedViews.size)
}
Expand All @@ -451,24 +400,20 @@ class PhotoEditor private constructor(builder: Builder) :
ViewType.IMAGE -> rootView = layoutInflater.inflate(R.layout.view_photo_editor_image, null)
ViewType.EMOJI -> {
rootView = layoutInflater.inflate(R.layout.view_photo_editor_text, null)
val txtTextEmoji = rootView.tvPhotoEditorText
if (txtTextEmoji != null) {
if (mDefaultEmojiTypeface != null) {
txtTextEmoji.typeface = mDefaultEmojiTypeface
if (rootView.tvPhotoEditorText != null && mDefaultTextTypeface != null) {
rootView.tvPhotoEditorText.gravity = Gravity.CENTER
if (mDefaultTextTypeface != null) {
rootView.tvPhotoEditorText.typeface = mDefaultTextTypeface
}
txtTextEmoji.gravity = Gravity.CENTER
txtTextEmoji.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
}
rootView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
}
}

if (rootView != null) {
// We are setting tag as ViewType to identify what type of the view it is
// when we remove the view from stack i.e onRemoveViewListener(ViewType viewType, int numberOfAddedViews);
rootView.tag = viewType
val imgClose = rootView.findViewById<ImageView>(R.id.imgPhotoEditorClose)
val finalRootView = rootView
imgClose?.setOnClickListener { viewUndo(finalRootView, viewType) }
}
return rootView
}
Expand All @@ -479,7 +424,7 @@ class PhotoEditor private constructor(builder: Builder) :
* @param brushDrawingMode true if mode is enabled
*/
fun setBrushDrawingMode(brushDrawingMode: Boolean) {
brushDrawingView.brushDrawingMode = brushDrawingMode
brushDrawingView.brushDrawingMode = brushDrawingMode
}

/**
Expand Down Expand Up @@ -615,15 +560,15 @@ class PhotoEditor private constructor(builder: Builder) :
*/
@UiThread
fun clearHelperBox() {
for (i in 0 until parentView.childCount) {
val childAt = parentView.getChildAt(i)
val frmBorder = childAt.findViewById<FrameLayout>(R.id.frmBorder)
frmBorder?.setBackgroundResource(0)
val imgClose = childAt.findViewById<ImageView>(R.id.imgPhotoEditorClose)
if (imgClose != null) {
imgClose.visibility = View.GONE
}
}
// for (i in 0 until parentView.childCount) {
// val childAt = parentView.getChildAt(i)
// val frmBorder = childAt.findViewById<FrameLayout>(R.id.frmBorder)
// frmBorder?.setBackgroundResource(0)
// val imgClose = childAt.findViewById<ImageView>(R.id.imgPhotoEditorClose)
// if (imgClose != null) {
// imgClose.visibility = View.GONE
// }
// }
}

/**
Expand Down
42 changes: 10 additions & 32 deletions photoeditor/src/main/res/layout/view_photo_editor_text.xml
Original file line number Diff line number Diff line change
@@ -1,34 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<FrameLayout
android:id="@+id/frmBorder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:background="@drawable/rounded_border_tv">

<TextView
android:id="@+id/tvPhotoEditorText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textColor="#000000"
android:textSize="18sp"
tools:text="Burhanuddin"
tools:textColor="@android:color/black" />

</FrameLayout>

<ImageView
android:id="@+id/imgPhotoEditorClose"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="top|start"
android:elevation="1dp"
android:src="@drawable/ic_remove" />

</FrameLayout>
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tvPhotoEditorText"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_margin="4dp"
android:textColor="#000000"
app:autoSizeTextType="uniform"
tools:text="Burhanuddin"
tools:textColor="@android:color/black" />
6 changes: 6 additions & 0 deletions photoeditor/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="autosize_tv_margin">4dp</dimen>
<dimen name="autosize_tv_initial_width">96dp</dimen>
<dimen name="autosize_tv_initial_height">96dp</dimen>
</resources>