aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/kotlin/ca/allanwang
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-06-29 00:29:46 -0700
committerAllan Wang <me@allanwang.ca>2017-06-29 00:29:46 -0700
commit8f2e56eb5ecfc6b5ce2f1530ead6fe73f5bfe89c (patch)
treef6e3d074c1f5d21faac6375f356607408e1e465c /library/src/main/kotlin/ca/allanwang
parentb45156a92f9b2f3fcce62fd58d25474196048f1a (diff)
downloadkau-8f2e56eb5ecfc6b5ce2f1530ead6fe73f5bfe89c.tar.gz
kau-8f2e56eb5ecfc6b5ce2f1530ead6fe73f5bfe89c.tar.bz2
kau-8f2e56eb5ecfc6b5ce2f1530ead6fe73f5bfe89c.zip
Completely rewrite about activity
Diffstat (limited to 'library/src/main/kotlin/ca/allanwang')
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/about/AboutActivityBase.kt75
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/iitems/CardIItem.kt116
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/iitems/CutoutIItem.kt44
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/iitems/LibraryIItem.kt (renamed from library/src/main/kotlin/ca/allanwang/kau/about/LibraryItem.kt)6
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt2
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt1
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt10
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt22
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/views/CutoutView.kt (renamed from library/src/main/kotlin/ca/allanwang/kau/views/CutoutTextView.kt)50
9 files changed, 252 insertions, 74 deletions
diff --git a/library/src/main/kotlin/ca/allanwang/kau/about/AboutActivityBase.kt b/library/src/main/kotlin/ca/allanwang/kau/about/AboutActivityBase.kt
index c518ca6..143e92a 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/about/AboutActivityBase.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/about/AboutActivityBase.kt
@@ -1,26 +1,24 @@
package ca.allanwang.kau.about
+import android.graphics.drawable.Drawable
import android.os.Bundle
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
-import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.transition.TransitionInflater
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
-import android.widget.FrameLayout
-import android.widget.TextView
import ca.allanwang.kau.R
-import ca.allanwang.kau.utils.bindView
-import ca.allanwang.kau.utils.dimenPixelSize
-import ca.allanwang.kau.utils.string
-import ca.allanwang.kau.views.CutoutTextView
+import ca.allanwang.kau.iitems.CutoutIItem
+import ca.allanwang.kau.iitems.LibraryIItem
+import ca.allanwang.kau.utils.*
import ca.allanwang.kau.widgets.ElasticDragDismissFrameLayout
import ca.allanwang.kau.widgets.InkPageIndicator
import com.mikepenz.aboutlibraries.Libs
import com.mikepenz.aboutlibraries.entity.Library
+import com.mikepenz.fastadapter.IItem
import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
@@ -66,7 +64,9 @@ abstract class AboutActivityBase(val rClass: Class<*>, val configBuilder: Config
inner class Configs {
var cutoutTextRes: Int = -1
- val cutoutText: String? = "KAU" //todo make null
+ var cutoutText: String? = null
+ var cutoutDrawableRes: Int = -1
+ var cutoutDrawable: Drawable? = null
var mainPageTitleRes: Int = -1
var mainPageTitle: String = "Kau test"
var libPageTitleRes: Int = -1
@@ -78,6 +78,11 @@ abstract class AboutActivityBase(val rClass: Class<*>, val configBuilder: Config
open val pageCount: Int = 2
+ /**
+ * Gets the view associated with the given page position
+ * Keep in mind that when inflating, do NOT add the view to the viewgroup
+ * Use layoutInflater.inflate(id, parent, false)
+ */
open fun getPage(position: Int, layoutInflater: LayoutInflater, parent: ViewGroup): View {
return when (position) {
0 -> inflateMainPage(layoutInflater, parent)
@@ -86,43 +91,35 @@ abstract class AboutActivityBase(val rClass: Class<*>, val configBuilder: Config
}
}
- fun inflateMainPage(layoutInflater: LayoutInflater, parent: ViewGroup): View {
- val v = layoutInflater.inflate(R.layout.kau_about_section_main, parent, false)
- postInflateMainPage(
- v.findViewById<CutoutTextView>(R.id.about_main_cutout),
- v.findViewById<FrameLayout>(R.id.about_main_bottom_container),
- v.findViewById<TextView>(R.id.about_main_bottom_text)
- )
- return v
- }
-
- open fun postInflateMainPage(cutout: CutoutTextView, bottomContainer: FrameLayout, bottomText: TextView) {
- with (configs) {
- cutout.text = string(cutoutTextRes, cutoutText)
- bottomText.text = string(mainPageTitleRes, mainPageTitle)
+ open fun inflateMainPage(layoutInflater: LayoutInflater, parent: ViewGroup): View {
+ val fastAdapter = FastItemAdapter<IItem<*, *>>()
+ val recycler = fullLinearRecycler {
+ adapter = fastAdapter
}
+ fastAdapter.add(CutoutIItem {
+ with(configs) {
+ text = string(cutoutTextRes, cutoutText)
+ drawable = drawable(cutoutDrawableRes, cutoutDrawable)
+ }
+ })
+ return recycler
}
- fun inflateLibPage(layoutInflater: LayoutInflater, parent: ViewGroup): View {
- val v = layoutInflater.inflate(R.layout.kau_about_section_libraries, parent, false)
- postInflateLibPage(
- v.findViewById<TextView>(R.id.about_library_title),
- v.findViewById<RecyclerView>(R.id.about_library_recycler)
- )
- return v
- }
- open fun postInflateLibPage(title: TextView, recycler: RecyclerView) {
- title.text = string(configs.libPageTitleRes, configs.libPageTitle)
- val libAdapter = FastItemAdapter<LibraryItem>()
- with(recycler) {
- layoutManager = LinearLayoutManager(this@AboutActivityBase)
- adapter = libAdapter
- }
+ open fun inflateLibPage(layoutInflater: LayoutInflater, parent: ViewGroup): View {
+ val v = layoutInflater.inflate(R.layout.kau_recycler_detached_background, parent, false)
+ val fastAdapter = FastItemAdapter<IItem<*, *>>()
+ val recycler = v.findViewById<RecyclerView>(R.id.kau_recycler_detached)
+ recycler.adapter = fastAdapter
+ val background = v.findViewById<View>(R.id.kau_recycler_detached_background)
doAsync {
- val libs = getLibraries(Libs(this@AboutActivityBase, Libs.toStringArray(rClass.fields))).map { LibraryItem(it) }
- uiThread { libAdapter.add(libs) }
+ val libs = getLibraries(Libs(this@AboutActivityBase, Libs.toStringArray(rClass.fields))).map { LibraryIItem(it) }
+ uiThread {
+ recycler.transitionDelayed(R.transition.kau_enter_slide_top) //TODO fix this
+ fastAdapter.add(libs)
+ }
}
+ return v
}
inner class AboutPagerAdapter : PagerAdapter() {
diff --git a/library/src/main/kotlin/ca/allanwang/kau/iitems/CardIItem.kt b/library/src/main/kotlin/ca/allanwang/kau/iitems/CardIItem.kt
new file mode 100644
index 0000000..36e7ef8
--- /dev/null
+++ b/library/src/main/kotlin/ca/allanwang/kau/iitems/CardIItem.kt
@@ -0,0 +1,116 @@
+package ca.allanwang.kau.iitems
+
+import android.graphics.Color
+import android.graphics.drawable.Drawable
+import android.support.v7.widget.CardView
+import android.support.v7.widget.RecyclerView
+import android.view.View
+import android.widget.Button
+import android.widget.ImageView
+import android.widget.LinearLayout
+import android.widget.TextView
+import ca.allanwang.kau.R
+import ca.allanwang.kau.utils.*
+import com.mikepenz.fastadapter.FastAdapter
+import com.mikepenz.fastadapter.items.AbstractItem
+import com.mikepenz.fastadapter.listeners.ClickEventHook
+import com.mikepenz.iconics.typeface.IIcon
+
+/**
+ * Created by Allan Wang on 2017-06-28.
+ *
+ * Simple generic card item with an icon, title, description and button
+ * The icon and button are hidden by default unless values are given
+ */
+class CardIItem(val builder: Config.() -> Unit = {}) : AbstractItem<CardIItem, CardIItem.ViewHolder>() {
+
+
+ companion object {
+ fun bindClickEvents(fastAdapter: FastAdapter<CardIItem>) {
+ fastAdapter.withEventHook(object : ClickEventHook<CardIItem>() {
+ override fun onBindMany(viewHolder: RecyclerView.ViewHolder): List<View>? {
+ return if (viewHolder is ViewHolder) listOf(viewHolder.card, viewHolder.button) else null
+ }
+
+ override fun onClick(v: View, position: Int, adapter: FastAdapter<CardIItem>, item: CardIItem) {
+ with(item.configs) {
+ when (v.id) {
+ R.id.kau_card_container -> cardClick?.onClick(v)
+ R.id.kau_card_button -> buttonClick?.onClick(v)
+ else -> {
+ }
+ }
+ }
+ }
+ })
+ }
+ }
+
+ val configs = Config().apply { builder() }
+
+ class Config {
+ var title: String? = null
+ var titleRes: Int = -1
+ var desc: String? = null
+ var descRes: Int = -1
+ var button: String? = null
+ var buttonRes: Int = -1
+ var buttonClick: View.OnClickListener? = null
+ var cardClick: View.OnClickListener? = null
+ var image: Drawable? = null
+ var imageIIcon: IIcon? = null
+ var imageIIconColor: Int = Color.WHITE
+ var imageRes: Int = -1
+ }
+
+
+ override fun getType(): Int = R.id.kau_item_card
+
+ override fun getLayoutRes(): Int = R.layout.kau_iitem_card
+
+ override fun bindView(holder: ViewHolder, payloads: MutableList<Any>?) {
+ super.bindView(holder, payloads)
+ with(holder.itemView.context) context@ {
+ with(configs) {
+ holder.title.text = string(titleRes, title)
+ holder.description.text = string(descRes, desc)
+ val buttonText = string(buttonRes, button)
+ if (buttonText != null) {
+ holder.bottomRow.visible()
+ holder.button.text = buttonText
+ holder.button.setOnClickListener(buttonClick)
+ }
+ holder.icon.setImageDrawable(
+ if (imageRes > 0) drawable(imageRes)
+ else if (imageIIcon != null) imageIIcon!!.toDrawable(this@context, sizeDp = 40, color = imageIIconColor)
+ else image
+ )
+ holder.card.setOnClickListener(cardClick)
+ }
+ }
+ }
+
+ override fun unbindView(holder: ViewHolder) {
+ super.unbindView(holder)
+ with(holder) {
+ icon.gone().setImageDrawable(null)
+ title.text = null
+ description.text = null
+ bottomRow.gone()
+ button.setOnClickListener(null)
+ card.setOnClickListener(null)
+ }
+ }
+
+ override fun getViewHolder(v: View): ViewHolder = ViewHolder(v)
+
+ class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
+ val card: CardView by bindView(R.id.kau_card_container)
+ val icon: ImageView by bindView(R.id.kau_card_image)
+ val title: TextView by bindView(R.id.kau_card_title)
+ val description: TextView by bindView(R.id.kau_card_description)
+ val bottomRow: LinearLayout by bindView(R.id.kau_card_bottom_row)
+ val button: Button by bindView(R.id.kau_card_button)
+ }
+
+} \ No newline at end of file
diff --git a/library/src/main/kotlin/ca/allanwang/kau/iitems/CutoutIItem.kt b/library/src/main/kotlin/ca/allanwang/kau/iitems/CutoutIItem.kt
new file mode 100644
index 0000000..d138917
--- /dev/null
+++ b/library/src/main/kotlin/ca/allanwang/kau/iitems/CutoutIItem.kt
@@ -0,0 +1,44 @@
+package ca.allanwang.kau.iitems
+
+import android.support.v7.widget.RecyclerView
+import android.view.View
+import ca.allanwang.kau.R
+import ca.allanwang.kau.utils.bindView
+import ca.allanwang.kau.views.CutoutView
+import com.mikepenz.fastadapter.items.AbstractItem
+
+/**
+ * Created by Allan Wang on 2017-06-28.
+ *
+ * Just a cutout item with some defaults in [R.layout.kau_iitem_cutout]
+ */
+class CutoutIItem(val config: CutoutView.() -> Unit = {}) : AbstractItem<CutoutIItem, CutoutIItem.ViewHolder>() {
+
+ override fun getType(): Int = R.id.kau_item_cutout
+
+ override fun getLayoutRes(): Int = R.layout.kau_iitem_cutout
+
+ override fun isSelectable(): Boolean = false
+
+ override fun bindView(holder: ViewHolder, payloads: MutableList<Any>?) {
+ super.bindView(holder, payloads)
+ with(holder) {
+ cutout.config()
+ }
+ }
+
+ override fun unbindView(holder: ViewHolder) {
+ super.unbindView(holder)
+ with(holder) {
+ cutout.drawable = null
+ cutout.text = "Text" //back to default
+ }
+ }
+
+ override fun getViewHolder(v: View): ViewHolder = ViewHolder(v)
+
+ class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
+ val cutout: CutoutView by bindView(R.id.kau_cutout)
+ }
+
+} \ No newline at end of file
diff --git a/library/src/main/kotlin/ca/allanwang/kau/about/LibraryItem.kt b/library/src/main/kotlin/ca/allanwang/kau/iitems/LibraryIItem.kt
index c3bfb5a..e617aaa 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/about/LibraryItem.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/iitems/LibraryIItem.kt
@@ -1,4 +1,4 @@
-package ca.allanwang.kau.about
+package ca.allanwang.kau.iitems
/**
* Created by Allan Wang on 2017-06-27.
@@ -22,11 +22,11 @@ import com.mikepenz.fastadapter.items.AbstractItem
/**
* Created by mikepenz on 28.12.15.
*/
-class LibraryItem(val lib: Library) : AbstractItem<LibraryItem, LibraryItem.ViewHolder>() {
+class LibraryIItem(val lib: Library) : AbstractItem<LibraryIItem, LibraryIItem.ViewHolder>() {
override fun getType(): Int = R.id.kau_item_about_library
- override fun getLayoutRes(): Int = R.layout.kau_about_item_library
+ override fun getLayoutRes(): Int = R.layout.kau_about_iitem_library
override fun isSelectable(): Boolean = false
diff --git a/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt b/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt
index a672e8a..60727a5 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt
@@ -47,7 +47,7 @@ class SearchItem(val key: String,
styledContent!!.setSpan(StyleSpan(Typeface.BOLD), index, index + subText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
- override fun getLayoutRes(): Int = R.layout.kau_search_item
+ override fun getLayoutRes(): Int = R.layout.kau_search_iitem
override fun getType(): Int = R.id.kau_item_search
diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
index 876f634..2a929a2 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
@@ -82,6 +82,7 @@ fun Context.integer(@IntegerRes id: Int): Int = resources.getInteger(id)
fun Context.dimen(@DimenRes id: Int): Float = resources.getDimension(id)
fun Context.dimenPixelSize(@DimenRes id: Int): Int = resources.getDimensionPixelSize(id)
fun Context.drawable(@DrawableRes id: Int): Drawable = ContextCompat.getDrawable(this, id)
+fun Context.drawable(@DrawableRes id: Int, fallback: Drawable?): Drawable? = if (id > 0) drawable(id) else fallback
//Attr retrievers
fun Context.resolveColor(@AttrRes attr: Int, fallback: Int = 0): Int {
diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt
index 6c1b6a1..55fb9ea 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt
@@ -60,7 +60,11 @@ annotation class KauUtils
return formatter.format(this)
}
-@KauUtils fun Drawable.toBitmap(scaling: Float = 1f): Bitmap {
+/**
+ * Extracts the bitmap of a drawable, and applies a scale if given
+ * For solid colors, a 1 x 1 pixel will be generated
+ */
+@KauUtils fun Drawable.toBitmap(scaling: Float = 1f, config: Bitmap.Config = Bitmap.Config.ARGB_8888): Bitmap {
if (this is BitmapDrawable && bitmap != null) {
if (scaling == 1f) return bitmap
val width = (bitmap.width * scaling).toInt()
@@ -68,9 +72,9 @@ annotation class KauUtils
return Bitmap.createScaledBitmap(bitmap, width, height, false)
}
val bitmap = if (intrinsicWidth <= 0 || intrinsicHeight <= 0)
- Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888) // Single color bitmap will be created of 1x1 pixel
+ Bitmap.createBitmap(1, 1, config)
else
- Bitmap.createBitmap((intrinsicWidth * scaling).toInt(), (intrinsicHeight * scaling).toInt(), Bitmap.Config.ARGB_8888)
+ Bitmap.createBitmap((intrinsicWidth * scaling).toInt(), (intrinsicHeight * scaling).toInt(), config)
val canvas = Canvas(bitmap)
setBounds(0, 0, canvas.width, canvas.height)
draw(canvas)
diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt
index 0f070d1..3fb8f27 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt
@@ -6,9 +6,14 @@ import android.graphics.Outline
import android.graphics.Rect
import android.support.annotation.ColorInt
import android.support.annotation.StringRes
+import android.support.annotation.TransitionRes
import android.support.design.widget.Snackbar
import android.support.transition.AutoTransition
+import android.support.transition.Transition
+import android.support.transition.TransitionInflater
import android.support.transition.TransitionManager
+import android.support.v7.widget.LinearLayoutManager
+import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.view.ViewOutlineProvider
@@ -77,6 +82,12 @@ fun View.snackbar(@StringRes textId: Int, duration: Int = Snackbar.LENGTH_LONG,
TransitionManager.beginDelayedTransition(this, transition)
}
+@KauUtils fun ViewGroup.transitionDelayed(@TransitionRes id: Int, builder: Transition.() -> Unit = {}) {
+ val transition = TransitionInflater.from(context).inflateTransition(id)
+ transition.builder()
+ TransitionManager.beginDelayedTransition(this, transition)
+}
+
@KauUtils fun View.setRippleBackground(@ColorInt foregroundColor: Int, @ColorInt backgroundColor: Int) {
background = createSimpleRippleDrawable(foregroundColor, backgroundColor)
}
@@ -99,4 +110,15 @@ val CIRCULAR_OUTLINE: ViewOutlineProvider = object : ViewOutlineProvider() {
view.width - view.paddingRight,
view.height - view.paddingBottom)
}
+}
+
+/**
+ * Generates a recycler view with match parent and a linearlayoutmanager, since it's so commonly used
+ */
+fun Context.fullLinearRecycler(configs: RecyclerView.() -> Unit = {}): RecyclerView {
+ return RecyclerView(this).apply {
+ layoutManager = LinearLayoutManager(this@fullLinearRecycler)
+ layoutParams = RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.MATCH_PARENT)
+ configs()
+ }
} \ No newline at end of file
diff --git a/library/src/main/kotlin/ca/allanwang/kau/views/CutoutTextView.kt b/library/src/main/kotlin/ca/allanwang/kau/views/CutoutView.kt
index 83ed6dd..023bdb4 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/views/CutoutTextView.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/views/CutoutView.kt
@@ -33,7 +33,7 @@ import ca.allanwang.kau.utils.toBitmap
/**
* A view which punches out some text from an opaque color block, allowing you to see through it.
*/
-class CutoutTextView @JvmOverloads constructor(
+class CutoutView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
@@ -43,8 +43,7 @@ class CutoutTextView @JvmOverloads constructor(
const val TYPE_DRAWABLE = 101
}
- private val textPaint: TextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
- private val bitmapPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
+ private val paint: TextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
private var bitmapScaling: Float = 1f
private var cutout: Bitmap? = null
var foregroundColor = Color.MAGENTA
@@ -58,7 +57,7 @@ class CutoutTextView @JvmOverloads constructor(
private var textSize: Float = 0f
private var cutoutY: Float = 0f
private var cutoutX: Float = 0f
- private var drawable: Drawable? = null
+ var drawable: Drawable? = null
set(value) {
field = value
if (value != null) cutoutType = TYPE_DRAWABLE
@@ -70,13 +69,13 @@ class CutoutTextView @JvmOverloads constructor(
init {
if (attrs != null) {
- val a = context.obtainStyledAttributes(attrs, R.styleable.CutoutTextView, 0, 0)
- if (a.hasValue(R.styleable.CutoutTextView_font))
- textPaint.typeface = context.getFont(a.getString(R.styleable.CutoutTextView_font))
- foregroundColor = a.getColor(R.styleable.CutoutTextView_foregroundColor, foregroundColor)
- text = a.getString(R.styleable.CutoutTextView_android_text) ?: text
- minHeight = a.getDimension(R.styleable.CutoutTextView_android_minHeight, minHeight)
- heightPercentage = a.getFloat(R.styleable.CutoutTextView_heightPercentageToScreen, heightPercentage)
+ val a = context.obtainStyledAttributes(attrs, R.styleable.CutoutView, 0, 0)
+ if (a.hasValue(R.styleable.CutoutView_font))
+ paint.typeface = context.getFont(a.getString(R.styleable.CutoutView_font))
+ foregroundColor = a.getColor(R.styleable.CutoutView_foregroundColor, foregroundColor)
+ text = a.getString(R.styleable.CutoutView_android_text) ?: text
+ minHeight = a.getDimension(R.styleable.CutoutView_android_minHeight, minHeight)
+ heightPercentage = a.getFloat(R.styleable.CutoutView_heightPercentageToScreen, heightPercentage)
a.recycle()
}
maxTextSize = context.dimenPixelSize(R.dimen.kau_display_4_text_size).toFloat()
@@ -97,14 +96,14 @@ class CutoutTextView @JvmOverloads constructor(
private fun calculateTextPosition() {
val targetWidth = width / PHI
- textSize = getSingleLineTextSize(text!!, textPaint, targetWidth, 0f, maxTextSize,
+ textSize = getSingleLineTextSize(text!!, paint, targetWidth, 0f, maxTextSize,
0.5f, resources.displayMetrics)
- textPaint.textSize = textSize
+ paint.textSize = textSize
// measuring text is fun :] see: https://chris.banes.me/2014/03/27/measuring-text/
- cutoutX = (width - textPaint.measureText(text)) / 2
+ cutoutX = (width - paint.measureText(text)) / 2
val textBounds = Rect()
- textPaint.getTextBounds(text, 0, text!!.length, textBounds)
+ paint.getTextBounds(text, 0, text!!.length, textBounds)
val textHeight = textBounds.height().toFloat()
cutoutY = (height + textHeight) / 2
}
@@ -146,15 +145,10 @@ class CutoutTextView @JvmOverloads constructor(
paint.textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, mid, metrics)
val maxLineWidth = paint.measureText(text)
- if (high - low < precision) {
- return low
- } else if (maxLineWidth > targetWidth) {
- return getSingleLineTextSize(text, paint, targetWidth, low, mid, precision, metrics)
- } else if (maxLineWidth < targetWidth) {
- return getSingleLineTextSize(text, paint, targetWidth, mid, high, precision, metrics)
- } else {
- return mid
- }
+ return if (high - low < precision) low
+ else if (maxLineWidth > targetWidth) getSingleLineTextSize(text, paint, targetWidth, low, mid, precision, metrics)
+ else if (maxLineWidth < targetWidth) getSingleLineTextSize(text, paint, targetWidth, mid, high, precision, metrics)
+ else mid
}
private fun createBitmap() {
@@ -165,16 +159,16 @@ class CutoutTextView @JvmOverloads constructor(
cutout!!.setHasAlpha(true)
val cutoutCanvas = Canvas(cutout!!)
cutoutCanvas.drawColor(foregroundColor)
+ paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
when (cutoutType) {
TYPE_TEXT -> {
// this is the magic – Clear mode punches out the bitmap
- textPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
- cutoutCanvas.drawText(text, cutoutX, cutoutY, textPaint)
+ paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
+ cutoutCanvas.drawText(text, cutoutX, cutoutY, paint)
}
TYPE_DRAWABLE -> {
- bitmapPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
- cutoutCanvas.drawBitmap(drawable!!.toBitmap(bitmapScaling), cutoutX, cutoutY, bitmapPaint)
+ cutoutCanvas.drawBitmap(drawable!!.toBitmap(bitmapScaling, Bitmap.Config.ALPHA_8), cutoutX, cutoutY, paint)
}
}