Skip to content

Commit

Permalink
Added StickerView Text
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-manshu committed Oct 7, 2020
1 parent 8565d3f commit 44ac7d2
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 188 deletions.
12 changes: 6 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,18 @@
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 androidx.appcompat.app.AppCompatActivity
import com.gappein.StickerView
import com.gappein.util.generateSticker
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val x = StickerView(this)
x.updateText("dsfsdfsdf")
setContentView(R.layout.activity_main)

val x = Generator(this).setText(TextValues(text = "hey Himanshuhey Himan")).draw()
background.setImageBitmap(x)
background.setImageBitmap(x.generateSticker())
}
}
5 changes: 3 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
tools:context=".MainActivity">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:padding="20dp"
android:layout_height="0dp"
android:text="Hello World!"
android:id="@+id/background"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down

This file was deleted.

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

/
</manifest>
package="com.gappein" />
79 changes: 79 additions & 0 deletions sticker/src/main/java/com/gappein/StickerView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.res.ResourcesCompat
import com.gappein.sticker.util.generateColor

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

companion object {
private const val DEFAULT_STROKE = 4f
private const val DEFAULT_TEXT_SIZE = 60f
private const val mStrokeWidth: Float = 10.toFloat()
private const val strokeColor: Int = Color.WHITE
}

private var isDrawing: Boolean = false
private val mShadowColors: ColorStateList = ColorStateList.valueOf(Color.GRAY)
private val typeFace = ResourcesCompat.getFont(this.context, R.font.bumper)

init {
initResources()
}

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


private fun initResources() {
typeface = typeFace
textSize = DEFAULT_TEXT_SIZE
setTextColor(generateColor())
updateShadowColor()
}

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

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

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

textPaint.style = Paint.Style.FILL

super.onDraw(canvas)

val currentTextColor = currentTextColor
textPaint.apply {
style = Paint.Style.STROKE
strokeWidth = mStrokeWidth
}
setTextColor(strokeColor)
super.onDraw(canvas)
setTextColor(currentTextColor)
isDrawing = false
} else {
super.onDraw(canvas)
}
}

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

This file was deleted.

76 changes: 0 additions & 76 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.

10 changes: 10 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,10 @@
package com.gappein.sticker.util

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

fun View.generateColor(): Int {
val random = Random()
return Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256))
}
20 changes: 20 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,20 @@
package com.gappein.util

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

fun StickerView.generateSticker(): Bitmap {
val view = this
view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED)
val bitmap = Bitmap.createBitmap(
view.measuredWidth, view.measuredHeight,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight())
view.draw(canvas)
return bitmap
}
17 changes: 0 additions & 17 deletions sticker/src/test/java/com/gappein/sticker/ExampleUnitTest.kt

This file was deleted.

0 comments on commit 44ac7d2

Please sign in to comment.