Skip to content

Commit

Permalink
Merge pull request gappein#1 from Gappein/feature/sticker
Browse files Browse the repository at this point in the history
Feature/sticker
  • Loading branch information
hi-manshu authored Oct 7, 2020
2 parents 3442a96 + fb55a78 commit c2a322e
Show file tree
Hide file tree
Showing 31 changed files with 406 additions and 177 deletions.
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.gappein.sample">

<application
Expand All @@ -8,7 +9,8 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
tools:ignore="AllowBackup">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
34 changes: 28 additions & 6 deletions app/src/main/java/com/gappein/sample/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
package com.gappein.sample

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.gappein.sticker.Generator
import com.gappein.sticker.model.TextValues
import android.text.Editable
import android.text.TextWatcher
import androidx.appcompat.app.AppCompatActivity
import com.gappein.Sticker
import com.gappein.ui.StickerView
import kotlinx.android.synthetic.main.activity_main.*

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

val x = Generator.setText(TextValues(text = "Himanshu")).draw()
background.setImageBitmap(x)
val stickerView = StickerView(this)

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) {
}


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

})
}


}
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ImageView
android:layout_width="0dp"
android:padding="20dp"
android:layout_height="0dp"
android:text="Hello World!"
android:id="@+id/background"
Expand All @@ -16,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>

This file was deleted.

6 changes: 1 addition & 5 deletions sticker/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gappein.sticker">

/
</manifest>
<manifest package="com.gappein" />
12 changes: 12 additions & 0 deletions sticker/src/main/java/com/gappein/Sticker.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.gappein

import android.content.Context
import android.graphics.Bitmap
import com.gappein.ui.StickerView
import com.gappein.util.generateSticker

object Sticker {

fun with(context: Context, text:String):Bitmap = StickerView(context).generateSticker(text)

}
41 changes: 0 additions & 41 deletions sticker/src/main/java/com/gappein/sticker/Generator.kt

This file was deleted.

67 changes: 0 additions & 67 deletions sticker/src/main/java/com/gappein/sticker/model/DrawaingValues.kt

This file was deleted.

16 changes: 0 additions & 16 deletions sticker/src/main/java/com/gappein/sticker/model/TextValues.kt

This file was deleted.

91 changes: 91 additions & 0 deletions sticker/src/main/java/com/gappein/ui/StickerView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.gappein.ui

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
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) {

companion object {
private const val DEFAULT_STROKE = 4f
private const val DEFAULT_TEXT_SIZE = 40f
private const val DEFAULT_TEXT = " "
private const val STROKE_WIDTH: Float = 10.toFloat()
private const val STROKE_COLOR: Int = Color.WHITE
}

private var _isDrawing: Boolean = false
private val _shadowColors: ColorStateList = ColorStateList.valueOf(Color.GRAY)
private val _typeFace = ResourcesCompat.getFont(this.context, R.font.bumper)
private val _color = generateColor()

init {
initResources()
}

private fun updateShadowColor() {
setShadowLayer(
DEFAULT_STROKE,
DEFAULT_STROKE,
DEFAULT_STROKE,
_shadowColors.getColorForState(drawableState, 0)
)
}


private fun initResources() {
typeface = _typeFace
textSize = DEFAULT_TEXT_SIZE
setTextColor(_color)
textAlignment = TEXT_ALIGNMENT_CENTER
text = DEFAULT_TEXT
updateShadowColor()
}

fun updateText(text: String) {
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
setText(displayText)
} else {
setText(text)
}
}


override fun invalidate() {
if (_isDrawing) return
super.invalidate()
}

override fun onDraw(canvas: Canvas) {
if (STROKE_WIDTH > 0) {
_isDrawing = true
val textPaint = paint

textPaint.style = Paint.Style.FILL

super.onDraw(canvas)

val currentTextColor = currentTextColor
textPaint.apply {
style = Paint.Style.STROKE
strokeWidth = STROKE_WIDTH
}
setTextColor(STROKE_COLOR)
super.onDraw(canvas)
setTextColor(currentTextColor)
_isDrawing = false
} else {
super.onDraw(canvas)
}
}
}
9 changes: 9 additions & 0 deletions sticker/src/main/java/com/gappein/util/ColorExtension.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.gappein.util

import android.graphics.Color
import java.util.*

fun generateColor(): Int {
val random = Random()
return Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256))
}
25 changes: 25 additions & 0 deletions sticker/src/main/java/com/gappein/util/StickerExtension.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.gappein.util

import android.graphics.Bitmap
import android.graphics.Canvas
import android.view.View.MeasureSpec
import com.gappein.ui.StickerView

fun StickerView.generateSticker(text: String): Bitmap {
val view = this
view.updateText(text)
view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED)

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 canvas = Canvas(bitmap)

view.layout(0, 0, width, height)
view.draw(canvas)
return bitmap
}
Loading

0 comments on commit c2a322e

Please sign in to comment.