Skip to content

Commit

Permalink
Merge pull request gappein#2 from Gappein/feature/sticker
Browse files Browse the repository at this point in the history
Added - Trigger for Change
  • Loading branch information
hi-manshu authored Oct 7, 2020
2 parents c2a322e + 0fe381e commit 7e00f30
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
12 changes: 5 additions & 7 deletions app/src/main/java/com/gappein/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.appcompat.app.AppCompatActivity
import com.gappein.Sticker
import com.gappein.ui.StickerView
import com.gappein.util.generateSticker
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val stickerView = StickerView(this)

editTextInput.addTextChangedListener(object : TextWatcher {
Expand All @@ -27,14 +26,13 @@ class MainActivity : AppCompatActivity() {

override fun afterTextChanged(s: Editable?) {
if (s.toString().length < 20) {
// stickerView.updateText(s.toString())
background.setImageBitmap(
Sticker.with(this@MainActivity, s.toString())
stickerView.generateSticker(s.toString())
)
}
}

})
}
)
}


}
13 changes: 11 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@
tools:context=".MainActivity">

<ImageView
android:id="@+id/background"
android:layout_width="0dp"
android:padding="20dp"
android:layout_height="0dp"
android:padding="20dp"
android:text="Hello World!"
android:id="@+id/background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<com.gappein.ui.StickerView
android:id="@+id/stickerView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="Hello World!"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editTextInput"
android:layout_width="match_parent"
Expand Down
22 changes: 19 additions & 3 deletions sticker/src/main/java/com/gappein/ui/StickerView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import android.content.res.ColorStateList
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.res.ResourcesCompat
import com.gappein.R
import com.gappein.util.generateColor

class StickerView(context: Context) : AppCompatTextView(context) {
class StickerView : AppCompatTextView {

companion object {
private const val DEFAULT_STROKE = 4f
Expand All @@ -25,10 +27,23 @@ class StickerView(context: Context) : AppCompatTextView(context) {
private val _typeFace = ResourcesCompat.getFont(this.context, R.font.bumper)
private val _color = generateColor()

init {
constructor(context: Context, attributeSet: AttributeSet?, defStyle: Int) : super(
context,
attributeSet,
defStyle
) {
initResources()
}

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

constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet) {
initResources()
}


private fun updateShadowColor() {
setShadowLayer(
DEFAULT_STROKE,
Expand All @@ -49,11 +64,12 @@ class StickerView(context: Context) : AppCompatTextView(context) {
}

fun updateText(text: String) {
layoutParams = ViewGroup.LayoutParams(measuredWidth, measuredHeight)
if (text.contains(DEFAULT_TEXT)) {
val splitString = text.split(DEFAULT_TEXT, limit = 2)
val firstString = splitString.first()
val lastString = splitString.last()
val displayText =firstString + "\n" + lastString
val displayText = firstString + "\n" + lastString
setText(displayText)
} else {
setText(text)
Expand Down
5 changes: 1 addition & 4 deletions sticker/src/main/java/com/gappein/util/StickerExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ fun StickerView.generateSticker(text: String): Bitmap {
val width = if (view.measuredWidth == 0) 1 else view.measuredWidth
val height = if (view.measuredHeight == 0) 1 else view.measuredHeight

val bitmap = Bitmap.createBitmap(
width, height,
Bitmap.Config.ARGB_8888
)
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)

view.layout(0, 0, width, height)
Expand Down

0 comments on commit 7e00f30

Please sign in to comment.