Skip to content

Commit

Permalink
Added Sticker Example
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-manshu committed Oct 7, 2020
1 parent 9c46b34 commit 0ccad37
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
26 changes: 24 additions & 2 deletions app/src/main/java/com/gappein/sample/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.gappein.sample

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.gappein.StickerView
import com.gappein.util.generateSticker
Expand All @@ -9,8 +12,27 @@ import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val x = StickerView(this)
setContentView(R.layout.activity_main)
background.setImageBitmap(x.generateSticker("Himanshu"))
editTextInput.addTextChangedListener(object : TextWatcher{

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}


override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
val x = StickerView(this@MainActivity)
background.setImageBitmap(x.generateSticker(s.toString()))

}


override fun afterTextChanged(s: Editable?) {
if (s.toString().length<10){
}
}

})
}


}
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editTextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
5 changes: 3 additions & 2 deletions sticker/src/main/java/com/gappein/StickerView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.gappein

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
Expand All @@ -22,6 +21,7 @@ class StickerView(context: Context) : AppCompatTextView(context) {
private var isDrawing: Boolean = false
private val mShadowColors: ColorStateList = ColorStateList.valueOf(Color.GRAY)
private val typeFace = ResourcesCompat.getFont(this.context, R.font.bumper)
private val COLOR = generateColor()

init {
initResources()
Expand All @@ -40,12 +40,13 @@ class StickerView(context: Context) : AppCompatTextView(context) {
private fun initResources() {
typeface = typeFace
textSize = DEFAULT_TEXT_SIZE
setTextColor(generateColor())
setTextColor(COLOR)
updateShadowColor()
}

fun updateText(text: String) {
setText(text)
invalidate()
}

override fun invalidate() {
Expand Down
2 changes: 1 addition & 1 deletion sticker/src/main/java/com/gappein/util/StickerExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fun StickerView.generateSticker(text:String): Bitmap {
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight())
view.layout(0, 0, view.measuredWidth, view.measuredHeight)
view.draw(canvas)
return bitmap
}

0 comments on commit 0ccad37

Please sign in to comment.